@@ -6,7 +6,7 @@ import Overlay from './overlay';
6
6
import { PassportError , PassportErrorType } from './errors/passportError' ;
7
7
import { PassportConfiguration } from './config' ;
8
8
import { mockUser , mockUserImx , mockUserZkEvm } from './test/mocks' ;
9
- import { isTokenExpired } from './utils/token' ;
9
+ import { isAccessTokenExpiredOrExpiring } from './utils/token' ;
10
10
import { isUserZkEvm , PassportModuleConfiguration } from './types' ;
11
11
12
12
jest . mock ( 'jwt-decode' ) ;
@@ -352,7 +352,7 @@ describe('AuthManager', () => {
352
352
describe ( 'when getUser returns a user' , ( ) => {
353
353
it ( 'should return the user' , async ( ) => {
354
354
mockGetUser . mockReturnValue ( mockOidcUser ) ;
355
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
355
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
356
356
357
357
const result = await authManager . getUserOrLogin ( ) ;
358
358
@@ -364,7 +364,7 @@ describe('AuthManager', () => {
364
364
it ( 'calls attempts to sign in the user using signinPopup' , async ( ) => {
365
365
mockGetUser . mockRejectedValue ( new Error ( mockErrorMsg ) ) ;
366
366
mockSigninPopup . mockReturnValue ( mockOidcUser ) ;
367
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
367
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
368
368
369
369
const result = await authManager . getUserOrLogin ( ) ;
370
370
@@ -510,16 +510,67 @@ describe('AuthManager', () => {
510
510
describe ( 'getUser' , ( ) => {
511
511
it ( 'should retrieve the user from the userManager and return the domain model' , async ( ) => {
512
512
mockGetUser . mockReturnValue ( mockOidcUser ) ;
513
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
513
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
514
514
515
515
const result = await authManager . getUser ( ) ;
516
516
517
517
expect ( result ) . toEqual ( mockUser ) ;
518
518
} ) ;
519
519
520
+ it ( 'should return null when user has no idToken' , async ( ) => {
521
+ const userWithoutIdToken = { ...mockOidcUser , id_token : undefined , refresh_token : undefined } ;
522
+ mockGetUser . mockReturnValue ( userWithoutIdToken ) ;
523
+ // Restore real function behavior for this test
524
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockImplementation (
525
+ jest . requireActual ( './utils/token' ) . isAccessTokenExpiredOrExpiring ,
526
+ ) ;
527
+
528
+ const result = await authManager . getUser ( ) ;
529
+
530
+ expect ( result ) . toBeNull ( ) ;
531
+ } ) ;
532
+
533
+ it ( 'should refresh token when access token is expired or expiring' , async ( ) => {
534
+ const userWithExpiringAccessToken = { ...mockOidcUser } ;
535
+ mockGetUser . mockReturnValue ( userWithExpiringAccessToken ) ;
536
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
537
+ mockSigninSilent . mockResolvedValue ( mockOidcUser ) ;
538
+
539
+ const result = await authManager . getUser ( ) ;
540
+
541
+ expect ( mockSigninSilent ) . toBeCalledTimes ( 1 ) ;
542
+ expect ( result ) . toEqual ( mockUser ) ;
543
+ } ) ;
544
+
545
+ it ( 'should handle user with missing access token' , async ( ) => {
546
+ const userWithoutAccessToken = { ...mockOidcUser , access_token : undefined } ;
547
+ mockGetUser . mockReturnValue ( userWithoutAccessToken ) ;
548
+ // Restore real function behavior for this test
549
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockImplementation (
550
+ jest . requireActual ( './utils/token' ) . isAccessTokenExpiredOrExpiring ,
551
+ ) ;
552
+ mockSigninSilent . mockResolvedValue ( mockOidcUser ) ;
553
+
554
+ const result = await authManager . getUser ( ) ;
555
+
556
+ expect ( mockSigninSilent ) . toBeCalledTimes ( 1 ) ;
557
+ expect ( result ) . toEqual ( mockUser ) ;
558
+ } ) ;
559
+
560
+ it ( 'should return user directly when access token is not expired or expiring' , async ( ) => {
561
+ const freshUser = { ...mockOidcUser } ;
562
+ mockGetUser . mockReturnValue ( freshUser ) ;
563
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
564
+
565
+ const result = await authManager . getUser ( ) ;
566
+
567
+ expect ( mockSigninSilent ) . not . toHaveBeenCalled ( ) ;
568
+ expect ( result ) . toEqual ( mockUser ) ;
569
+ } ) ;
570
+
520
571
it ( 'should call signinSilent and returns user when user token is expired with the refresh token' , async ( ) => {
521
572
mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
522
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
573
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
523
574
mockSigninSilent . mockResolvedValue ( mockOidcUser ) ;
524
575
525
576
const result = await authManager . getUser ( ) ;
@@ -530,7 +581,7 @@ describe('AuthManager', () => {
530
581
531
582
it ( 'should reject with an error when signinSilent throws a string' , async ( ) => {
532
583
mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
533
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
584
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
534
585
mockSigninSilent . mockRejectedValue ( 'oops' ) ;
535
586
536
587
await expect ( ( ) => authManager . getUser ( ) ) . rejects . toThrow (
@@ -543,7 +594,7 @@ describe('AuthManager', () => {
543
594
544
595
it ( 'should return null when the user token is expired without refresh token' , async ( ) => {
545
596
mockGetUser . mockReturnValue ( mockOidcExpiredNoRefreshTokenUser ) ;
546
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
597
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
547
598
548
599
const result = await authManager . getUser ( ) ;
549
600
@@ -553,7 +604,7 @@ describe('AuthManager', () => {
553
604
554
605
it ( 'should return null when the user token is expired with the refresh token, but signinSilent returns null' , async ( ) => {
555
606
mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
556
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
607
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
557
608
mockSigninSilent . mockResolvedValue ( null ) ;
558
609
const result = await authManager . getUser ( ) ;
559
610
@@ -584,7 +635,7 @@ describe('AuthManager', () => {
584
635
describe ( 'when the user is expired' , ( ) => {
585
636
it ( 'should only call refresh the token once' , async ( ) => {
586
637
mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
587
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
638
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
588
639
mockSigninSilent . mockReturnValue ( mockOidcUser ) ;
589
640
590
641
await Promise . allSettled ( [
@@ -600,7 +651,7 @@ describe('AuthManager', () => {
600
651
describe ( 'when the user does not meet the type assertion' , ( ) => {
601
652
it ( 'should return null' , async ( ) => {
602
653
mockGetUser . mockReturnValue ( mockOidcUser ) ;
603
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
654
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
604
655
605
656
const result = await authManager . getUser ( isUserZkEvm ) ;
606
657
@@ -617,7 +668,7 @@ describe('AuthManager', () => {
617
668
zkevm_user_admin_address : mockUserZkEvm . zkEvm . userAdminAddress ,
618
669
} ,
619
670
} ) ;
620
- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
671
+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
621
672
622
673
const result = await authManager . getUser ( isUserZkEvm ) ;
623
674
0 commit comments