@@ -342,7 +342,7 @@ describe('#db IAMAuth Plugin', function() {
342342 } ) ;
343343 } ) ;
344344
345- it ( 'skips IAM authentication if access token returns non-200 response' , function ( done ) {
345+ it ( 'returns an error if access token returns non-200 response' , function ( done ) {
346346 if ( process . env . NOCK_OFF ) {
347347 this . skip ( ) ;
348348 }
@@ -356,19 +356,11 @@ describe('#db IAMAuth Plugin', function() {
356356 . times ( 3 )
357357 . reply ( 500 , 'Internal Error 500\nThe server encountered an unexpected condition which prevented it from fulfilling the request.' ) ;
358358
359- var cloudantMocks = nock ( SERVER )
360- . get ( DBNAME )
361- . reply ( 401 , { error : 'unauthorized' , reason : 'Unauthorized' } ) ;
362-
363359 var cloudantClient = new Client ( { plugins : { iamauth : { iamApiKey : IAM_API_KEY } } } ) ;
364360 var req = { url : SERVER + DBNAME , method : 'GET' } ;
365361 cloudantClient . request ( req , function ( err , resp , data ) {
366- assert . equal ( err , null ) ;
367- assert . equal ( resp . request . headers . cookie , null ) ;
368- assert . equal ( resp . statusCode , 401 ) ;
369- assert . ok ( data . indexOf ( '"error":"unauthorized"' ) > - 1 ) ;
362+ assert . equal ( err . message , 'Failed to acquire access token. Status code: 500' ) ;
370363 iamMocks . done ( ) ;
371- cloudantMocks . done ( ) ;
372364 done ( ) ;
373365 } ) ;
374366 } ) ;
@@ -408,7 +400,7 @@ describe('#db IAMAuth Plugin', function() {
408400 } ) ;
409401 } ) ;
410402
411- it ( 'skips IAM authentication if IAM cookie login returns non-200 response' , function ( done ) {
403+ it ( 'returns an error if IAM cookie login returns non-200 response' , function ( done ) {
412404 if ( process . env . NOCK_OFF ) {
413405 this . skip ( ) ;
414406 }
@@ -425,17 +417,12 @@ describe('#db IAMAuth Plugin', function() {
425417 var cloudantMocks = nock ( SERVER )
426418 . post ( '/_iam_session' , { access_token : MOCK_ACCESS_TOKEN } )
427419 . times ( 3 )
428- . reply ( 500 , { error : 'internal_server_error' , reason : 'Internal Server Error' } )
429- . get ( DBNAME )
430- . reply ( 401 , { error : 'unauthorized' , reason : 'Unauthorized' } ) ;
420+ . reply ( 500 , { error : 'internal_server_error' , reason : 'Internal Server Error' } ) ;
431421
432422 var cloudantClient = new Client ( { plugins : { iamauth : { iamApiKey : IAM_API_KEY } } } ) ;
433423 var req = { url : SERVER + DBNAME , method : 'GET' } ;
434424 cloudantClient . request ( req , function ( err , resp , data ) {
435- assert . equal ( err , null ) ;
436- assert . equal ( resp . request . headers . cookie , null ) ;
437- assert . equal ( resp . statusCode , 401 ) ;
438- assert . ok ( data . indexOf ( '"error":"unauthorized"' ) > - 1 ) ;
425+ assert . equal ( err . message , 'Failed to exchange IAM token with Cloudant. Status code: 500' ) ;
439426 iamMocks . done ( ) ;
440427 cloudantMocks . done ( ) ;
441428 done ( ) ;
@@ -479,39 +466,6 @@ describe('#db IAMAuth Plugin', function() {
479466 } ) ;
480467 } ) ;
481468
482- it ( 'skips authentication renewal on 401 response if previous attempts failed' , function ( done ) {
483- if ( process . env . NOCK_OFF ) {
484- this . skip ( ) ;
485- }
486-
487- var iamMocks = nock ( TOKEN_SERVER )
488- . post ( '/identity/token' , {
489- 'grant_type' : 'urn:ibm:params:oauth:grant-type:apikey' ,
490- 'response_type' : 'cloud_iam' ,
491- 'apikey' : IAM_API_KEY
492- } )
493- . reply ( 400 , {
494- errorCode : 'BXNIM0415E' ,
495- errorMessage : 'Provided API key could not be found'
496- } ) ;
497-
498- var cloudantMocks = nock ( SERVER )
499- . get ( DBNAME )
500- . reply ( 401 , { error : 'unauthorized' , reason : 'Unauthorized' } ) ;
501-
502- var cloudantClient = new Client ( { plugins : { iamauth : { iamApiKey : IAM_API_KEY } } } ) ;
503- var req = { url : SERVER + DBNAME , method : 'GET' } ;
504- cloudantClient . request ( req , function ( err , resp , data ) {
505- assert . equal ( err , null ) ;
506- assert . equal ( resp . request . headers . cookie , null ) ;
507- assert . equal ( resp . statusCode , 401 ) ;
508- assert . ok ( data . indexOf ( '"error":"unauthorized"' ) > - 1 ) ;
509- iamMocks . done ( ) ;
510- cloudantMocks . done ( ) ;
511- done ( ) ;
512- } ) ;
513- } ) ;
514-
515469 it ( 'throws error for unspecified IAM API key' , function ( ) {
516470 assert . throws (
517471 ( ) => {
@@ -616,6 +570,7 @@ describe('#db IAMAuth Plugin', function() {
616570 'response_type' : 'cloud_iam' ,
617571 'apikey' : 'bad_key'
618572 } )
573+ . times ( 3 )
619574 . reply ( 400 , {
620575 errorCode : 'BXNIM0415E' ,
621576 errorMessage : 'Provided API key could not be found'
@@ -628,8 +583,6 @@ describe('#db IAMAuth Plugin', function() {
628583 . reply ( 200 , MOCK_IAM_TOKEN_RESPONSE ) ;
629584
630585 var cloudantMocks = nock ( SERVER )
631- . get ( DBNAME )
632- . reply ( 401 , { error : 'unauthorized' , reason : 'Unauthorized' } )
633586 . post ( '/_iam_session' , { access_token : MOCK_ACCESS_TOKEN } )
634587 . reply ( 200 , { ok : true } , MOCK_SET_IAM_SESSION_HEADER )
635588 . get ( DBNAME )
@@ -638,10 +591,7 @@ describe('#db IAMAuth Plugin', function() {
638591 var cloudantClient = new Client ( { plugins : { iamauth : { iamApiKey : 'bad_key' } } } ) ;
639592 var req = { url : SERVER + DBNAME , method : 'GET' } ;
640593 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 ) ;
594+ assert . equal ( err . message , 'Failed to acquire access token. Status code: 400' ) ;
645595
646596 // update IAM API key
647597 cloudantClient . getPlugin ( 'iamauth' ) . setIamApiKey ( IAM_API_KEY ) ;
0 commit comments