17
17
#import < XCTest/XCTest.h>
18
18
19
19
#import " FirebaseAppDistribution/Sources/FIRFADApiService.h"
20
+ #import " FirebaseAppDistribution/Sources/FIRFADLogger.h"
20
21
#import " FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
21
22
#import " FirebaseInstallations/Source/Library/Private/FirebaseInstallationsInternal.h"
22
23
25
26
@interface FIRFADApiServiceTests : XCTestCase
26
27
@end
27
28
29
+ @interface FIRFADApiService (PrivateUnitTesting)
30
+
31
+ + (NSString *)tryParseGoogleAPIErrorFromResponse : (NSData *)data ;
32
+
33
+ @end
34
+
28
35
@implementation FIRFADApiServiceTests {
29
36
id _mockFIRAppClass;
30
37
id _mockURLSession;
31
38
id _mockFIRInstallations;
32
39
id _mockInstallationToken;
33
40
NSString *_mockAuthToken;
34
41
NSString *_mockInstallationId;
42
+ NSString *_mockAPINotEnabledMessage;
35
43
NSDictionary *_mockReleases;
44
+ NSDictionary *_mockAPINotEnabledResponse;
36
45
}
37
46
38
47
- (void )setUp {
@@ -65,6 +74,24 @@ - (void)setUp {
65
74
}
66
75
]
67
76
};
77
+
78
+ _mockAPINotEnabledMessage =
79
+ @" This is a long message about what's happening. This is a fake message from the Firebase "
80
+ @" App Testers API in project 123456789. This should be logged." ;
81
+ _mockAPINotEnabledResponse = @{
82
+ @" error" : @{
83
+ @" code" : @403 ,
84
+ @" message" : _mockAPINotEnabledMessage,
85
+ @" status" : @" PERMISSION_DENIED" ,
86
+ @" details" : @[ @{
87
+ @" type" : @" type.fakeapis.com/appdistro.api.Help" ,
88
+ @" links" : @[ @{
89
+ @" description" : @" this is a short statement about enabling the api" ,
90
+ @" url" : @" this should be a link"
91
+ } ],
92
+ } ],
93
+ }
94
+ };
68
95
}
69
96
70
97
- (void )tearDown {
@@ -140,6 +167,37 @@ - (void)rejectUrlSessionResponseWithData {
140
167
completionHandler: [OCMArg isNotNil ]]);
141
168
}
142
169
170
+ - (void )testTryParseGoogleAPIErrorFromResponseSuccess {
171
+ NSData *data = [NSJSONSerialization dataWithJSONObject: _mockAPINotEnabledResponse
172
+ options: 0
173
+ error: nil ];
174
+ NSString *message = [FIRFADApiService tryParseGoogleAPIErrorFromResponse: data];
175
+ XCTAssertTrue ([message isEqualToString: _mockAPINotEnabledMessage]);
176
+ }
177
+
178
+ - (void )testTryParseGoogleAPIErrorFromResponseParseFailure {
179
+ NSData *data = [@" malformed{json[data" dataUsingEncoding: NSUTF8StringEncoding];
180
+ NSString *message = [FIRFADApiService tryParseGoogleAPIErrorFromResponse: data];
181
+ XCTAssertTrue (
182
+ [message isEqualToString: @" Could not parse additional details about this API error." ]);
183
+ }
184
+
185
+ - (void )testTryParseGoogleAPIErrorFromResponseNoErrorFailure {
186
+ NSDictionary *errorDictionary = @{@" message" : @" This has no subdict" };
187
+ NSData *data = [NSJSONSerialization dataWithJSONObject: errorDictionary options: 0 error: nil ];
188
+ NSString *message = [FIRFADApiService tryParseGoogleAPIErrorFromResponse: data];
189
+ XCTAssertTrue (
190
+ [message isEqualToString: @" Could not parse additional details about this API error." ]);
191
+ }
192
+
193
+ - (void )testTryParseGoogleAPIErrorFromResponseNoMessageFailure {
194
+ NSDictionary *errorDictionary = @{@" error" : @{@" status" : @" This has no message" }};
195
+ NSData *data = [NSJSONSerialization dataWithJSONObject: errorDictionary options: 0 error: nil ];
196
+ NSString *message = [FIRFADApiService tryParseGoogleAPIErrorFromResponse: data];
197
+ XCTAssertTrue (
198
+ [message isEqualToString: @" Could not parse additional details about this API error." ]);
199
+ }
200
+
143
201
- (void )testGenerateAuthTokenWithCompletionSuccess {
144
202
[self mockInstallationAuthCompletion: _mockInstallationToken error: nil ];
145
203
[self mockInstallationIdCompletion: _mockInstallationId error: nil ];
@@ -153,8 +211,8 @@ - (void)testGenerateAuthTokenWithCompletionSuccess {
153
211
XCTAssertNotNil (authTokenResult);
154
212
XCTAssertNotNil (identifier);
155
213
XCTAssertNil (error);
156
- XCTAssertEqual ( identifier, self->_mockInstallationId );
157
- XCTAssertEqual ([ authTokenResult authToken ], self -> _mockAuthToken);
214
+ XCTAssertTrue ([ identifier isEqualToString: self ->_mockInstallationId] );
215
+ XCTAssertTrue ([[ authTokenResult authToken ] isEqualToString: self -> _mockAuthToken] );
158
216
[expectation fulfill ];
159
217
}];
160
218
@@ -318,7 +376,7 @@ - (void)testFetchReleasesWithCompletionUnauthorized403Failure {
318
376
OCMStub ([fakeResponse statusCode ]).andReturn (403 );
319
377
[self mockInstallationAuthCompletion: _mockInstallationToken error: nil ];
320
378
[self mockInstallationIdCompletion: _mockInstallationId error: nil ];
321
- [self mockUrlSessionResponse: _mockReleases response: fakeResponse error: nil ];
379
+ [self mockUrlSessionResponse: _mockAPINotEnabledResponse response: fakeResponse error: nil ];
322
380
XCTestExpectation *expectation =
323
381
[self expectationWithDescription: @" Fetch releases rejects with a 403." ];
324
382
0 commit comments