FIRStorageUpdateMetadataTests.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = ^(GTMSessionFetcher *fetcher,
  48. 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 =
  60. [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  61. statusCode:200
  62. HTTPVersion:kHTTPVersion
  63. headerFields:nil];
  64. response(httpResponse, nil, nil);
  65. self.fetcherService.testBlock = nil;
  66. };
  67. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  68. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  69. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  70. initWithReference:ref
  71. fetcherService:self.fetcherService
  72. metadata:self.metadata
  73. completion:^(FIRStorageMetadata *_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. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  84. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  85. initWithReference:ref
  86. fetcherService:self.fetcherService
  87. metadata:self.metadata
  88. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  89. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  90. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  91. XCTAssertNil(error);
  92. [expectation fulfill];
  93. }];
  94. [task enqueue];
  95. [FIRStorageTestHelpers waitForExpectation:self];
  96. }
  97. - (void)testUnsuccessfulFetchUnauthenticated {
  98. XCTestExpectation *expectation =
  99. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  100. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  101. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  102. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  103. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  104. initWithReference:ref
  105. fetcherService:self.fetcherService
  106. metadata:self.metadata
  107. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  108. XCTAssertNil(metadata);
  109. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  110. [expectation fulfill];
  111. }];
  112. [task enqueue];
  113. [FIRStorageTestHelpers waitForExpectation:self];
  114. }
  115. - (void)testUnsuccessfulFetchUnauthorized {
  116. XCTestExpectation *expectation =
  117. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  118. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  119. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  120. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  121. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  122. initWithReference:ref
  123. fetcherService:self.fetcherService
  124. metadata:self.metadata
  125. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  126. XCTAssertNil(metadata);
  127. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  128. [expectation fulfill];
  129. }];
  130. [task enqueue];
  131. [FIRStorageTestHelpers waitForExpectation:self];
  132. }
  133. - (void)testUnsuccessfulFetchObjectDoesntExist {
  134. XCTestExpectation *expectation =
  135. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  136. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  137. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  138. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  139. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  140. initWithReference:ref
  141. fetcherService:self.fetcherService
  142. metadata:self.metadata
  143. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  144. XCTAssertNil(metadata);
  145. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  146. [expectation fulfill];
  147. }];
  148. [task enqueue];
  149. [FIRStorageTestHelpers waitForExpectation:self];
  150. }
  151. - (void)testUnsuccessfulFetchBadJSON {
  152. XCTestExpectation *expectation =
  153. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  154. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  155. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  156. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  157. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  158. initWithReference:ref
  159. fetcherService:self.fetcherService
  160. metadata:self.metadata
  161. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  162. XCTAssertNil(metadata);
  163. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  164. [expectation fulfill];
  165. }];
  166. [task enqueue];
  167. [FIRStorageTestHelpers waitForExpectation:self];
  168. }
  169. @end