FIRStorageDeleteTests.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/FIRStorageDeleteTask.h"
  15. #import "FirebaseStorage/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) FIRStorage *storage;
  20. @property(strong, nonatomic) id mockApp;
  21. @end
  22. @implementation FIRStorageDeleteTests
  23. - (void)setUp {
  24. [super setUp];
  25. id mockOptions = OCMClassMock([FIROptions class]);
  26. OCMStub([mockOptions storageBucket]).andReturn(@"bucket.appspot.com");
  27. self.mockApp = [FIRStorageTestHelpers mockedApp];
  28. OCMStub([self.mockApp name]).andReturn(kFIRStorageAppName);
  29. OCMStub([(FIRApp *)self.mockApp options]).andReturn(mockOptions);
  30. self.fetcherService = [[GTMSessionFetcherService alloc] init];
  31. self.fetcherService.authorizer =
  32. [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  33. fetcherService:self.fetcherService
  34. authProvider:nil
  35. appCheck:nil];
  36. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  37. self.storage = [FIRStorage storageForApp:self.mockApp];
  38. }
  39. - (void)tearDown {
  40. self.fetcherService = nil;
  41. self.storage = nil;
  42. self.mockApp = nil;
  43. [super tearDown];
  44. }
  45. - (void)testFetcherConfiguration {
  46. XCTestExpectation *expectation = [self expectationWithDescription:@"testFetcherConfiguration"];
  47. self.fetcherService.testBlock =
  48. ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  49. #pragma clang diagnostic push
  50. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  51. XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
  52. #pragma clang diagnostic pop
  53. XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"DELETE");
  54. NSHTTPURLResponse *httpResponse = [[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. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  63. fetcherService:self.fetcherService
  64. dispatchQueue:self.dispatchQueue
  65. completion:^(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 successBlock];
  74. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  75. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  76. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  77. fetcherService:self.fetcherService
  78. dispatchQueue:self.dispatchQueue
  79. completion:^(NSError *error) {
  80. XCTAssertEqual(error, nil);
  81. [expectation fulfill];
  82. }];
  83. [task enqueue];
  84. [FIRStorageTestHelpers waitForExpectation:self];
  85. }
  86. - (void)testSuccessfulFetchWithEmulator {
  87. XCTestExpectation *expectation =
  88. [self expectationWithDescription:@"testSuccessfulFetchWithEmulator"];
  89. [self.storage useEmulatorWithHost:@"localhost" port:8080];
  90. self.fetcherService.allowLocalhostRequest = YES;
  91. self.fetcherService.testBlock =
  92. [FIRStorageTestHelpers successBlockWithURL:@"http://localhost:8080/v0/b/bucket/o/object"];
  93. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  94. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  95. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  96. fetcherService:self.fetcherService
  97. dispatchQueue:self.dispatchQueue
  98. completion:^(NSError *error) {
  99. XCTAssertNil(error);
  100. [expectation fulfill];
  101. }];
  102. [task enqueue];
  103. [FIRStorageTestHelpers waitForExpectation:self];
  104. }
  105. - (void)testUnsuccessfulFetchUnauthenticated {
  106. XCTestExpectation *expectation =
  107. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  108. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  109. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  110. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  111. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  112. initWithReference:ref
  113. fetcherService:self.fetcherService
  114. dispatchQueue:self.dispatchQueue
  115. completion:^(NSError *error) {
  116. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  117. [expectation fulfill];
  118. }];
  119. [task enqueue];
  120. [FIRStorageTestHelpers waitForExpectation:self];
  121. }
  122. - (void)testUnsuccessfulFetchUnauthorized {
  123. XCTestExpectation *expectation =
  124. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  125. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  126. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  127. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage 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, FIRStorageErrorCodeUnauthorized);
  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. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  145. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  146. initWithReference:ref
  147. fetcherService:self.fetcherService
  148. dispatchQueue:self.dispatchQueue
  149. completion:^(NSError *error) {
  150. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  151. [expectation fulfill];
  152. }];
  153. [task enqueue];
  154. [FIRStorageTestHelpers waitForExpectation:self];
  155. }
  156. @end