FIRStorageGetMetadataTests.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "FIRStorageGetMetadataTask.h"
  15. #import "FIRStorageTestHelpers.h"
  16. @interface FIRStorageGetMetadataTests : 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 FIRStorageGetMetadataTests
  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 =
  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. #pragma clang diagnostic pop
  52. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
  53. NSHTTPURLResponse *httpResponse =
  54. [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  55. statusCode:200
  56. HTTPVersion:kHTTPVersion
  57. headerFields:nil];
  58. response(httpResponse, nil, nil);
  59. };
  60. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  61. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  62. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  63. initWithReference:ref
  64. fetcherService:self.fetcherService
  65. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  66. [expectation fulfill];
  67. }];
  68. [task enqueue];
  69. [FIRStorageTestHelpers waitForExpectation:self];
  70. }
  71. - (void)testSuccessfulFetch {
  72. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  73. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlockWithMetadata:self.metadata];
  74. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  75. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  76. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  77. initWithReference:ref
  78. fetcherService:self.fetcherService
  79. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  80. XCTAssertEqualObjects(self.metadata.bucket, metadata.bucket);
  81. XCTAssertEqualObjects(self.metadata.name, metadata.name);
  82. XCTAssertEqual(error, nil);
  83. [expectation fulfill];
  84. }];
  85. [task enqueue];
  86. [FIRStorageTestHelpers waitForExpectation:self];
  87. }
  88. - (void)testUnsuccessfulFetchUnauthenticated {
  89. XCTestExpectation *expectation =
  90. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  91. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  92. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  93. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  94. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  95. initWithReference:ref
  96. fetcherService:self.fetcherService
  97. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  98. XCTAssertEqual(metadata, nil);
  99. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  100. [expectation fulfill];
  101. }];
  102. [task enqueue];
  103. [FIRStorageTestHelpers waitForExpectation:self];
  104. }
  105. - (void)testUnsuccessfulFetchUnauthorized {
  106. XCTestExpectation *expectation =
  107. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  108. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  109. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  110. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  111. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  112. initWithReference:ref
  113. fetcherService:self.fetcherService
  114. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  115. XCTAssertEqual(metadata, nil);
  116. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  117. [expectation fulfill];
  118. }];
  119. [task enqueue];
  120. [FIRStorageTestHelpers waitForExpectation:self];
  121. }
  122. - (void)testUnsuccessfulFetchObjectDoesntExist {
  123. XCTestExpectation *expectation =
  124. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  125. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  126. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  127. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  128. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  129. initWithReference:ref
  130. fetcherService:self.fetcherService
  131. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  132. XCTAssertEqual(metadata, nil);
  133. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  134. [expectation fulfill];
  135. }];
  136. [task enqueue];
  137. [FIRStorageTestHelpers waitForExpectation:self];
  138. }
  139. - (void)testUnsuccessfulFetchBadJSON {
  140. XCTestExpectation *expectation =
  141. [self expectationWithDescription:@"testUnsuccessfulFetchBadJSON"];
  142. self.fetcherService.testBlock = [FIRStorageTestHelpers invalidJSONBlock];
  143. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  144. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  145. FIRStorageGetMetadataTask *task = [[FIRStorageGetMetadataTask alloc]
  146. initWithReference:ref
  147. fetcherService:self.fetcherService
  148. completion:^(FIRStorageMetadata *metadata, NSError *error) {
  149. XCTAssertEqual(metadata, nil);
  150. XCTAssertEqual(error.code, FIRStorageErrorCodeUnknown);
  151. [expectation fulfill];
  152. }];
  153. [task enqueue];
  154. [FIRStorageTestHelpers waitForExpectation:self];
  155. }
  156. @end