@@ -6,7 +6,7 @@ import Overlay from './overlay';
66import { PassportError , PassportErrorType } from './errors/passportError' ;
77import { PassportConfiguration } from './config' ;
88import { mockUser , mockUserImx , mockUserZkEvm } from './test/mocks' ;
9- import { isTokenExpired } from './utils/token' ;
9+ import { isAccessTokenExpiredOrExpiring } from './utils/token' ;
1010import { isUserZkEvm , PassportModuleConfiguration } from './types' ;
1111
1212jest . mock ( 'jwt-decode' ) ;
@@ -352,7 +352,7 @@ describe('AuthManager', () => {
352352 describe ( 'when getUser returns a user' , ( ) => {
353353 it ( 'should return the user' , async ( ) => {
354354 mockGetUser . mockReturnValue ( mockOidcUser ) ;
355- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
355+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
356356
357357 const result = await authManager . getUserOrLogin ( ) ;
358358
@@ -364,7 +364,7 @@ describe('AuthManager', () => {
364364 it ( 'calls attempts to sign in the user using signinPopup' , async ( ) => {
365365 mockGetUser . mockRejectedValue ( new Error ( mockErrorMsg ) ) ;
366366 mockSigninPopup . mockReturnValue ( mockOidcUser ) ;
367- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
367+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
368368
369369 const result = await authManager . getUserOrLogin ( ) ;
370370
@@ -510,16 +510,67 @@ describe('AuthManager', () => {
510510 describe ( 'getUser' , ( ) => {
511511 it ( 'should retrieve the user from the userManager and return the domain model' , async ( ) => {
512512 mockGetUser . mockReturnValue ( mockOidcUser ) ;
513- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
513+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
514514
515515 const result = await authManager . getUser ( ) ;
516516
517517 expect ( result ) . toEqual ( mockUser ) ;
518518 } ) ;
519519
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+
520571 it ( 'should call signinSilent and returns user when user token is expired with the refresh token' , async ( ) => {
521572 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
522- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
573+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
523574 mockSigninSilent . mockResolvedValue ( mockOidcUser ) ;
524575
525576 const result = await authManager . getUser ( ) ;
@@ -530,7 +581,7 @@ describe('AuthManager', () => {
530581
531582 it ( 'should reject with an error when signinSilent throws a string' , async ( ) => {
532583 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
533- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
584+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
534585 mockSigninSilent . mockRejectedValue ( 'oops' ) ;
535586
536587 await expect ( ( ) => authManager . getUser ( ) ) . rejects . toThrow (
@@ -543,7 +594,7 @@ describe('AuthManager', () => {
543594
544595 it ( 'should return null when the user token is expired without refresh token' , async ( ) => {
545596 mockGetUser . mockReturnValue ( mockOidcExpiredNoRefreshTokenUser ) ;
546- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
597+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
547598
548599 const result = await authManager . getUser ( ) ;
549600
@@ -553,7 +604,7 @@ describe('AuthManager', () => {
553604
554605 it ( 'should return null when the user token is expired with the refresh token, but signinSilent returns null' , async ( ) => {
555606 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
556- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
607+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
557608 mockSigninSilent . mockResolvedValue ( null ) ;
558609 const result = await authManager . getUser ( ) ;
559610
@@ -584,7 +635,7 @@ describe('AuthManager', () => {
584635 describe ( 'when the user is expired' , ( ) => {
585636 it ( 'should only call refresh the token once' , async ( ) => {
586637 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
587- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
638+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
588639 mockSigninSilent . mockReturnValue ( mockOidcUser ) ;
589640
590641 await Promise . allSettled ( [
@@ -600,7 +651,7 @@ describe('AuthManager', () => {
600651 describe ( 'when the user does not meet the type assertion' , ( ) => {
601652 it ( 'should return null' , async ( ) => {
602653 mockGetUser . mockReturnValue ( mockOidcUser ) ;
603- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
654+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
604655
605656 const result = await authManager . getUser ( isUserZkEvm ) ;
606657
@@ -617,7 +668,7 @@ describe('AuthManager', () => {
617668 zkevm_user_admin_address : mockUserZkEvm . zkEvm . userAdminAddress ,
618669 } ,
619670 } ) ;
620- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
671+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
621672
622673 const result = await authManager . getUser ( isUserZkEvm ) ;
623674
0 commit comments