FIRStorageUpdateMetadataTests.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FIRStorageMetadata_Private.h"
  15. #import "FIRStorageTestHelpers.h"
  16. #import "FIRStorageUpdateMetadataTask.h"
  17. @interface FIRStorageUpdateMetadataTests : XCTestCase
  18. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  19. @property(strong, nonatomic) FIRStorageMetadata *metadata;
  20. @property(strong, nonatomic) FIRStorage *storage;
  21. @property(strong, nonatomic) id mockApp;
  22. @end
  23. @implementation FIRStorageUpdateMetadataTests
  24. - (void)setUp {
  25. [super setUp];
  26. NSDictionary *metadataDict = @{@"bucket" : @"bucket", @"name" : @"path/to/object"};
  27. self.metadata = [[FIRStorageMetadata alloc] initWithDictionary:metadataDict];
  28. id mockOptions = OCMClassMock([FIROptions class]);
  29. OCMStub([mockOptions storageBucket]).andReturn(@"bucket.appspot.com");
  30. self.mockApp = OCMClassMock([FIRApp class]);
  31. OCMStub([self.mockApp name]).andReturn(kFIRStorageAppName);
  32. OCMStub([(FIRApp *)self.mockApp options]).andReturn(mockOptions);
  33. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  34. self.fetcherService.authorizer =
  35. [[FIRStorageTokenAuthorizer alloc] initWithApp:self.mockApp
  36. fetcherService:self.fetcherService];
  37. self.storage = [FIRStorage storageForApp:self.mockApp];
  38. }
  39. - (void)tearDown {
  40. self.fetcherService = nil;
  41. self.storage = nil;
  42. self.mockApp = nil;
  43. [super tearDown];
  44. }
  45. - (void)testFetcherConfiguration {
  46. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  47. self.fetcherService.testBlock =
  48. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  49. #pragma clang diagnostic push
  50. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  51. XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
  52. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"PATCH");
  53. NSData *bodyData = [NSData frs_dataFromJSONDictionary:[self.metadata updatedMetadata]];
  54. XCTAssertEqualObjects(fetcher.request.HTTPBody, bodyData);
  55. NSDictionary *HTTPHeaders = fetcher.request.allHTTPHeaderFields;
  56. XCTAssertEqualObjects(HTTPHeaders[@"Content-Type"], @"application/json; charset=UTF-8");
  57. XCTAssertEqualObjects(HTTPHeaders[@"Content-Length"], [@(bodyData.length) stringValue]);
  58. #pragma clang diagnostic pop
  59. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  60. statusCode:200
  61. HTTPVersion:kHTTPVersion
  62. headerFields:nil];
  63. response(httpResponse, nil, nil);
  64. self.fetcherService.testBlock = nil;
  65. };
  66. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  67. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  68. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  69. initWithReference:ref
  70. fetcherService:self.fetcherService
  71. metadata:self.metadata
  72. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  73. [expectation fulfill];
  74. }];
  75. [task enqueue];
  76. [FIRStorageTestHelpers waitForExpectation:self];
  77. }
  78. - (void)testSuccessfulFetch {
  79. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  80. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  81. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  82. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  83. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  84. initWithReference:ref
  85. fetcherService:self.fetcherService
  86. metadata:self.metadata
  87. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  88. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  89. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  90. XCTAssertNil(error);
  91. [expectation fulfill];
  92. }];
  93. [task enqueue];
  94. [FIRStorageTestHelpers waitForExpectation:self];
  95. }
  96. - (void)testUnsuccessfulFetchUnauthenticated {
  97. XCTestExpectation *expectation =
  98. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  99. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  100. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  101. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  102. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  103. initWithReference:ref
  104. fetcherService:self.fetcherService
  105. metadata:self.metadata
  106. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  107. XCTAssertNil(metadata);
  108. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  109. [expectation fulfill];
  110. }];
  111. [task enqueue];
  112. [FIRStorageTestHelpers waitForExpectation:self];
  113. }
  114. - (void)testUnsuccessfulFetchUnauthorized {
  115. XCTestExpectation *expectation =
  116. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  117. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  118. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  119. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  120. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  121. initWithReference:ref
  122. fetcherService:self.fetcherService
  123. metadata:self.metadata
  124. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  125. XCTAssertNil(metadata);
  126. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  127. [expectation fulfill];
  128. }];
  129. [task enqueue];
  130. [FIRStorageTestHelpers waitForExpectation:self];
  131. }
  132. - (void)testUnsuccessfulFetchObjectDoesntExist {
  133. XCTestExpectation *expectation =
  134. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  135. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  136. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  137. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  138. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  139. initWithReference:ref
  140. fetcherService:self.fetcherService
  141. metadata:self.metadata
  142. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  143. XCTAssertNil(metadata);
  144. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  145. [expectation fulfill];
  146. }];
  147. [task enqueue];
  148. [FIRStorageTestHelpers waitForExpectation:self];
  149. }
  150. - (void)testUnsuccessfulFetchBadJSON {
  151. XCTestExpectation *expectation =
  152. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  153. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  154. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  155. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  156. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  157. initWithReference:ref
  158. fetcherService:self.fetcherService
  159. metadata:self.metadata
  160. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  161. XCTAssertNil(metadata);
  162. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  163. [expectation fulfill];
  164. }];
  165. [task enqueue];
  166. [FIRStorageTestHelpers waitForExpectation:self];
  167. }
  168. @end