FIRStorageGetMetadataTests.m 7.5 KB

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