@@ -37,11 +37,14 @@ describe('AtlasServiceMain', function () {
3737 AtlasService [ 'attachOidcPluginLoggerEvents' ] ( ) ;
3838
3939 const fetch = AtlasService [ 'fetch' ] ;
40+ const ipcMain = AtlasService [ 'ipcMain' ] ;
4041 const apiBaseUrl = process . env . COMPASS_ATLAS_SERVICE_BASE_URL ;
4142 const issuer = process . env . COMPASS_OIDC_ISSUER ;
4243 const clientId = process . env . COMPASS_CLIENT_ID ;
4344
4445 beforeEach ( function ( ) {
46+ AtlasService [ 'ipcMain' ] = { handle : sandbox . stub ( ) } ;
47+
4548 process . env . COMPASS_ATLAS_SERVICE_BASE_URL = 'http://example.com' ;
4649 process . env . COMPASS_OIDC_ISSUER = 'http://example.com' ;
4750 process . env . COMPASS_CLIENT_ID = '1234abcd' ;
@@ -53,7 +56,9 @@ describe('AtlasServiceMain', function () {
5356 process . env . COMPASS_CLIENT_ID = clientId ;
5457
5558 AtlasService [ 'fetch' ] = fetch ;
59+ AtlasService [ 'ipcMain' ] = ipcMain ;
5660 AtlasService [ 'token' ] = null ;
61+ AtlasService [ 'initPromise' ] = null ;
5762 AtlasService [ 'oidcPluginSyncedFromLoggerState' ] = 'initial' ;
5863
5964 sandbox . resetHistory ( ) ;
@@ -182,10 +187,11 @@ describe('AtlasServiceMain', function () {
182187 ( async ( ) => {
183188 await wait ( 20 ) ;
184189 AtlasService [ 'oidcPluginLogger' ] . emit (
185- 'mongodb-oidc-plugin:refresh-succeeded '
190+ 'mongodb-oidc-plugin:refresh-started '
186191 ) ;
187192 AtlasService [ 'oidcPluginLogger' ] . emit (
188- 'mongodb-oidc-plugin:state-updated'
193+ 'mongodb-oidc-plugin:auth-succeeded' ,
194+ { } as any
189195 ) ;
190196 } ) ( ) ,
191197 ] ) ;
@@ -346,10 +352,11 @@ describe('AtlasServiceMain', function () {
346352 ( async ( ) => {
347353 await wait ( 20 ) ;
348354 AtlasService [ 'oidcPluginLogger' ] . emit (
349- 'mongodb-oidc-plugin:refresh-succeeded '
355+ 'mongodb-oidc-plugin:refresh-started '
350356 ) ;
351357 AtlasService [ 'oidcPluginLogger' ] . emit (
352- 'mongodb-oidc-plugin:state-updated'
358+ 'mongodb-oidc-plugin:auth-succeeded' ,
359+ { } as any
353360 ) ;
354361 } ) ( ) ,
355362 ] ) ;
@@ -480,51 +487,67 @@ describe('AtlasServiceMain', function () {
480487 ) ;
481488 } ) ;
482489
483- it ( 'should set AtlasService state to error on `mongodb-oidc-plugin:refresh-failed` event' , function ( ) {
484- AtlasService [ 'oidcPluginLogger' ] . emit (
485- 'mongodb-oidc-plugin:refresh-failed' as any ,
486- { error : 'Stringified logger error' }
487- ) ;
488- expect ( AtlasService ) . to . have . property (
489- 'oidcPluginSyncedFromLoggerState' ,
490- 'error'
491- ) ;
492- } ) ;
493-
494- it ( 'should refresh token in atlas service state on `mongodb-oidc-plugin:refresh-succeeded` event' , async function ( ) {
495- const initialListenerCount = getListenerCount (
496- mockOidcPlugin . logger as EventEmitter
497- ) ;
490+ it ( 'should refresh token in atlas service state on `mongodb-oidc-plugin:refresh-started` event' , async function ( ) {
498491 // Checking that multiple events while we are refreshing don't cause
499- // multiple calls to REFRESH_TOKEN_CALLBACK
500- mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:refresh-succeeded' ) ;
501- mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:refresh-succeeded' ) ;
502- mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:refresh-succeeded' ) ;
503- // Checking that refresh-succeeded doesn't update the service state as we
504- // are just starting the refresh actually
505- expect ( AtlasService ) . to . have . property (
506- 'oidcPluginSyncedFromLoggerState' ,
507- 'initial'
492+ // multiple calls to REQUEST_TOKEN_CALLBACK
493+ mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:refresh-started' ) ;
494+ mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:refresh-started' ) ;
495+ mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:refresh-started' ) ;
496+ // Make it look like oidc-plugin successfully updated
497+ mockOidcPlugin . logger . emit (
498+ 'mongodb-oidc-plugin:auth-succeeded' ,
499+ { } as any
508500 ) ;
509- mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:state-updated' ) ;
510501 await once (
511502 AtlasService [ 'oidcPluginLogger' ] ,
512503 'atlas-service-token-refreshed'
513504 ) ;
514505 expect (
515506 mockOidcPlugin . mongoClientOptions . authMechanismProperties
516- . REFRESH_TOKEN_CALLBACK
507+ . REQUEST_TOKEN_CALLBACK
517508 ) . to . have . been . calledOnce ;
518509 expect ( AtlasService ) . to . have . property (
519510 'oidcPluginSyncedFromLoggerState' ,
520511 'authenticated'
521512 ) ;
522513 expect ( AtlasService )
523514 . to . have . property ( 'token' )
524- . deep . eq ( { accessToken : '4321' } ) ;
525- // Checking that we cleaned up all listeners
526- expect ( getListenerCount ( mockOidcPlugin . logger as EventEmitter ) ) . to . eq (
527- initialListenerCount
515+ . deep . eq ( { accessToken : '1234' } ) ;
516+ } ) ;
517+ } ) ;
518+
519+ describe ( 'init' , function ( ) {
520+ it ( 'should set service to unauthenticated state if requesting token throws' , async function ( ) {
521+ mockOidcPlugin . mongoClientOptions . authMechanismProperties . REQUEST_TOKEN_CALLBACK =
522+ sandbox
523+ . stub ( )
524+ . rejects ( new Error ( 'Could not retrieve valid access token' ) ) ;
525+ const createPlugin = ( ) => mockOidcPlugin ;
526+ const initPromise = AtlasService . init ( createPlugin ) ;
527+ expect ( AtlasService ) . to . have . property (
528+ 'oidcPluginSyncedFromLoggerState' ,
529+ 'restoring'
530+ ) ;
531+ await initPromise ;
532+ expect ( AtlasService ) . to . have . property (
533+ 'oidcPluginSyncedFromLoggerState' ,
534+ 'unauthenticated'
535+ ) ;
536+ } ) ;
537+
538+ it ( 'should set service to autenticated state if token was returned' , async function ( ) {
539+ mockOidcPlugin . mongoClientOptions . authMechanismProperties . REQUEST_TOKEN_CALLBACK =
540+ sandbox . stub ( ) . resolves ( { accessToken : 'token' } ) ;
541+ const createPlugin = ( ) => mockOidcPlugin ;
542+ const initPromise = AtlasService . init ( createPlugin ) ;
543+ expect ( AtlasService ) . to . have . property (
544+ 'oidcPluginSyncedFromLoggerState' ,
545+ 'restoring'
546+ ) ;
547+ await initPromise ;
548+ expect ( AtlasService ) . to . have . property (
549+ 'oidcPluginSyncedFromLoggerState' ,
550+ 'authenticated'
528551 ) ;
529552 } ) ;
530553 } ) ;
0 commit comments