@@ -32,6 +32,12 @@ class ContextProviderTests: XCTestCase {
3232 code: - 1 ,
3333 userInfo: nil
3434 ) )
35+ let appCheckLimitedUseTokenError = FIRAppCheckTokenResultFake ( token: " limited use token " ,
36+ error: NSError (
37+ domain: " testAppCheckError " ,
38+ code: - 1 ,
39+ userInfo: nil
40+ ) )
3541 let appCheckTokenSuccess = FIRAppCheckTokenResultFake ( token: " valid_token " , error: nil )
3642 let messagingFake = FIRMessagingInteropFake ( )
3743
@@ -143,8 +149,20 @@ class ContextProviderTests: XCTestCase {
143149
144150 XCTAssertNil ( context. authToken)
145151 XCTAssertNil ( context. fcmToken)
146- // Don't expect any token in the case of App Check error.
147- XCTAssertNil ( context. appCheckToken)
152+ // Expect placeholder token in the case of App Check error.
153+ XCTAssertEqual ( context. appCheckToken, appCheckFake. tokenResult. token)
154+ }
155+
156+ func testAsyncContextWithAppCheckOnlyError_LimitedUseToken( ) async throws {
157+ appCheckFake. limitedUseTokenResult = appCheckLimitedUseTokenError
158+ let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheckFake)
159+
160+ let context = try await provider. context ( options: . init( requireLimitedUseAppCheckTokens: true ) )
161+
162+ XCTAssertNil ( context. authToken)
163+ XCTAssertNil ( context. fcmToken)
164+ // Expect placeholder token in the case of App Check error.
165+ XCTAssertEqual ( context. limitedUseAppCheckToken, appCheckFake. limitedUseTokenResult. token)
148166 }
149167
150168 func testContextWithAppCheckOnlyError( ) {
@@ -156,8 +174,24 @@ class ContextProviderTests: XCTestCase {
156174 XCTAssertNil ( error)
157175 XCTAssertNil ( context. authToken)
158176 XCTAssertNil ( context. fcmToken)
159- // Don't expect any token in the case of App Check error.
160- XCTAssertNil ( context. appCheckToken)
177+ // Expect placeholder token in the case of App Check error.
178+ XCTAssertEqual ( context. appCheckToken, self . appCheckFake. tokenResult. token)
179+ expectation. fulfill ( )
180+ }
181+ waitForExpectations ( timeout: 0.1 )
182+ }
183+
184+ func testContextWithAppCheckOnlyError_LimitedUseToken( ) {
185+ appCheckFake. limitedUseTokenResult = appCheckLimitedUseTokenError
186+ let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheckFake)
187+ let expectation = expectation ( description: " Verify bad app check token " )
188+ provider. getContext ( options: . init( requireLimitedUseAppCheckTokens: true ) ) { context, error in
189+ XCTAssertNotNil ( context)
190+ XCTAssertNil ( error)
191+ XCTAssertNil ( context. authToken)
192+ XCTAssertNil ( context. fcmToken)
193+ // Expect placeholder token in the case of App Check error.
194+ XCTAssertEqual ( context. limitedUseAppCheckToken, self . appCheckFake. limitedUseTokenResult. token)
161195 expectation. fulfill ( )
162196 }
163197 waitForExpectations ( timeout: 0.1 )
@@ -264,8 +298,30 @@ class ContextProviderTests: XCTestCase {
264298 XCTAssertEqual ( error as NSError ? , authError)
265299 XCTAssertNil ( context. authToken)
266300 XCTAssertEqual ( context. fcmToken, self . messagingFake. fcmToken)
267- // Don't expect any token in the case of App Check error.
268- XCTAssertNil ( context. appCheckToken)
301+ // Expect placeholder token in the case of App Check error.
302+ XCTAssertEqual ( context. appCheckToken, self . appCheckFake. tokenResult. token)
303+ expectation. fulfill ( )
304+ }
305+ waitForExpectations ( timeout: 0.1 )
306+ }
307+
308+ func testAllContextsAuthAndAppCheckError_LimitedUseToken( ) {
309+ appCheckFake. limitedUseTokenResult = appCheckLimitedUseTokenError
310+ let authError = NSError ( domain: " com.functions.tests " , code: 4 , userInfo: nil )
311+ let auth = FIRAuthInteropFake ( token: nil , userID: " userID " , error: authError)
312+ let provider = FunctionsContextProvider (
313+ auth: auth,
314+ messaging: messagingFake,
315+ appCheck: appCheckFake
316+ )
317+ let expectation = expectation ( description: " All contexts with errors " )
318+ provider. getContext ( options: . init( requireLimitedUseAppCheckTokens: true ) ) { context, error in
319+ XCTAssertNotNil ( context)
320+ XCTAssertEqual ( error as NSError ? , authError)
321+ XCTAssertNil ( context. authToken)
322+ XCTAssertEqual ( context. fcmToken, self . messagingFake. fcmToken)
323+ // Expect placeholder token in the case of App Check error.
324+ XCTAssertEqual ( context. limitedUseAppCheckToken, self . appCheckFake. limitedUseTokenResult. token)
269325 expectation. fulfill ( )
270326 }
271327 waitForExpectations ( timeout: 0.1 )
0 commit comments