FIRStorageDeleteTests.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "FIRStorageDeleteTask.h"
  15. #import "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. self.dispatchQueue = dispatch_queue_create("Test dispatch queue", DISPATCH_QUEUE_SERIAL);
  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:@"testFetcherConfiguration"];
  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, @"DELETE");
  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. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  62. fetcherService:self.fetcherService
  63. dispatchQueue:self.dispatchQueue
  64. completion:^(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 successBlock];
  73. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  74. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  75. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  76. fetcherService:self.fetcherService
  77. dispatchQueue:self.dispatchQueue
  78. completion:^(NSError *error) {
  79. XCTAssertEqual(error, nil);
  80. [expectation fulfill];
  81. }];
  82. [task enqueue];
  83. [FIRStorageTestHelpers waitForExpectation:self];
  84. }
  85. - (void)testUnsuccessfulFetchUnauthenticated {
  86. XCTestExpectation *expectation =
  87. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  88. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  89. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  90. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  91. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  92. initWithReference:ref
  93. fetcherService:self.fetcherService
  94. dispatchQueue:self.dispatchQueue
  95. completion:^(NSError *error) {
  96. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  97. [expectation fulfill];
  98. }];
  99. [task enqueue];
  100. [FIRStorageTestHelpers waitForExpectation:self];
  101. }
  102. - (void)testUnsuccessfulFetchUnauthorized {
  103. XCTestExpectation *expectation =
  104. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  105. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  106. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  107. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  108. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  109. initWithReference:ref
  110. fetcherService:self.fetcherService
  111. dispatchQueue:self.dispatchQueue
  112. completion:^(NSError *error) {
  113. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthorized);
  114. [expectation fulfill];
  115. }];
  116. [task enqueue];
  117. [FIRStorageTestHelpers waitForExpectation:self];
  118. }
  119. - (void)testUnsuccessfulFetchObjectDoesntExist {
  120. XCTestExpectation *expectation =
  121. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  122. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  123. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  124. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  125. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc]
  126. initWithReference:ref
  127. fetcherService:self.fetcherService
  128. dispatchQueue:self.dispatchQueue
  129. completion:^(NSError *error) {
  130. XCTAssertEqual(error.code, FIRStorageErrorCodeObjectNotFound);
  131. [expectation fulfill];
  132. }];
  133. [task enqueue];
  134. [FIRStorageTestHelpers waitForExpectation:self];
  135. }
  136. @end