@@ -546,6 +546,96 @@ describe('OIDC Auth Spec Tests', function () {
546546 expect ( callbackSpy ) . to . have . been . calledTwice ;
547547 } ) ;
548548 } ) ;
549+
550+ describe ( '4.4 Speculative Authentication should be ignored on Reauthentication' , function ( ) {
551+ let utilClient : MongoClient ;
552+ const callbackSpy = sinon . spy ( createCallback ( ) ) ;
553+ const commands = [ ] ;
554+ // - Create an OIDC configured client.
555+ // - Populate the *Client Cache* with a valid access token to enforce Speculative Authentication.
556+ // - Perform an `insert` operation that succeeds.
557+ // - Assert that the callback was not called.
558+ // - Assert there were no `SaslStart` commands executed.
559+ // - Set a fail point for `insert` commands of the form:
560+ // ```javascript
561+ // {
562+ // configureFailPoint: "failCommand",
563+ // mode: {
564+ // times: 1
565+ // },
566+ // data: {
567+ // failCommands: [
568+ // "insert"
569+ // ],
570+ // errorCode: 391 // ReauthenticationRequired
571+ // }
572+ // }
573+ // ```
574+ // - Perform an `insert` operation that succeeds.
575+ // - Assert that the callback was called once.
576+ // - Assert there were `SaslStart` commands executed.
577+ // - Close the client.
578+ beforeEach ( async function ( ) {
579+ client = new MongoClient ( uriSingle , {
580+ authMechanismProperties : {
581+ OIDC_CALLBACK : callbackSpy
582+ } ,
583+ retryReads : false ,
584+ monitorCommands : true
585+ } ) ;
586+ client . on ( 'commandStarted' , event => {
587+ console . log ( event ) ;
588+ if ( event . commandName === 'saslStart' ) {
589+ commands . push ( event ) ;
590+ }
591+ } )
592+ const provider = client . s . authProviders . getOrCreateProvider ( 'MONGODB-OIDC' , {
593+ OIDC_CALLBACK : callbackSpy
594+ } ) as MongoDBOIDC ;
595+ const token = await readFile ( path . join ( process . env . OIDC_TOKEN_DIR , 'test_user1' ) , {
596+ encoding : 'utf8'
597+ } ) ;
598+ provider . workflow . cache . put ( { accessToken : token } ) ;
599+ collection = client . db ( 'test' ) . collection ( 'test' ) ;
600+ await collection . insertOne ( { name : 'test' } ) ;
601+ expect ( callbackSpy ) . to . not . have . been . called ;
602+ expect ( commands ) . to . be . empty ;
603+
604+ utilClient = new MongoClient ( uriSingle , {
605+ authMechanismProperties : {
606+ OIDC_CALLBACK : createCallback ( )
607+ } ,
608+ retryReads : false
609+ } ) ;
610+ await utilClient
611+ . db ( )
612+ . admin ( )
613+ . command ( {
614+ configureFailPoint : 'failCommand' ,
615+ mode : {
616+ times : 1
617+ } ,
618+ data : {
619+ failCommands : [ 'insert' ] ,
620+ errorCode : 391
621+ }
622+ } ) ;
623+ } ) ;
624+
625+ afterEach ( async function ( ) {
626+ await utilClient . db ( ) . admin ( ) . command ( {
627+ configureFailPoint : 'failCommand' ,
628+ mode : 'off'
629+ } ) ;
630+ await utilClient . close ( ) ;
631+ } ) ;
632+
633+ it ( 'successfully authenticates' , async function ( ) {
634+ await collection . insertOne ( { name : 'test' } ) ;
635+ expect ( callbackSpy ) . to . have . been . calledOnce ;
636+ expect ( commands . length ) . to . equal ( 1 ) ;
637+ } ) ;
638+ } ) ;
549639 } ) ;
550640 } ) ;
551641
0 commit comments