aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Storage/Tests/Unit/FIRStorageDeleteTests.m
blob: 42a3b1a528595b32b4a0febbbcc6a8833e66f1fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "FIRStorageDeleteTask.h"
#import "FIRStorageTestHelpers.h"

@interface FIRStorageDeleteTests : XCTestCase

@property(strong, nonatomic) GTMSessionFetcherService *fetcherService;
@property(strong, nonatomic) FIRStorageMetadata *metadata;
@property(strong, nonatomic) FIRStorage *storage;
@property(strong, nonatomic) id mockApp;

@end

@implementation FIRStorageDeleteTests

- (void)setUp {
  [super setUp];

  NSDictionary *metadataDict = @{ @"bucket" : @"bucket", @"name" : @"path/to/object" };
  self.metadata = [[FIRStorageMetadata alloc] initWithDictionary:metadataDict];

  id mockOptions = OCMClassMock([FIROptions class]);
  OCMStub([mockOptions storageBucket]).andReturn(@"bucket.appspot.com");

  self.mockApp = OCMClassMock([FIRApp class]);
  OCMStub([self.mockApp name]).andReturn(kFIRStorageAppName);
  OCMStub([(FIRApp *)self.mockApp options]).andReturn(mockOptions);

  self.fetcherService = [[GTMSessionFetcherService alloc] init];
  self.fetcherService.authorizer =
      [[FIRStorageTokenAuthorizer alloc] initWithApp:self.mockApp
                                      fetcherService:self.fetcherService];

  self.storage = [FIRStorage storageForApp:self.mockApp];
}

- (void)tearDown {
  self.fetcherService = nil;
  self.storage = nil;
  self.mockApp = nil;
  [super tearDown];
}

- (void)testFetcherConfiguration {
  XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];

  self.fetcherService.testBlock =
      ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
        XCTAssertEqualObjects(fetcher.request.URL, [FIRStorageTestHelpers objectURL]);
#pragma clang diagnostic pop
        XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"DELETE");
        NSHTTPURLResponse *httpResponse =
            [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
                                        statusCode:200
                                       HTTPVersion:kHTTPVersion
                                      headerFields:nil];
        response(httpResponse, nil, nil);
      };

  FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
                                                                fetcherService:self.fetcherService
                                                                    completion:^(NSError *error) {
                                                                      [expectation fulfill];
                                                                    }];
  [task enqueue];

  [FIRStorageTestHelpers waitForExpectation:self];
}

- (void)testSuccessfulFetch {
  XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulFetch"];

  self.fetcherService.testBlock = [FIRStorageTestHelpers successBlock];
  FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  FIRStorageDeleteTask *task = [[FIRStorageDeleteTask alloc] initWithReference:ref
                                                                fetcherService:self.fetcherService
                                                                    completion:^(NSError *error) {
                                                                      XCTAssertEqual(error, nil);
                                                                      [expectation fulfill];
                                                                    }];
  [task enqueue];

  [FIRStorageTestHelpers waitForExpectation:self];
}

- (void)testUnsuccessfulFetchUnauthenticated {
  XCTestExpectation *expectation =
      [self expectationWithDescription:@"testUnsuccessfulFetchUnauthenticated"];

  self.fetcherService.testBlock = [FIRStorageTestHelpers unauthenticatedBlock];
  FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  FIRStorageDeleteTask *task =
      [[FIRStorageDeleteTask alloc] initWithReference:ref
                                       fetcherService:self.fetcherService
                                           completion:^(NSError *error) {
                                             XCTAssertEqual(error.code,
                                                            FIRStorageErrorCodeUnauthenticated);
                                             [expectation fulfill];
                                           }];
  [task enqueue];

  [FIRStorageTestHelpers waitForExpectation:self];
}

- (void)testUnsuccessfulFetchUnauthorized {
  XCTestExpectation *expectation =
      [self expectationWithDescription:@"testUnsuccessfulFetchUnauthorized"];

  self.fetcherService.testBlock = [FIRStorageTestHelpers unauthorizedBlock];
  FIRStoragePath *path = [FIRStorageTestHelpers objectPath];
  FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  FIRStorageDeleteTask *task =
      [[FIRStorageDeleteTask alloc] initWithReference:ref
                                       fetcherService:self.fetcherService
                                           completion:^(NSError *error) {
                                             XCTAssertEqual(error.code,
                                                            FIRStorageErrorCodeUnauthorized);
                                             [expectation fulfill];
                                           }];
  [task enqueue];

  [FIRStorageTestHelpers waitForExpectation:self];
}

- (void)testUnsuccessfulFetchObjectDoesntExist {
  XCTestExpectation *expectation =
      [self expectationWithDescription:@"testUnsuccessfulFetchObjectDoesntExist"];

  self.fetcherService.testBlock = [FIRStorageTestHelpers notFoundBlock];
  FIRStoragePath *path = [FIRStorageTestHelpers notFoundPath];
  FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
  FIRStorageDeleteTask *task =
      [[FIRStorageDeleteTask alloc] initWithReference:ref
                                       fetcherService:self.fetcherService
                                           completion:^(NSError *error) {
                                             XCTAssertEqual(error.code,
                                                            FIRStorageErrorCodeObjectNotFound);
                                             [expectation fulfill];
                                           }];
  [task enqueue];

  [FIRStorageTestHelpers waitForExpectation:self];
}

@end