FIRStorageUpdateMetadataTests.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "FirebaseStorage/Sources/FIRStorageMetadata_Private.h"
  15. #import "FirebaseStorage/Sources/FIRStorageUpdateMetadataTask.h"
  16. #import "FirebaseStorage/Tests/Unit/FIRStorageTestHelpers.h"
  17. @interface FIRStorageUpdateMetadataTests : XCTestCase
  18. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  19. @property(nonatomic) dispatch_queue_t dispatchQueue;
  20. @property(strong, nonatomic) FIRIMPLStorageMetadata *metadata;
  21. @property(strong, nonatomic) FIRIMPLStorage *storage;
  22. @property(strong, nonatomic) id mockApp;
  23. @end
  24. @implementation FIRStorageUpdateMetadataTests
  25. - (void)setUp {
  26. [super setUp];
  27. NSDictionary *metadataDict = @{@"bucket" : @"bucket", @"name" : @"path/to/object"};
  28. self.metadata = [[FIRIMPLStorageMetadata alloc] initWithDictionary:metadataDict];
  29. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  30. self.fetcherService.authorizer =
  31. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  32. fetcherService:self.fetcherService
  33. authProvider:nil
  34. appCheck:nil];
  35. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  36. self.storage = [FIRStorageTestHelpers storageWithMockedApp];
  37. }
  38. - (void)tearDown {
  39. self.fetcherService = nil;
  40. self.storage = nil;
  41. self.mockApp = nil;
  42. [super tearDown];
  43. }
  44. - (void)testFetcherConfiguration {
  45. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  46. self.fetcherService.testBlock =
  47. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  48. #pragma clang diagnostic push
  49. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  50. XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
  51. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"PATCH");
  52. NSData *bodyData = [NSData frs_dataFromJSONDictionary:[self.metadata updatedMetadata]];
  53. XCTAssertEqualObjects(fetcher.request.HTTPBody, bodyData);
  54. NSDictionary *HTTPHeaders = fetcher.request.allHTTPHeaderFields;
  55. XCTAssertEqualObjects(HTTPHeaders[@"Content-Type"], @"application/json; charset=UTF-8");
  56. XCTAssertEqualObjects(HTTPHeaders[@"Content-Length"], [@(bodyData.length) stringValue]);
  57. #pragma clang diagnostic pop
  58. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  59. statusCode:200
  60. HTTPVersion:kHTTPVersion
  61. headerFields:nil];
  62. response(httpResponse, nil, nil);
  63. self.fetcherService.testBlock = nil;
  64. };
  65. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  66. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  67. path:path];
  68. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  69. initWithReference:ref
  70. fetcherService:self.fetcherService
  71. dispatchQueue:self.dispatchQueue
  72. metadata:self.metadata
  73. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  74. [expectation fulfill];
  75. }];
  76. [task enqueue];
  77. [FIRStorageTestHelpers waitForExpectation:self];
  78. }
  79. - (void)testSuccessfulFetch {
  80. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  81. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  82. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  83. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  84. path:path];
  85. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  86. initWithReference:ref
  87. fetcherService:self.fetcherService
  88. dispatchQueue:self.dispatchQueue
  89. metadata:self.metadata
  90. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  91. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  92. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  93. XCTAssertNil(error);
  94. [expectation fulfill];
  95. }];
  96. [task enqueue];
  97. [FIRStorageTestHelpers waitForExpectation:self];
  98. }
  99. - (void)testUnsuccessfulFetchUnauthenticated {
  100. XCTestExpectation *expectation =
  101. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  102. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  103. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  104. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  105. path:path];
  106. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  107. initWithReference:ref
  108. fetcherService:self.fetcherService
  109. dispatchQueue:self.dispatchQueue
  110. metadata:self.metadata
  111. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  112. XCTAssertNil(metadata);
  113. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnauthenticated);
  114. [expectation fulfill];
  115. }];
  116. [task enqueue];
  117. [FIRStorageTestHelpers waitForExpectation:self];
  118. }
  119. - (void)testUnsuccessfulFetchUnauthorized {
  120. XCTestExpectation *expectation =
  121. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  122. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  123. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  124. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  125. path:path];
  126. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  127. initWithReference:ref
  128. fetcherService:self.fetcherService
  129. dispatchQueue:self.dispatchQueue
  130. metadata:self.metadata
  131. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  132. XCTAssertNil(metadata);
  133. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnauthorized);
  134. [expectation fulfill];
  135. }];
  136. [task enqueue];
  137. [FIRStorageTestHelpers waitForExpectation:self];
  138. }
  139. - (void)testUnsuccessfulFetchObjectDoesntExist {
  140. XCTestExpectation *expectation =
  141. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  142. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  143. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  144. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  145. path:path];
  146. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  147. initWithReference:ref
  148. fetcherService:self.fetcherService
  149. dispatchQueue:self.dispatchQueue
  150. metadata:self.metadata
  151. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  152. XCTAssertNil(metadata);
  153. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeObjectNotFound);
  154. [expectation fulfill];
  155. }];
  156. [task enqueue];
  157. [FIRStorageTestHelpers waitForExpectation:self];
  158. }
  159. - (void)testUnsuccessfulFetchBadJSON {
  160. XCTestExpectation *expectation =
  161. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  162. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  163. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  164. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  165. path:path];
  166. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  167. initWithReference:ref
  168. fetcherService:self.fetcherService
  169. dispatchQueue:self.dispatchQueue
  170. metadata:self.metadata
  171. completion:^(FIRIMPLStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  172. XCTAssertNil(metadata);
  173. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnknown);
  174. [expectation fulfill];
  175. }];
  176. [task enqueue];
  177. [FIRStorageTestHelpers waitForExpectation:self];
  178. }
  179. @end