@@ -35,6 +35,17 @@ class ContextProviderTests: XCTestCase {
35
35
let appCheckTokenSuccess = FIRAppCheckTokenResultFake ( token: " valid_token " , error: nil )
36
36
let messagingFake = FIRMessagingInteropFake ( )
37
37
38
+ func testAsyncContextWithAuth( ) async throws {
39
+ let auth = FIRAuthInteropFake ( token: " token " , userID: " userID " , error: nil )
40
+ let provider = FunctionsContextProvider ( auth: auth, messaging: messagingFake, appCheck: nil )
41
+
42
+ let context = try await provider. context ( options: nil )
43
+
44
+ XCTAssertNotNil ( context)
45
+ XCTAssertEqual ( context. authToken, " token " )
46
+ XCTAssertEqual ( context. fcmToken, messagingFake. fcmToken)
47
+ }
48
+
38
49
func testContextWithAuth( ) {
39
50
let auth = FIRAuthInteropFake ( token: " token " , userID: " userID " , error: nil )
40
51
let provider = FunctionsContextProvider ( auth: auth, messaging: messagingFake, appCheck: nil )
@@ -49,6 +60,19 @@ class ContextProviderTests: XCTestCase {
49
60
waitForExpectations ( timeout: 0.1 )
50
61
}
51
62
63
+ func testAsyncContextWithAuthError( ) async {
64
+ let authError = NSError ( domain: " com.functions.tests " , code: 4 , userInfo: nil )
65
+ let auth = FIRAuthInteropFake ( token: nil , userID: " userID " , error: authError)
66
+ let provider = FunctionsContextProvider ( auth: auth, messaging: messagingFake, appCheck: nil )
67
+
68
+ do {
69
+ _ = try await provider. context ( options: nil )
70
+ XCTFail ( " Expected an error " )
71
+ } catch {
72
+ XCTAssertEqual ( error as NSError , authError)
73
+ }
74
+ }
75
+
52
76
func testContextWithAuthError( ) {
53
77
let authError = NSError ( domain: " com.functions.tests " , code: 4 , userInfo: nil )
54
78
let auth = FIRAuthInteropFake ( token: nil , userID: " userID " , error: authError)
@@ -63,6 +87,15 @@ class ContextProviderTests: XCTestCase {
63
87
waitForExpectations ( timeout: 0.1 )
64
88
}
65
89
90
+ func testAsyncContextWithoutAuth( ) async throws {
91
+ let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: nil )
92
+
93
+ let context = try await provider. context ( options: nil )
94
+
95
+ XCTAssertNil ( context. authToken)
96
+ XCTAssertNil ( context. fcmToken)
97
+ }
98
+
66
99
func testContextWithoutAuth( ) {
67
100
let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: nil )
68
101
let expectation = expectation ( description: " Completion handler should succeed without Auth. " )
@@ -76,6 +109,17 @@ class ContextProviderTests: XCTestCase {
76
109
waitForExpectations ( timeout: 0.1 )
77
110
}
78
111
112
+ func testAsyncContextWithAppCheckOnlySuccess( ) async throws {
113
+ appCheckFake. tokenResult = appCheckTokenSuccess
114
+ let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheckFake)
115
+
116
+ let context = try await provider. context ( options: nil )
117
+
118
+ XCTAssertNil ( context. authToken)
119
+ XCTAssertNil ( context. fcmToken)
120
+ XCTAssertEqual ( context. appCheckToken, appCheckTokenSuccess. token)
121
+ }
122
+
79
123
func testContextWithAppCheckOnlySuccess( ) {
80
124
appCheckFake. tokenResult = appCheckTokenSuccess
81
125
let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheckFake)
@@ -91,6 +135,18 @@ class ContextProviderTests: XCTestCase {
91
135
waitForExpectations ( timeout: 0.1 )
92
136
}
93
137
138
+ func testAsyncContextWithAppCheckOnlyError( ) async throws {
139
+ appCheckFake. tokenResult = appCheckTokenError
140
+ let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheckFake)
141
+
142
+ let context = try await provider. context ( options: nil )
143
+
144
+ XCTAssertNil ( context. authToken)
145
+ XCTAssertNil ( context. fcmToken)
146
+ // Don't expect any token in the case of App Check error.
147
+ XCTAssertNil ( context. appCheckToken)
148
+ }
149
+
94
150
func testContextWithAppCheckOnlyError( ) {
95
151
appCheckFake. tokenResult = appCheckTokenError
96
152
let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheckFake)
@@ -107,6 +163,19 @@ class ContextProviderTests: XCTestCase {
107
163
waitForExpectations ( timeout: 0.1 )
108
164
}
109
165
166
+ func testAsyncContextWithAppCheckWithoutOptionalMethods( ) async throws {
167
+ let appCheck = AppCheckFakeWithoutOptionalMethods ( tokenResult: appCheckTokenSuccess)
168
+ let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheck)
169
+
170
+ let context = try await provider. context ( options: . init( requireLimitedUseAppCheckTokens: true ) )
171
+
172
+ XCTAssertNil ( context. authToken)
173
+ XCTAssertNil ( context. fcmToken)
174
+ XCTAssertNil ( context. appCheckToken)
175
+ // If the method for limited-use tokens is not implemented, the value should be `nil`:
176
+ XCTAssertNil ( context. limitedUseAppCheckToken)
177
+ }
178
+
110
179
func testContextWithAppCheckWithoutOptionalMethods( ) {
111
180
let appCheck = AppCheckFakeWithoutOptionalMethods ( tokenResult: appCheckTokenSuccess)
112
181
let provider = FunctionsContextProvider ( auth: nil , messaging: nil , appCheck: appCheck)
@@ -126,6 +195,22 @@ class ContextProviderTests: XCTestCase {
126
195
waitForExpectations ( timeout: 0.1 )
127
196
}
128
197
198
+ func testAsyncAllContextsAvailableSuccess( ) async throws {
199
+ appCheckFake. tokenResult = appCheckTokenSuccess
200
+ let auth = FIRAuthInteropFake ( token: " token " , userID: " userID " , error: nil )
201
+ let provider = FunctionsContextProvider (
202
+ auth: auth,
203
+ messaging: messagingFake,
204
+ appCheck: appCheckFake
205
+ )
206
+
207
+ let context = try await provider. context ( options: nil )
208
+
209
+ XCTAssertEqual ( context. authToken, " token " )
210
+ XCTAssertEqual ( context. fcmToken, messagingFake. fcmToken)
211
+ XCTAssertEqual ( context. appCheckToken, appCheckTokenSuccess. token)
212
+ }
213
+
129
214
func testAllContextsAvailableSuccess( ) {
130
215
appCheckFake. tokenResult = appCheckTokenSuccess
131
216
let auth = FIRAuthInteropFake ( token: " token " , userID: " userID " , error: nil )
@@ -146,6 +231,24 @@ class ContextProviderTests: XCTestCase {
146
231
waitForExpectations ( timeout: 0.1 )
147
232
}
148
233
234
+ func testAsyncAllContextsAuthAndAppCheckError( ) async {
235
+ appCheckFake. tokenResult = appCheckTokenError
236
+ let authError = NSError ( domain: " com.functions.tests " , code: 4 , userInfo: nil )
237
+ let auth = FIRAuthInteropFake ( token: nil , userID: " userID " , error: authError)
238
+ let provider = FunctionsContextProvider (
239
+ auth: auth,
240
+ messaging: messagingFake,
241
+ appCheck: appCheckFake
242
+ )
243
+
244
+ do {
245
+ _ = try await provider. context ( options: nil )
246
+ XCTFail ( " Expected an error " )
247
+ } catch {
248
+ XCTAssertEqual ( error as NSError , authError)
249
+ }
250
+ }
251
+
149
252
func testAllContextsAuthAndAppCheckError( ) {
150
253
appCheckFake. tokenResult = appCheckTokenError
151
254
let authError = NSError ( domain: " com.functions.tests " , code: 4 , userInfo: nil )
0 commit comments