FIRStorageUpdateMetadataTests.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "FIRStorageTestHelpers.h"
  15. #import "FIRStorageUpdateMetadataTask.h"
  16. @interface FIRStorageUpdateMetadataTests : XCTestCase
  17. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  18. @property(strong, nonatomic) FIRStorageMetadata *metadata;
  19. @property(strong, nonatomic) FIRStorage *storage;
  20. @property(strong, nonatomic) id mockApp;
  21. @end
  22. @implementation FIRStorageUpdateMetadataTests
  23. - (void)setUp {
  24. [super setUp];
  25. NSDictionary *metadataDict = @{ @"bucket" : @"bucket", @"name" : @"path/to/object" };
  26. self.metadata = [[FIRStorageMetadata alloc] initWithDictionary:metadataDict];
  27. id mockOptions = OCMClassMock([FIROptions class]);
  28. OCMStub([mockOptions storageBucket]).andReturn(@"bucket.appspot.com");
  29. self.mockApp = OCMClassMock([FIRApp class]);
  30. OCMStub([self.mockApp name]).andReturn(kFIRStorageAppName);
  31. OCMStub([(FIRApp *)self.mockApp options]).andReturn(mockOptions);
  32. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  33. self.fetcherService.authorizer =
  34. [[FIRStorageTokenAuthorizer alloc] initWithApp:self.mockApp
  35. fetcherService:self.fetcherService];
  36. self.storage = [FIRStorage storageForApp:self.mockApp];
  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 = ^(GTMSessionFetcher *fetcher,
  47. 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 dictionaryRepresentation]];
  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 =
  59. [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  60. statusCode:200
  61. HTTPVersion:kHTTPVersion
  62. headerFields:nil];
  63. response(httpResponse, nil, nil);
  64. };
  65. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  66. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  67. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  68. initWithReference:ref
  69. fetcherService:self.fetcherService
  70. metadata:self.metadata
  71. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  72. [expectation fulfill];
  73. }];
  74. [task enqueue];
  75. [FIRStorageTestHelpers waitForExpectation:self];
  76. }
  77. - (void)testSuccessfulFetch {
  78. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  79. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  80. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  81. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  82. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  83. initWithReference:ref
  84. fetcherService:self.fetcherService
  85. metadata:self.metadata
  86. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  87. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  88. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  89. XCTAssertNil(error);
  90. [expectation fulfill];
  91. }];
  92. [task enqueue];
  93. [FIRStorageTestHelpers waitForExpectation:self];
  94. }
  95. - (void)testUnsuccessfulFetchUnauthenticated {
  96. XCTestExpectation *expectation =
  97. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  98. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  99. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  100. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  101. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  102. initWithReference:ref
  103. fetcherService:self.fetcherService
  104. metadata:self.metadata
  105. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  106. XCTAssertNil(metadata);
  107. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  108. [expectation fulfill];
  109. }];
  110. [task enqueue];
  111. [FIRStorageTestHelpers waitForExpectation:self];
  112. }
  113. - (void)testUnsuccessfulFetchUnauthorized {
  114. XCTestExpectation *expectation =
  115. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  116. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  117. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  118. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  119. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  120. initWithReference:ref
  121. fetcherService:self.fetcherService
  122. metadata:self.metadata
  123. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  124. XCTAssertNil(metadata);
  125. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  126. [expectation fulfill];
  127. }];
  128. [task enqueue];
  129. [FIRStorageTestHelpers waitForExpectation:self];
  130. }
  131. - (void)testUnsuccessfulFetchObjectDoesntExist {
  132. XCTestExpectation *expectation =
  133. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  134. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  135. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  136. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  137. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  138. initWithReference:ref
  139. fetcherService:self.fetcherService
  140. metadata:self.metadata
  141. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  142. XCTAssertNil(metadata);
  143. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  144. [expectation fulfill];
  145. }];
  146. [task enqueue];
  147. [FIRStorageTestHelpers waitForExpectation:self];
  148. }
  149. - (void)testUnsuccessfulFetchBadJSON {
  150. XCTestExpectation *expectation =
  151. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  152. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  153. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  154. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  155. FIRStorageUpdateMetadataTask *task = [[FIRStorageUpdateMetadataTask alloc]
  156. initWithReference:ref
  157. fetcherService:self.fetcherService
  158. metadata:self.metadata
  159. completion:^(FIRStorageMetadata *_Nullable metadata, NSError *_Nullable error) {
  160. XCTAssertNil(metadata);
  161. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  162. [expectation fulfill];
  163. }];
  164. [task enqueue];
  165. [FIRStorageTestHelpers waitForExpectation:self];
  166. }
  167. @end