FIRStorageUpdateMetadataTests.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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(nonatomic) dispatch_queue_t dispatchQueue;
  20. @property(strong, nonatomic) FIRStorageMetadata *metadata;
  21. @property(strong, nonatomic) FIRStorage *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 = [[FIRStorageMetadata alloc] initWithDictionary:metadataDict];
  29. id mockOptions = OCMClassMock([FIROptions class]);
  30. OCMStub([mockOptions storageBucket]).andReturn(@"bucket.appspot.com");
  31. self.mockApp = [FIRStorageTestHelpers mockedApp];
  32. OCMStub([self.mockApp name]).andReturn(kFIRStorageAppName);
  33. OCMStub([(FIRApp *)self.mockApp options]).andReturn(mockOptions);
  34. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  35. self.fetcherService.authorizer =
  36. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  37. fetcherService:self.fetcherService
  38. authProvider:nil];
  39. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  40. self.storage = [FIRStorage storageForApp:self.mockApp];
  41. }
  42. - (void)tearDown {
  43. self.fetcherService = nil;
  44. self.storage = nil;
  45. self.mockApp = nil;
  46. [super tearDown];
  47. }
  48. - (void)testFetcherConfiguration {
  49. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  50. self.fetcherService.testBlock =
  51. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  52. #pragma clang diagnostic push
  53. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  54. XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
  55. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"PATCH");
  56. NSData *bodyData = [NSData frs_dataFromJSONDictionary:[self.metadata updatedMetadata]];
  57. XCTAssertEqualObjects(fetcher.request.HTTPBody, bodyData);
  58. NSDictionary *HTTPHeaders = fetcher.request.allHTTPHeaderFields;
  59. XCTAssertEqualObjects(HTTPHeaders[@"Content-Type"], @"application/json; charset=UTF-8");
  60. XCTAssertEqualObjects(HTTPHeaders[@"Content-Length"], [@(bodyData.length) stringValue]);
  61. #pragma clang diagnostic pop
  62. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  63. statusCode:200
  64. HTTPVersion:kHTTPVersion
  65. headerFields:nil];
  66. response(httpResponse, nil, nil);
  67. self.fetcherService.testBlock = nil;
  68. };
  69. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  70. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  71. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  72. initWithReference:ref
  73. fetcherService:self.fetcherService
  74. dispatchQueue:self.dispatchQueue
  75. metadata:self.metadata
  76. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  77. [expectation fulfill];
  78. }];
  79. [task enqueue];
  80. [FIRStorageTestHelpers waitForExpectation:self];
  81. }
  82. - (void)testSuccessfulFetch {
  83. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  84. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  85. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  86. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  87. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  88. initWithReference:ref
  89. fetcherService:self.fetcherService
  90. dispatchQueue:self.dispatchQueue
  91. metadata:self.metadata
  92. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  93. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  94. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  95. XCTAssertNil(error);
  96. [expectation fulfill];
  97. }];
  98. [task enqueue];
  99. [FIRStorageTestHelpers waitForExpectation:self];
  100. }
  101. - (void)testUnsuccessfulFetchUnauthenticated {
  102. XCTestExpectation *expectation =
  103. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  104. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  105. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  106. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  107. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  108. initWithReference:ref
  109. fetcherService:self.fetcherService
  110. dispatchQueue:self.dispatchQueue
  111. metadata:self.metadata
  112. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  113. XCTAssertNil(metadata);
  114. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  115. [expectation fulfill];
  116. }];
  117. [task enqueue];
  118. [FIRStorageTestHelpers waitForExpectation:self];
  119. }
  120. - (void)testUnsuccessfulFetchUnauthorized {
  121. XCTestExpectation *expectation =
  122. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  123. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  124. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  125. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  126. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  127. initWithReference:ref
  128. fetcherService:self.fetcherService
  129. dispatchQueue:self.dispatchQueue
  130. metadata:self.metadata
  131. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  132. XCTAssertNil(metadata);
  133. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  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. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  145. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  146. initWithReference:ref
  147. fetcherService:self.fetcherService
  148. dispatchQueue:self.dispatchQueue
  149. metadata:self.metadata
  150. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  151. XCTAssertNil(metadata);
  152. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  153. [expectation fulfill];
  154. }];
  155. [task enqueue];
  156. [FIRStorageTestHelpers waitForExpectation:self];
  157. }
  158. - (void)testUnsuccessfulFetchBadJSON {
  159. XCTestExpectation *expectation =
  160. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  161. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  162. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  163. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  164. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  165. initWithReference:ref
  166. fetcherService:self.fetcherService
  167. dispatchQueue:self.dispatchQueue
  168. metadata:self.metadata
  169. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  170. XCTAssertNil(metadata);
  171. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  172. [expectation fulfill];
  173. }];
  174. [task enqueue];
  175. [FIRStorageTestHelpers waitForExpectation:self];
  176. }
  177. @end