18
18
import { FirebaseError } from '@firebase/util' ;
19
19
import { expect , use } from 'chai' ;
20
20
import * as sinon from 'sinon' ;
21
+ import * as mockFetch from '../../../test/helpers/mock_fetch' ;
21
22
import sinonChai from 'sinon-chai' ;
22
23
import chaiAsPromised from 'chai-as-promised' ;
23
24
24
25
import {
25
26
regionalTestAuth ,
27
+ regionalTestAuthWithTokenRefreshHandler ,
26
28
testAuth ,
27
29
testUser
28
30
} from '../../../test/helpers/mock_auth' ;
31
+ import { RegionalEndpoint } from '../../api' ;
32
+ import { mockRegionalEndpointWithParent } from '../../../test/helpers/api/helper' ;
29
33
import { AuthInternal } from '../../model/auth' ;
30
34
import { UserInternal } from '../../model/user' ;
31
35
import { AuthInterop } from './firebase_internal' ;
@@ -227,17 +231,32 @@ describe('core/auth/firebase_internal', () => {
227
231
228
232
describe ( 'core/auth/firebase_internal - Regional Firebase Auth' , ( ) => {
229
233
let regionalAuth : AuthInternal ;
234
+ let regionalAuthWithRefreshToken : AuthInternal ;
230
235
let regionalAuthInternal : AuthInterop ;
236
+ let regionalAuthWithRefreshTokenInternal : AuthInterop ;
231
237
let now : number ;
232
238
beforeEach ( async ( ) => {
233
239
regionalAuth = await regionalTestAuth ( ) ;
234
240
regionalAuthInternal = new AuthInterop ( regionalAuth ) ;
241
+ regionalAuthWithRefreshToken =
242
+ await regionalTestAuthWithTokenRefreshHandler ( ) ;
243
+ regionalAuthWithRefreshTokenInternal = new AuthInterop (
244
+ regionalAuthWithRefreshToken
245
+ ) ;
235
246
now = Date . now ( ) ;
236
247
sinon . stub ( Date , 'now' ) . returns ( now ) ;
248
+ mockFetch . setUp ( ) ;
249
+ mockRegionalEndpointWithParent (
250
+ RegionalEndpoint . EXCHANGE_TOKEN ,
251
+ 'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config' ,
252
+ 'test-api-key' ,
253
+ { accessToken : 'access-token-new' , expiresIn : 10_000 }
254
+ ) ;
237
255
} ) ;
238
256
239
257
afterEach ( ( ) => {
240
258
sinon . restore ( ) ;
259
+ mockFetch . tearDown ( ) ;
241
260
} ) ;
242
261
243
262
context ( 'getFirebaseToken' , ( ) => {
@@ -265,7 +284,8 @@ describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
265
284
expirationTime : now - 5_000
266
285
} ) ;
267
286
expect ( await regionalAuthInternal . getToken ( ) ) . to . null ;
268
- expect ( regionalAuth . firebaseToken ) . to . null ;
287
+ const firebaseToken = await regionalAuth . getFirebaseAccessToken ( ) ;
288
+ expect ( firebaseToken ) . to . null ;
269
289
} ) ;
270
290
271
291
it ( 'logs out if token is expiring in next 5 seconds' , async ( ) => {
@@ -274,24 +294,36 @@ describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
274
294
expirationTime : now + 5_000
275
295
} ) ;
276
296
expect ( await regionalAuthInternal . getToken ( ) ) . to . null ;
277
- expect ( regionalAuth . firebaseToken ) . to . null ;
297
+ const firebaseToken = await regionalAuth . getFirebaseAccessToken ( ) ;
298
+ expect ( firebaseToken ) . to . null ;
278
299
} ) ;
279
300
280
- it ( 'logs warning if getToken is called with forceRefresh true' , async ( ) => {
281
- sinon . stub ( console , 'warn' ) ;
282
- await regionalAuth . _updateFirebaseToken ( {
301
+ it ( 'returns refreshIdToken if getToken is called with forceRefresh true' , async ( ) => {
302
+ await regionalAuthWithRefreshToken . _updateFirebaseToken ( {
283
303
token : 'access-token' ,
284
- expirationTime : now + 300_000
304
+ expirationTime : now + 30_000
285
305
} ) ;
286
- expect ( await regionalAuthInternal . getToken ( true ) ) . to . eql ( {
287
- accessToken : 'access-token'
306
+ expect ( await regionalAuthWithRefreshTokenInternal . getToken ( true ) ) . to . eql ( {
307
+ accessToken : 'access-token-new '
288
308
} ) ;
289
- expect ( console . warn ) . to . have . been . calledWith (
290
- sinon . match . string ,
291
- sinon . match (
292
- / R e f r e s h t o k e n i s n o t a v a l i d o p e r a t i o n f o r R e g i o n a l A u t h i n s t a n c e i n i t i a l i z e d \. /
293
- )
294
- ) ;
309
+ } ) ;
310
+
311
+ it ( 'returns refreshIdToken if current idToken is expired' , async ( ) => {
312
+ await regionalAuthWithRefreshToken . _updateFirebaseToken ( {
313
+ token : 'access-token' ,
314
+ expirationTime : now - 5_000
315
+ } ) ;
316
+ expect ( await regionalAuthWithRefreshTokenInternal . getToken ( ) ) . to . eql ( {
317
+ accessToken : 'access-token-new'
318
+ } ) ;
319
+ } ) ;
320
+
321
+ it ( 'returns null if current idToken is expired and tokenRefreshHandler is not implemented' , async ( ) => {
322
+ await regionalAuth . _updateFirebaseToken ( {
323
+ token : 'access-token' ,
324
+ expirationTime : now - 5_000
325
+ } ) ;
326
+ expect ( await regionalAuthInternal . getToken ( ) ) . to . eql ( null ) ;
295
327
} ) ;
296
328
} ) ;
297
329
} ) ;
0 commit comments