@@ -604,4 +604,58 @@ describe('#db IAMAuth Plugin', function() {
604604 done ( ) ;
605605 } ) ;
606606 } ) ;
607+
608+ it ( 'supports changing the IAM API key' , function ( done ) {
609+ if ( process . env . NOCK_OFF ) {
610+ this . skip ( ) ;
611+ }
612+
613+ var iamMocks = nock ( TOKEN_SERVER )
614+ . post ( '/identity/token' , {
615+ 'grant_type' : 'urn:ibm:params:oauth:grant-type:apikey' ,
616+ 'response_type' : 'cloud_iam' ,
617+ 'apikey' : 'bad_key'
618+ } )
619+ . reply ( 400 , {
620+ errorCode : 'BXNIM0415E' ,
621+ errorMessage : 'Provided API key could not be found'
622+ } )
623+ . post ( '/identity/token' , {
624+ 'grant_type' : 'urn:ibm:params:oauth:grant-type:apikey' ,
625+ 'response_type' : 'cloud_iam' ,
626+ 'apikey' : IAM_API_KEY
627+ } )
628+ . reply ( 200 , MOCK_IAM_TOKEN_RESPONSE ) ;
629+
630+ var cloudantMocks = nock ( SERVER )
631+ . get ( DBNAME )
632+ . reply ( 401 , { error : 'unauthorized' , reason : 'Unauthorized' } )
633+ . post ( '/_iam_session' , { access_token : MOCK_ACCESS_TOKEN } )
634+ . reply ( 200 , { ok : true } , MOCK_SET_IAM_SESSION_HEADER )
635+ . get ( DBNAME )
636+ . reply ( 200 , { doc_count : 0 } ) ;
637+
638+ var cloudantClient = new Client ( { plugins : { iamauth : { iamApiKey : 'bad_key' } } } ) ;
639+ var req = { url : SERVER + DBNAME , method : 'GET' } ;
640+ cloudantClient . request ( req , function ( err , resp , data ) {
641+ assert . equal ( err , null ) ;
642+ assert . equal ( resp . request . headers . cookie , null ) ;
643+ assert . equal ( resp . statusCode , 401 ) ;
644+ assert . ok ( data . indexOf ( '"error":"unauthorized"' ) > - 1 ) ;
645+
646+ // update IAM API key
647+ cloudantClient . getPlugin ( 'iamauth' ) . setIamApiKey ( IAM_API_KEY ) ;
648+
649+ cloudantClient . request ( req , function ( err , resp , data ) {
650+ assert . equal ( err , null ) ;
651+ assert . equal ( resp . request . headers . cookie , MOCK_IAM_SESSION ) ;
652+ assert . equal ( resp . statusCode , 200 ) ;
653+ assert . ok ( data . indexOf ( '"doc_count":0' ) > - 1 ) ;
654+
655+ iamMocks . done ( ) ;
656+ cloudantMocks . done ( ) ;
657+ done ( ) ;
658+ } ) ;
659+ } ) ;
660+ } ) ;
607661} ) ;
0 commit comments