18
18
import { FirebaseError } from '@firebase/util' ;
19
19
import { expect , use } from 'chai' ;
20
20
import * as sinon from 'sinon' ;
21
+ import sinonChai from 'sinon-chai' ;
21
22
import chaiAsPromised from 'chai-as-promised' ;
22
23
23
- import { testAuth , testUser } from '../../../test/helpers/mock_auth' ;
24
+ import {
25
+ regionalTestAuth ,
26
+ testAuth ,
27
+ testUser
28
+ } from '../../../test/helpers/mock_auth' ;
24
29
import { AuthInternal } from '../../model/auth' ;
25
30
import { UserInternal } from '../../model/user' ;
26
31
import { AuthInterop } from './firebase_internal' ;
27
32
33
+ use ( sinonChai ) ;
28
34
use ( chaiAsPromised ) ;
29
35
30
36
describe ( 'core/auth/firebase_internal' , ( ) => {
@@ -37,6 +43,9 @@ describe('core/auth/firebase_internal', () => {
37
43
38
44
afterEach ( ( ) => {
39
45
sinon . restore ( ) ;
46
+ delete ( auth as unknown as Record < string , unknown > ) [
47
+ '_initializationPromise'
48
+ ] ;
40
49
} ) ;
41
50
42
51
context ( 'getUid' , ( ) => {
@@ -215,3 +224,74 @@ describe('core/auth/firebase_internal', () => {
215
224
} ) ;
216
225
} ) ;
217
226
} ) ;
227
+
228
+ describe ( 'core/auth/firebase_internal - Regional Firebase Auth' , ( ) => {
229
+ let regionalAuth : AuthInternal ;
230
+ let regionalAuthInternal : AuthInterop ;
231
+ let now : number ;
232
+ beforeEach ( async ( ) => {
233
+ regionalAuth = await regionalTestAuth ( ) ;
234
+ regionalAuthInternal = new AuthInterop ( regionalAuth ) ;
235
+ now = Date . now ( ) ;
236
+ sinon . stub ( Date , 'now' ) . returns ( now ) ;
237
+ } ) ;
238
+
239
+ afterEach ( ( ) => {
240
+ sinon . restore ( ) ;
241
+ } ) ;
242
+
243
+ context ( 'getFirebaseToken' , ( ) => {
244
+ it ( 'returns null if firebase token is undefined' , async ( ) => {
245
+ expect ( await regionalAuthInternal . getToken ( ) ) . to . be . null ;
246
+ } ) ;
247
+
248
+ it ( 'returns the id token correctly' , async ( ) => {
249
+ await regionalAuth . _updateFirebaseToken ( {
250
+ token : 'access-token' ,
251
+ expirationTime : now + 300_000
252
+ } ) ;
253
+ expect ( await regionalAuthInternal . getToken ( ) ) . to . eql ( {
254
+ accessToken : 'access-token'
255
+ } ) ;
256
+ } ) ;
257
+
258
+ it ( 'logs out the the id token expires in next 30 seconds' , async ( ) => {
259
+ expect ( await regionalAuthInternal . getToken ( ) ) . to . be . null ;
260
+ } ) ;
261
+
262
+ it ( 'logs out if token has expired' , async ( ) => {
263
+ await regionalAuth . _updateFirebaseToken ( {
264
+ token : 'access-token' ,
265
+ expirationTime : now - 5_000
266
+ } ) ;
267
+ expect ( await regionalAuthInternal . getToken ( ) ) . to . null ;
268
+ expect ( regionalAuth . firebaseToken ) . to . null ;
269
+ } ) ;
270
+
271
+ it ( 'logs out if token is expiring in next 5 seconds' , async ( ) => {
272
+ await regionalAuth . _updateFirebaseToken ( {
273
+ token : 'access-token' ,
274
+ expirationTime : now + 5_000
275
+ } ) ;
276
+ expect ( await regionalAuthInternal . getToken ( ) ) . to . null ;
277
+ expect ( regionalAuth . firebaseToken ) . to . null ;
278
+ } ) ;
279
+
280
+ it ( 'logs warning if getToken is called with forceRefresh true' , async ( ) => {
281
+ sinon . stub ( console , 'warn' ) ;
282
+ await regionalAuth . _updateFirebaseToken ( {
283
+ token : 'access-token' ,
284
+ expirationTime : now + 300_000
285
+ } ) ;
286
+ expect ( await regionalAuthInternal . getToken ( true ) ) . to . eql ( {
287
+ accessToken : 'access-token'
288
+ } ) ;
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
+ ) ;
295
+ } ) ;
296
+ } ) ;
297
+ } ) ;
0 commit comments