@@ -37,11 +37,14 @@ describe('AtlasServiceMain', function () {
37
37
AtlasService [ 'attachOidcPluginLoggerEvents' ] ( ) ;
38
38
39
39
const fetch = AtlasService [ 'fetch' ] ;
40
+ const ipcMain = AtlasService [ 'ipcMain' ] ;
40
41
const apiBaseUrl = process . env . COMPASS_ATLAS_SERVICE_BASE_URL ;
41
42
const issuer = process . env . COMPASS_OIDC_ISSUER ;
42
43
const clientId = process . env . COMPASS_CLIENT_ID ;
43
44
44
45
beforeEach ( function ( ) {
46
+ AtlasService [ 'ipcMain' ] = { handle : sandbox . stub ( ) } ;
47
+
45
48
process . env . COMPASS_ATLAS_SERVICE_BASE_URL = 'http://example.com' ;
46
49
process . env . COMPASS_OIDC_ISSUER = 'http://example.com' ;
47
50
process . env . COMPASS_CLIENT_ID = '1234abcd' ;
@@ -53,7 +56,9 @@ describe('AtlasServiceMain', function () {
53
56
process . env . COMPASS_CLIENT_ID = clientId ;
54
57
55
58
AtlasService [ 'fetch' ] = fetch ;
59
+ AtlasService [ 'ipcMain' ] = ipcMain ;
56
60
AtlasService [ 'token' ] = null ;
61
+ AtlasService [ 'initPromise' ] = null ;
57
62
AtlasService [ 'oidcPluginSyncedFromLoggerState' ] = 'initial' ;
58
63
59
64
sandbox . resetHistory ( ) ;
@@ -182,10 +187,11 @@ describe('AtlasServiceMain', function () {
182
187
( async ( ) => {
183
188
await wait ( 20 ) ;
184
189
AtlasService [ 'oidcPluginLogger' ] . emit (
185
- 'mongodb-oidc-plugin:refresh-succeeded '
190
+ 'mongodb-oidc-plugin:refresh-started '
186
191
) ;
187
192
AtlasService [ 'oidcPluginLogger' ] . emit (
188
- 'mongodb-oidc-plugin:state-updated'
193
+ 'mongodb-oidc-plugin:auth-succeeded' ,
194
+ { } as any
189
195
) ;
190
196
} ) ( ) ,
191
197
] ) ;
@@ -346,10 +352,11 @@ describe('AtlasServiceMain', function () {
346
352
( async ( ) => {
347
353
await wait ( 20 ) ;
348
354
AtlasService [ 'oidcPluginLogger' ] . emit (
349
- 'mongodb-oidc-plugin:refresh-succeeded '
355
+ 'mongodb-oidc-plugin:refresh-started '
350
356
) ;
351
357
AtlasService [ 'oidcPluginLogger' ] . emit (
352
- 'mongodb-oidc-plugin:state-updated'
358
+ 'mongodb-oidc-plugin:auth-succeeded' ,
359
+ { } as any
353
360
) ;
354
361
} ) ( ) ,
355
362
] ) ;
@@ -480,51 +487,67 @@ describe('AtlasServiceMain', function () {
480
487
) ;
481
488
} ) ;
482
489
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 ( ) {
498
491
// 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
508
500
) ;
509
- mockOidcPlugin . logger . emit ( 'mongodb-oidc-plugin:state-updated' ) ;
510
501
await once (
511
502
AtlasService [ 'oidcPluginLogger' ] ,
512
503
'atlas-service-token-refreshed'
513
504
) ;
514
505
expect (
515
506
mockOidcPlugin . mongoClientOptions . authMechanismProperties
516
- . REFRESH_TOKEN_CALLBACK
507
+ . REQUEST_TOKEN_CALLBACK
517
508
) . to . have . been . calledOnce ;
518
509
expect ( AtlasService ) . to . have . property (
519
510
'oidcPluginSyncedFromLoggerState' ,
520
511
'authenticated'
521
512
) ;
522
513
expect ( AtlasService )
523
514
. 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'
528
551
) ;
529
552
} ) ;
530
553
} ) ;
0 commit comments