@@ -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,61 @@ 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 and isAccessTokenExpiredOrExpiring returns true' , async ( ) => {
521+ const userWithoutIdToken = { ...mockOidcUser , id_token : undefined } ;
522+ mockGetUser . mockReturnValue ( userWithoutIdToken ) ;
523+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
524+
525+ const result = await authManager . getUser ( ) ;
526+
527+ expect ( result ) . toBeNull ( ) ;
528+ expect ( isAccessTokenExpiredOrExpiring ) . toHaveBeenCalledWith ( userWithoutIdToken ) ;
529+ } ) ;
530+
531+ it ( 'should refresh token when expires_in is 30 seconds or less' , async ( ) => {
532+ const expiringUser = { ...mockOidcUser , expires_in : 25 } ;
533+ mockGetUser . mockReturnValue ( expiringUser ) ;
534+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
535+ mockSigninSilent . mockResolvedValue ( mockOidcUser ) ;
536+
537+ const result = await authManager . getUser ( ) ;
538+
539+ expect ( mockSigninSilent ) . toBeCalledTimes ( 1 ) ;
540+ expect ( result ) . toEqual ( mockUser ) ;
541+ } ) ;
542+
543+ it ( 'should handle user with null expires_in' , async ( ) => {
544+ const userWithNullExpiresIn = { ...mockOidcUser , expires_in : null } ;
545+ mockGetUser . mockReturnValue ( userWithNullExpiresIn ) ;
546+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
547+
548+ const result = await authManager . getUser ( ) ;
549+
550+ expect ( mockSigninSilent ) . not . toHaveBeenCalled ( ) ;
551+ expect ( result ) . toEqual ( mockUser ) ;
552+ } ) ;
553+
554+ it ( 'should return user directly when token is not expired or expiring' , async ( ) => {
555+ const freshUser = { ...mockOidcUser , expires_in : 3600 } ; // 1 hour
556+ mockGetUser . mockReturnValue ( freshUser ) ;
557+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
558+
559+ const result = await authManager . getUser ( ) ;
560+
561+ expect ( mockSigninSilent ) . not . toHaveBeenCalled ( ) ;
562+ expect ( result ) . toEqual ( mockUser ) ;
563+ } ) ;
564+
520565 it ( 'should call signinSilent and returns user when user token is expired with the refresh token' , async ( ) => {
521566 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
522- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
567+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
523568 mockSigninSilent . mockResolvedValue ( mockOidcUser ) ;
524569
525570 const result = await authManager . getUser ( ) ;
@@ -530,7 +575,7 @@ describe('AuthManager', () => {
530575
531576 it ( 'should reject with an error when signinSilent throws a string' , async ( ) => {
532577 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
533- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
578+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
534579 mockSigninSilent . mockRejectedValue ( 'oops' ) ;
535580
536581 await expect ( ( ) => authManager . getUser ( ) ) . rejects . toThrow (
@@ -543,7 +588,7 @@ describe('AuthManager', () => {
543588
544589 it ( 'should return null when the user token is expired without refresh token' , async ( ) => {
545590 mockGetUser . mockReturnValue ( mockOidcExpiredNoRefreshTokenUser ) ;
546- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
591+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
547592
548593 const result = await authManager . getUser ( ) ;
549594
@@ -553,7 +598,7 @@ describe('AuthManager', () => {
553598
554599 it ( 'should return null when the user token is expired with the refresh token, but signinSilent returns null' , async ( ) => {
555600 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
556- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
601+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
557602 mockSigninSilent . mockResolvedValue ( null ) ;
558603 const result = await authManager . getUser ( ) ;
559604
@@ -584,7 +629,7 @@ describe('AuthManager', () => {
584629 describe ( 'when the user is expired' , ( ) => {
585630 it ( 'should only call refresh the token once' , async ( ) => {
586631 mockGetUser . mockReturnValue ( mockOidcExpiredUser ) ;
587- ( isTokenExpired as jest . Mock ) . mockReturnValue ( true ) ;
632+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( true ) ;
588633 mockSigninSilent . mockReturnValue ( mockOidcUser ) ;
589634
590635 await Promise . allSettled ( [
@@ -600,7 +645,7 @@ describe('AuthManager', () => {
600645 describe ( 'when the user does not meet the type assertion' , ( ) => {
601646 it ( 'should return null' , async ( ) => {
602647 mockGetUser . mockReturnValue ( mockOidcUser ) ;
603- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
648+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
604649
605650 const result = await authManager . getUser ( isUserZkEvm ) ;
606651
@@ -617,7 +662,7 @@ describe('AuthManager', () => {
617662 zkevm_user_admin_address : mockUserZkEvm . zkEvm . userAdminAddress ,
618663 } ,
619664 } ) ;
620- ( isTokenExpired as jest . Mock ) . mockReturnValue ( false ) ;
665+ ( isAccessTokenExpiredOrExpiring as jest . Mock ) . mockReturnValue ( false ) ;
621666
622667 const result = await authManager . getUser ( isUserZkEvm ) ;
623668
0 commit comments