FIRStorageDeleteTests.m 6.7 KB

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