@@ -456,6 +456,8 @@ describe('Auth Handler', () => {
456456
457457 beforeEach ( ( ) => {
458458 sandbox = createSandbox ( ) ;
459+ authHandler . oauthRefreshInFlight = null ;
460+ authHandler . isRefreshingToken = false ;
459461 configHandlerGetStub = sandbox . stub ( configHandler , 'get' ) ;
460462 cliuxPrintStub = sandbox . stub ( cliux , 'print' ) ;
461463 refreshTokenStub = sandbox . stub ( authHandler , 'refreshToken' ) . resolves ( ) ;
@@ -467,40 +469,64 @@ describe('Auth Handler', () => {
467469 } ) ;
468470
469471 it ( 'should resolve if the OAuth token is valid and not expired' , async ( ) => {
470- const expectedOAuthDateTime = '2023-05-30T12:00:00Z' ;
471- const expectedAuthorisationType = 'oauth' ;
472- const now = new Date ( '2023-05-30T12:30:00Z' ) ;
472+ const expectedOAuthDateTime = new Date ( Date . now ( ) - 30 * 60 * 1000 ) . toISOString ( ) ;
473+ const expectedAuthorisationType = 'OAUTH' ;
473474
474475 configHandlerGetStub . withArgs ( authHandler . oauthDateTimeKeyName ) . returns ( expectedOAuthDateTime ) ;
475476 configHandlerGetStub . withArgs ( authHandler . authorisationTypeKeyName ) . returns ( expectedAuthorisationType ) ;
476477
477- sandbox . stub ( Date , 'now' ) . returns ( now . getTime ( ) ) ;
478+ await authHandler . compareOAuthExpiry ( ) ;
479+ expect ( cliuxPrintStub . called ) . to . be . false ;
480+ expect ( refreshTokenStub . called ) . to . be . false ;
481+ expect ( unsetConfigDataStub . called ) . to . be . false ;
482+ } ) ;
478483
479- try {
480- await authHandler . compareOAuthExpiry ( ) ;
481- } catch ( error ) {
482- expect ( error ) . to . be . undefined ;
483- expect ( cliuxPrintStub . called ) . to . be . false ;
484- expect ( refreshTokenStub . called ) . to . be . false ;
485- expect ( unsetConfigDataStub . called ) . to . be . false ;
486- }
484+ it ( 'should refresh when force is true even if token is not expired' , async ( ) => {
485+ const expectedOAuthDateTime = new Date ( Date . now ( ) - 30 * 60 * 1000 ) . toISOString ( ) ;
486+ const expectedAuthorisationType = 'OAUTH' ;
487+
488+ configHandlerGetStub . withArgs ( authHandler . oauthDateTimeKeyName ) . returns ( expectedOAuthDateTime ) ;
489+ configHandlerGetStub . withArgs ( authHandler . authorisationTypeKeyName ) . returns ( expectedAuthorisationType ) ;
490+
491+ await authHandler . compareOAuthExpiry ( true ) ;
492+ expect ( cliuxPrintStub . calledOnceWithExactly ( 'Forcing token refresh...' ) ) . to . be . true ;
493+ expect ( refreshTokenStub . calledOnce ) . to . be . true ;
494+ expect ( unsetConfigDataStub . called ) . to . be . false ;
487495 } ) ;
488496
489- it ( 'should resolve if force is true and refreshToken is called ' , async ( ) => {
490- const expectedOAuthDateTime = '2023-05-30T12:00:00Z' ;
491- const expectedAuthorisationType = 'oauth ' ;
497+ it ( 'should refresh when token is expired ' , async ( ) => {
498+ const expectedOAuthDateTime = new Date ( Date . now ( ) - 2 * 60 * 60 * 1000 ) . toISOString ( ) ;
499+ const expectedAuthorisationType = 'OAUTH ' ;
492500
493501 configHandlerGetStub . withArgs ( authHandler . oauthDateTimeKeyName ) . returns ( expectedOAuthDateTime ) ;
494502 configHandlerGetStub . withArgs ( authHandler . authorisationTypeKeyName ) . returns ( expectedAuthorisationType ) ;
495503
496- try {
497- await authHandler . compareOAuthExpiry ( ) ;
498- } catch ( error ) {
499- expect ( error ) . to . be . undefined ;
500- expect ( cliuxPrintStub . calledOnceWithExactly ( 'Forcing token refresh...' ) ) . to . be . true ;
501- expect ( refreshTokenStub . calledOnce ) . to . be . true ;
502- expect ( unsetConfigDataStub . called ) . to . be . false ;
503- }
504+ await authHandler . compareOAuthExpiry ( false ) ;
505+ expect ( cliuxPrintStub . calledOnceWithExactly ( 'Token expired, refreshing the token' ) ) . to . be . true ;
506+ expect ( refreshTokenStub . calledOnce ) . to . be . true ;
507+ } ) ;
508+
509+ it ( 'should run a single refresh when compareOAuthExpiry is called concurrently' , async ( ) => {
510+ const expectedOAuthDateTime = new Date ( Date . now ( ) - 2 * 60 * 60 * 1000 ) . toISOString ( ) ;
511+ const expectedAuthorisationType = 'OAUTH' ;
512+ let resolveRefresh ;
513+ const refreshDone = new Promise ( ( r ) => {
514+ resolveRefresh = r ;
515+ } ) ;
516+
517+ configHandlerGetStub . withArgs ( authHandler . oauthDateTimeKeyName ) . returns ( expectedOAuthDateTime ) ;
518+ configHandlerGetStub . withArgs ( authHandler . authorisationTypeKeyName ) . returns ( expectedAuthorisationType ) ;
519+
520+ refreshTokenStub . callsFake ( async ( ) => {
521+ await refreshDone ;
522+ } ) ;
523+
524+ const p1 = authHandler . compareOAuthExpiry ( false ) ;
525+ const p2 = authHandler . compareOAuthExpiry ( false ) ;
526+ resolveRefresh ( ) ;
527+ await Promise . all ( [ p1 , p2 ] ) ;
528+
529+ expect ( refreshTokenStub . callCount ) . to . equal ( 1 ) ;
504530 } ) ;
505531 } ) ;
506532} ) ;
0 commit comments