FIRStorageDeleteTests.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(strong, nonatomic) FIRStorageMetadata *metadata;
  19. @property(strong, nonatomic) FIRStorage *storage;
  20. @property(strong, nonatomic) id mockApp;
  21. @end
  22. @implementation FIRStorageDeleteTests
  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, @"DELETE");
  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. FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
  63. fetcherService:self.fetcherService
  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. completion:^(NSError *error) {
  78. XCTAssertEqual(error, nil);
  79. [expectation fulfill];
  80. }];
  81. [task enqueue];
  82. [FIRStorageTestHelpers waitForExpectation:self];
  83. }
  84. - (void)testUnsuccessfulFetchUnauthenticated {
  85. XCTestExpectation *expectation =
  86. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];
  87. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  88. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  89. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  90. FIRStorageDeleteTask *task =
  91. [[FIRStorageDeleteTask alloc] initWithReference:ref
  92. fetcherService:self.fetcherService
  93. completion:^(NSError *error) {
  94. XCTAssertEqual(error.code,
  95. FIRStorageErrorCodeUnauthenticated);
  96. [expectation fulfill];
  97. }];
  98. [task enqueue];
  99. [FIRStorageTestHelpers waitForExpectation:self];
  100. }
  101. - (void)testUnsuccessfulFetchUnauthorized {
  102. XCTestExpectation *expectation =
  103. [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];
  104. self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  105. FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  106. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  107. FIRStorageDeleteTask *task =
  108. [[FIRStorageDeleteTask alloc] initWithReference:ref
  109. fetcherService:self.fetcherService
  110. completion:^(NSError *error) {
  111. XCTAssertEqual(error.code,
  112. FIRStorageErrorCodeUnauthorized);
  113. [expectation fulfill];
  114. }];
  115. [task enqueue];
  116. [FIRStorageTestHelpers waitForExpectation:self];
  117. }
  118. - (void)testUnsuccessfulFetchObjectDoesntExist {
  119. XCTestExpectation *expectation =
  120. [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];
  121. self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  122. FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  123. FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  124. FIRStorageDeleteTask *task =
  125. [[FIRStorageDeleteTask alloc] initWithReference:ref
  126. fetcherService:self.fetcherService
  127. completion:^(NSError *error) {
  128. XCTAssertEqual(error.code,
  129. FIRStorageErrorCodeObjectNotFound);
  130. [expectation fulfill];
  131. }];
  132. [task enqueue];
  133. [FIRStorageTestHelpers waitForExpectation:self];
  134. }
  135. @end