FIRStorageDeleteTests.m 8.6 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 "FirebaseStorageInternal/Sources/FIRStorageDeleteTask.h"
  15. #import "FirebaseStorageInternal/Tests/Unit/FIRStorageTestHelpers.h"
  16. @interface FIRStorageDeleteTests : XCTestCase
  17. @property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
  18. @property(nonatomic) dispatch_queue_t dispatchQueue;
  19. @property(strong, nonatomic) FIRIMPLStorage *storage;
  20. @property(strong, nonatomic) id mockApp;
  21. @end
  22. @implementation FIRStorageDeleteTests
  23. - (void)setUp {
  24. [super setUp];
  25. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  26. self.fetcherService.authorizer =
  27. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  28. fetcherService:self.fetcherService
  29. authProvider:nil
  30. appCheck:nil];
  31. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  32. self.storage = [FIRStorageTestHelpers storageWithMockedApp];
  33. }
  34. - (void)tearDown {
  35. self.fetcherService = nil;
  36. self.storage = nil;
  37. self.mockApp = nil;
  38. [super tearDown];
  39. }
  40. - (void)testFetcherConfiguration {
  41. XCTestExpectation *expectation = [self expectationWithDescription:@"testFetcherConfiguration"];
  42. self.fetcherService.testBlock =
  43. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  44. #pragma clang diagnostic push
  45. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  46. XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
  47. #pragma clang diagnostic pop
  48. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"DELETE");
  49. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  50. statusCode:200
  51. HTTPVersion:kHTTPVersion
  52. headerFields:nil];
  53. response(httpResponse, nil, nil);
  54. };
  55. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  56. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  57. path:path];
  58. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  59. fetcherService:self.fetcherService
  60. dispatchQueue:self.dispatchQueue
  61. completion:^(NSError *error) {
  62. [expectation fulfill];
  63. }];
  64. [task enqueue];
  65. [FIRStorageTestHelpers waitForExpectation:self];
  66. }
  67. - (void)testSuccessfulFetch {
  68. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];
  69. self.fetcherService.testBlock = [FIRStorageTestHelpers successBlock];
  70. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  71. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  72. path:path];
  73. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  74. fetcherService:self.fetcherService
  75. dispatchQueue:self.dispatchQueue
  76. completion:^(NSError *error) {
  77. XCTAssertEqual(error, nil);
  78. [expectation fulfill];
  79. }];
  80. [task enqueue];
  81. [FIRStorageTestHelpers waitForExpectation:self];
  82. }
  83. - (void)testSuccessfulFetchWithEmulator {
  84. XCTestExpectation *expectation =
  85. [self expectationWithDescription:@"testSuccessfulFetchWithEmulator"];
  86. [self.storage useEmulatorWithHost:@"localhost" port:8080];
  87. self.fetcherService.allowLocalhostRequest = YES;
  88. self.fetcherService.testBlock =
  89. [FIRStorageTestHelpers successBlockWithURL:@"http://localhost:8080/v0/b/bucket/o/object"];
  90. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  91. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  92. path:path];
  93. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  94. fetcherService:self.fetcherService
  95. dispatchQueue:self.dispatchQueue
  96. completion:^(NSError *error) {
  97. XCTAssertNil(error);
  98. [expectation fulfill];
  99. }];
  100. [task enqueue];
  101. [FIRStorageTestHelpers waitForExpectation:self];
  102. }
  103. - (void)testUnsuccessfulFetchUnauthenticated {
  104. XCTestExpectation *expectation =
  105. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  106. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  107. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  108. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  109. path:path];
  110. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  111. initWithReference:ref
  112. fetcherService:self.fetcherService
  113. dispatchQueue:self.dispatchQueue
  114. completion:^(NSError *error) {
  115. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnauthenticated);
  116. [expectation fulfill];
  117. }];
  118. [task enqueue];
  119. [FIRStorageTestHelpers waitForExpectation:self];
  120. }
  121. - (void)testUnsuccessfulFetchUnauthorized {
  122. XCTestExpectation *expectation =
  123. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  124. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  125. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  126. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  127. path:path];
  128. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  129. initWithReference:ref
  130. fetcherService:self.fetcherService
  131. dispatchQueue:self.dispatchQueue
  132. completion:^(NSError *error) {
  133. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeUnauthorized);
  134. [expectation fulfill];
  135. }];
  136. [task enqueue];
  137. [FIRStorageTestHelpers waitForExpectation:self];
  138. }
  139. - (void)testUnsuccessfulFetchObjectDoesntExist {
  140. XCTestExpectation *expectation =
  141. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  142. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  143. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  144. FIRIMPLStorageReference *ref = [[FIRIMPLStorageReference alloc] initWithStorage:self.storage
  145. path:path];
  146. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  147. initWithReference:ref
  148. fetcherService:self.fetcherService
  149. dispatchQueue:self.dispatchQueue
  150. completion:^(NSError *error) {
  151. XCTAssertEqual(error.code, FIRIMPLStorageErrorCodeObjectNotFound);
  152. [expectation fulfill];
  153. }];
  154. [task enqueue];
  155. [FIRStorageTestHelpers waitForExpectation:self];
  156. }
  157. @end