@@ -633,9 +633,9 @@ def test_cloudant_context_helper(self):
633633 self .fail ('Exception {0} was raised.' .format (str (err )))
634634
635635 @skip_if_not_cookie_auth
636- def test_cloudant_bluemix_context_helper (self ):
636+ def test_cloudant_bluemix_context_helper_with_legacy_creds (self ):
637637 """
638- Test that the cloudant_bluemix context helper works as expected.
638+ Test that the cloudant_bluemix context helper with legacy creds works as expected.
639639 """
640640 instance_name = 'Cloudant NoSQL DB-lv'
641641 vcap_services = {'cloudantNoSQLDB' : [{
@@ -657,6 +657,56 @@ def test_cloudant_bluemix_context_helper(self):
657657 except Exception as err :
658658 self .fail ('Exception {0} was raised.' .format (str (err )))
659659
660+ @unittest .skipUnless (os .environ .get ('IAM_API_KEY' ),
661+ 'Skipping Cloudant Bluemix context helper with IAM test' )
662+ def test_cloudant_bluemix_context_helper_with_iam (self ):
663+ """
664+ Test that the cloudant_bluemix context helper with IAM works as expected.
665+ """
666+ instance_name = 'Cloudant NoSQL DB-lv'
667+ vcap_services = {'cloudantNoSQLDB' : [{
668+ 'credentials' : {
669+ 'apikey' : self .iam_api_key ,
670+ 'username' : self .user ,
671+ 'host' : '{0}.cloudant.com' .format (self .account ),
672+ 'port' : 443 ,
673+ 'url' : self .url
674+ },
675+ 'name' : instance_name ,
676+ }]}
677+
678+ try :
679+ with cloudant_bluemix (vcap_services , instance_name = instance_name ) as c :
680+ self .assertIsInstance (c , Cloudant )
681+ self .assertIsInstance (c .r_session , requests .Session )
682+ except Exception as err :
683+ self .fail ('Exception {0} was raised.' .format (str (err )))
684+
685+ def test_cloudant_bluemix_context_helper_raise_error_for_missing_iam_and_creds (self ):
686+ """
687+ Test that the cloudant_bluemix context helper raises a CloudantClientException
688+ when the IAM key, username, and password are missing in the VCAP_SERVICES env variable.
689+ """
690+ instance_name = 'Cloudant NoSQL DB-lv'
691+ vcap_services = {'cloudantNoSQLDB' : [{
692+ 'credentials' : {
693+ 'host' : '{0}.cloudant.com' .format (self .account ),
694+ 'port' : 443 ,
695+ 'url' : self .url
696+ },
697+ 'name' : instance_name ,
698+ }]}
699+
700+ try :
701+ with cloudant_bluemix (vcap_services , instance_name = instance_name ) as c :
702+ self .assertIsInstance (c , Cloudant )
703+ self .assertIsInstance (c .r_session , requests .Session )
704+ except CloudantClientException as err :
705+ self .assertEqual (
706+ 'Invalid service: IAM API key or username/password credentials are required.' ,
707+ str (err )
708+ )
709+
660710 def test_cloudant_bluemix_dedicated_context_helper (self ):
661711 """
662712 Test that the cloudant_bluemix context helper works as expected when
@@ -698,7 +748,7 @@ def test_constructor_with_account(self):
698748 )
699749
700750 @skip_if_not_cookie_auth
701- def test_bluemix_constructor (self ):
751+ def test_bluemix_constructor_with_legacy_creds (self ):
702752 """
703753 Test instantiating a client object using a VCAP_SERVICES environment
704754 variable.
@@ -730,7 +780,39 @@ def test_bluemix_constructor(self):
730780 finally :
731781 c .disconnect ()
732782
733- @skip_if_not_cookie_auth
783+
784+ @unittest .skipUnless (os .environ .get ('IAM_API_KEY' ),
785+ 'Skipping Cloudant Bluemix constructor with IAM test' )
786+ def test_bluemix_constructor_with_iam (self ):
787+ """
788+ Test instantiating a client object using a VCAP_SERVICES environment
789+ variable.
790+ """
791+ instance_name = 'Cloudant NoSQL DB-lv'
792+ vcap_services = {'cloudantNoSQLDB' : [{
793+ 'credentials' : {
794+ 'apikey' : self .iam_api_key ,
795+ 'username' : self .user ,
796+ 'host' : '{0}.cloudant.com' .format (self .account ),
797+ 'port' : 443
798+ },
799+ 'name' : instance_name
800+ }]}
801+
802+ # create Cloudant Bluemix client
803+ c = Cloudant .bluemix (vcap_services )
804+
805+ try :
806+ c .connect ()
807+ self .assertIsInstance (c , Cloudant )
808+ self .assertIsInstance (c .r_session , requests .Session )
809+
810+ except Exception as err :
811+ self .fail ('Exception {0} was raised.' .format (str (err )))
812+
813+ finally :
814+ c .disconnect ()
815+
734816 def test_bluemix_constructor_specify_instance_name (self ):
735817 """
736818 Test instantiating a client object using a VCAP_SERVICES environment
@@ -773,8 +855,7 @@ def test_bluemix_constructor_with_multiple_services(self):
773855 vcap_services = {'cloudantNoSQLDB' : [
774856 {
775857 'credentials' : {
776- 'username' : self .user ,
777- 'password' : self .pwd ,
858+ 'apikey' : '1234api' ,
778859 'host' : '{0}.cloudant.com' .format (self .account ),
779860 'port' : 443 ,
780861 'url' : self .url
0 commit comments