Skip to content

Commit daa83b9

Browse files
Functions: don't send dummy FAC token (#8558)
* Functions: don't send dummy FAC token tests * more tests * fix * formatting * Changelog * Fix changelog version * Cleanup Xcode 11 workaround in tests * style
1 parent 66a7f23 commit daa83b9

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

Functions/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# v8.7.0
22
- [fixed] Add watchOS support (#8499).
3+
- [changed] Don't set the App Check header in the case of App Check error (#8558).
34

45
# v8.3.0
56
- [fixed] Fixed an issue where subclassing Functions was unusable (#8265).

Functions/Example/Tests/FIRFunctionsTests.m

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ - (void)testSetEmulatorSettings {
151151

152152
#pragma mark - App Check integration
153153

154-
- (void)testCallFunctionWhenAppCheckIsInstalled {
154+
- (void)testCallFunctionWhenAppCheckIsInstalledAndFACTokenSuccess {
155155
_appCheckFake.tokenResult = [[FIRAppCheckTokenResultFake alloc] initWithToken:@"valid_token"
156156
error:nil];
157157

@@ -161,12 +161,8 @@ - (void)testCallFunctionWhenAppCheckIsInstalled {
161161

162162
XCTestExpectation *httpRequestExpectation =
163163
[self expectationWithDescription:@"HTTPRequestExpectation"];
164-
__weak __auto_type weakSelf = self;
165164
_fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
166165
GTMSessionFetcherTestResponse _Nonnull testResponse) {
167-
// Fixes retain cycle warning for Xcode 11 and earlier.
168-
// __unused to avoid warning in Xcode 12+.
169-
__unused __auto_type self = weakSelf;
170166
[httpRequestExpectation fulfill];
171167

172168
NSString *appCheckTokenHeader =
@@ -189,6 +185,39 @@ - (void)testCallFunctionWhenAppCheckIsInstalled {
189185
[self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
190186
}
191187

188+
- (void)testCallFunctionWhenAppCheckIsInstalledAndFACTokenError {
189+
NSError *appCheckError = [NSError errorWithDomain:self.name code:-1 userInfo:nil];
190+
_appCheckFake.tokenResult = [[FIRAppCheckTokenResultFake alloc] initWithToken:@"dummy_token"
191+
error:appCheckError];
192+
193+
NSError *networkError = [NSError errorWithDomain:self.name code:-2 userInfo:nil];
194+
195+
XCTestExpectation *httpRequestExpectation =
196+
[self expectationWithDescription:@"HTTPRequestExpectation"];
197+
_fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
198+
GTMSessionFetcherTestResponse _Nonnull testResponse) {
199+
[httpRequestExpectation fulfill];
200+
201+
NSString *appCheckTokenHeader =
202+
[fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
203+
XCTAssertNil(appCheckTokenHeader);
204+
205+
testResponse(nil, nil, networkError);
206+
};
207+
208+
XCTestExpectation *completionExpectation =
209+
[self expectationWithDescription:@"completionExpectation"];
210+
[_functions callFunction:@"fake_func"
211+
withObject:nil
212+
timeout:10
213+
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
214+
XCTAssertEqualObjects(error, networkError);
215+
[completionExpectation fulfill];
216+
}];
217+
218+
[self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
219+
}
220+
192221
- (void)testCallFunctionWhenAppCheckIsNotInstalled {
193222
NSError *networkError = [NSError errorWithDomain:@"testCallFunctionWhenAppCheckIsInstalled"
194223
code:-1
@@ -197,12 +226,8 @@ - (void)testCallFunctionWhenAppCheckIsNotInstalled {
197226
XCTestExpectation *httpRequestExpectation =
198227
[self expectationWithDescription:@"HTTPRequestExpectation"];
199228

200-
__weak __auto_type weakSelf = self;
201229
_fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
202230
GTMSessionFetcherTestResponse _Nonnull testResponse) {
203-
// Fixes retain cycle warning for Xcode 11 and earlier.
204-
// __unused to avoid warning in Xcode 12+.
205-
__unused __auto_type self = weakSelf;
206231
[httpRequestExpectation fulfill];
207232

208233
NSString *appCheckTokenHeader =

Functions/Example/Tests/FUNContextProviderTests.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ - (void)testContextWithAppCheckOnlyError {
132132
XCTAssertNil(error);
133133
XCTAssertNil(context.authToken);
134134
XCTAssertNil(context.FCMToken);
135-
// Expect a dummy token to be passed even in the case of app check error.
136-
XCTAssertEqualObjects(context.appCheckToken, self.appCheckTokenError.token);
135+
// Don't expect any token in the case of App Check error.
136+
XCTAssertNil(context.appCheckToken);
137137
[expectation fulfill];
138138
}];
139139

@@ -181,8 +181,8 @@ - (void)testAllContextsAuthAndAppCheckError {
181181

182182
XCTAssertNil(context.authToken);
183183
XCTAssert([context.FCMToken isEqualToString:self.messagingFake.FCMToken]);
184-
// Expect a dummy token to be passed even in the case of app check error.
185-
XCTAssertEqualObjects(context.appCheckToken, self.appCheckTokenError.token);
184+
// Don't expect any token in the case of App Check error.
185+
XCTAssertNil(context.appCheckToken);
186186
[expectation fulfill];
187187
}];
188188

Functions/FirebaseFunctions/FUNContext.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ - (void)getContext:(void (^)(FUNContext *context, NSError *_Nullable error))comp
101101

102102
[_appCheck getTokenForcingRefresh:NO
103103
completion:^(id<FIRAppCheckTokenResultInterop> _Nonnull tokenResult) {
104-
appCheckToken = tokenResult.token;
104+
// Send only valid token to functions.
105+
if (tokenResult.error == nil) {
106+
appCheckToken = tokenResult.token;
107+
}
105108

106109
dispatch_group_leave(dispatchGroup);
107110
}];

0 commit comments

Comments
 (0)