FIRStorageGetMetadataTests.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/FIRStorageGetMetadataTask.h"
  15. #import "FirebaseStorage/Tests/Unit/FIRStorageTestHelpers.h"
  16. @interface FIRStorageGetMetadataTests : XCTestCase
  17. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  18. @property(nonatomic) dispatch_queue_t dispatchQueue;
  19. @property(strong, nonatomic) FIRStorageMetadata *metadata;
  20. @property(strong, nonatomic) FIRStorage *storage;
  21. @property(strong, nonatomic) id mockApp;
  22. @end
  23. @implementation FIRStorageGetMetadataTests
  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 = [FIRStorageTestHelpers mockedApp];
  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] initWithGoogleAppID:@"dummyAppID"
  36. fetcherService:self.fetcherService
  37. authProvider:nil
  38. appCheck: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. #pragma clang diagnostic pop
  56. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  57. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  58. statusCode:200
  59. HTTPVersion:kHTTPVersion
  60. headerFields:nil];
  61. response(httpResponse, nil, nil);
  62. };
  63. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  64. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  65. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  66. initWithReference:ref
  67. fetcherService:self.fetcherService
  68. dispatchQueue:self.dispatchQueue
  69. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  70. [expectation fulfill];
  71. }];
  72. [task enqueue];
  73. [FIRStorageTestHelpers waitForExpectation:self];
  74. }
  75. - (void)testSuccessfulFetch {
  76. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  77. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  78. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  79. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  80. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  81. initWithReference:ref
  82. fetcherService:self.fetcherService
  83. dispatchQueue:self.dispatchQueue
  84. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  85. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  86. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  87. XCTAssertEqual(error, nil);
  88. [expectation fulfill];
  89. }];
  90. [task enqueue];
  91. [FIRStorageTestHelpers waitForExpectation:self];
  92. }
  93. - (void)testSuccessfulFetchWithEmulator {
  94. XCTestExpectation *expectation =
  95. [self expectationWithDescription:@"testSuccessfulFetchWithEmulator"];
  96. [self.storage useEmulatorWithHost:@"localhost" port:8080];
  97. self.fetcherService.allowLocalhostRequest = YES;
  98. self.fetcherService.testBlock =
  99. [FIRStorageTestHelpers successBlockWithURL:@"http://localhost:8080/v0/b/bucket/o/object"];
  100. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  101. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  102. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  103. initWithReference:ref
  104. fetcherService:self.fetcherService
  105. dispatchQueue:self.dispatchQueue
  106. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  107. XCTAssertNil(error);
  108. [expectation fulfill];
  109. }];
  110. [task enqueue];
  111. [FIRStorageTestHelpers waitForExpectation:self];
  112. }
  113. - (void)testUnsuccessfulFetchUnauthenticated {
  114. XCTestExpectation *expectation =
  115. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  116. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  117. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  118. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  119. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  120. initWithReference:ref
  121. fetcherService:self.fetcherService
  122. dispatchQueue:self.dispatchQueue
  123. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  124. XCTAssertEqual(metadata, nil);
  125. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  126. [expectation fulfill];
  127. }];
  128. [task enqueue];
  129. [FIRStorageTestHelpers waitForExpectation:self];
  130. }
  131. - (void)testUnsuccessfulFetchUnauthorized {
  132. XCTestExpectation *expectation =
  133. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  134. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  135. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  136. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  137. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  138. initWithReference:ref
  139. fetcherService:self.fetcherService
  140. dispatchQueue:self.dispatchQueue
  141. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  142. XCTAssertEqual(metadata, nil);
  143. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  144. [expectation fulfill];
  145. }];
  146. [task enqueue];
  147. [FIRStorageTestHelpers waitForExpectation:self];
  148. }
  149. - (void)testUnsuccessfulFetchObjectDoesntExist {
  150. XCTestExpectation *expectation =
  151. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  152. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  153. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  154. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  155. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  156. initWithReference:ref
  157. fetcherService:self.fetcherService
  158. dispatchQueue:self.dispatchQueue
  159. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  160. XCTAssertEqual(metadata, nil);
  161. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  162. [expectation fulfill];
  163. }];
  164. [task enqueue];
  165. [FIRStorageTestHelpers waitForExpectation:self];
  166. }
  167. - (void)testUnsuccessfulFetchBadJSON {
  168. XCTestExpectation *expectation =
  169. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  170. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  171. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  172. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  173. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  174. initWithReference:ref
  175. fetcherService:self.fetcherService
  176. dispatchQueue:self.dispatchQueue
  177. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  178. XCTAssertEqual(metadata, nil);
  179. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  180. [expectation fulfill];
  181. }];
  182. [task enqueue];
  183. [FIRStorageTestHelpers waitForExpectation:self];
  184. }
  185. @end