1
1
#!/usr/bin/env python
2
- # Copyright (C) 2015, 2020 IBM Corp. All rights reserved.
2
+ # Copyright (C) 2015, 2021 IBM Corp. All rights reserved.
3
3
#
4
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
5
# you may not use this file except in compliance with the License.
27
27
import sys
28
28
import unittest
29
29
from time import sleep
30
+ from urllib .parse import urlparse
30
31
31
32
import mock
32
33
import requests
@@ -213,7 +214,7 @@ def test_auto_renew_enabled_with_auto_connect(self):
213
214
@skip_if_not_cookie_auth
214
215
def test_session (self ):
215
216
"""
216
- Test getting session information.
217
+ Test getting session information.
217
218
Session info is None if CouchDB Admin Party mode was selected.
218
219
"""
219
220
try :
@@ -563,7 +564,7 @@ def test_get_cached_db_object_via_get(self):
563
564
self .client .connect ()
564
565
# Default returns None
565
566
self .assertIsNone (self .client .get ('no_such_db' ))
566
- # Creates the database remotely and adds it to the
567
+ # Creates the database remotely and adds it to the
567
568
# client database cache
568
569
db = self .client .create_database (dbname )
569
570
# Locally cached database object is returned
@@ -702,7 +703,7 @@ def test_cloudant_context_helper(self):
702
703
Test that the cloudant context helper works as expected.
703
704
"""
704
705
try :
705
- with cloudant (self .user , self .pwd , account = self .account ) as c :
706
+ with cloudant (self .user , self .pwd , url = self .url ) as c :
706
707
self .assertIsInstance (c , Cloudant )
707
708
self .assertIsInstance (c .r_session , requests .Session )
708
709
except Exception as err :
@@ -718,7 +719,7 @@ def test_cloudant_bluemix_context_helper_with_legacy_creds(self):
718
719
'credentials' : {
719
720
'username' : self .user ,
720
721
'password' : self .pwd ,
721
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
722
+ 'host' : urlparse (self .url ). hostname ,
722
723
'port' : 443 ,
723
724
'url' : self .url
724
725
},
@@ -744,7 +745,7 @@ def test_cloudant_bluemix_context_helper_with_iam(self):
744
745
'credentials' : {
745
746
'apikey' : self .iam_api_key ,
746
747
'username' : self .user ,
747
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
748
+ 'host' : urlparse (self .url ). hostname ,
748
749
'port' : 443 ,
749
750
'url' : self .url
750
751
},
@@ -766,7 +767,7 @@ def test_cloudant_bluemix_context_helper_raise_error_for_missing_iam_and_creds(s
766
767
instance_name = 'Cloudant NoSQL DB-lv'
767
768
vcap_services = {'cloudantNoSQLDB' : [{
768
769
'credentials' : {
769
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
770
+ 'host' : urlparse (self .url ). hostname ,
770
771
'port' : 443 ,
771
772
'url' : self .url
772
773
},
@@ -795,7 +796,7 @@ def test_cloudant_bluemix_dedicated_context_helper(self):
795
796
'credentials' : {
796
797
'username' : self .user ,
797
798
'password' : self .pwd ,
798
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
799
+ 'host' : urlparse (self .url ). hostname ,
799
800
'port' : 443 ,
800
801
'url' : self .url
801
802
},
@@ -818,10 +819,10 @@ def test_constructor_with_account(self):
818
819
"""
819
820
# Ensure that the client is new
820
821
del self .client
821
- self .client = Cloudant (self . user , self . pwd , account = self . account )
822
+ self .client = Cloudant (' user' , 'pass' , account = 'foo' )
822
823
self .assertEqual (
823
824
self .client .server_url ,
824
- 'https://{0} .cloudant.com' . format ( self . account )
825
+ 'https://foo .cloudant.com'
825
826
)
826
827
827
828
@skip_if_not_cookie_auth
@@ -835,7 +836,7 @@ def test_bluemix_constructor_with_legacy_creds(self):
835
836
'credentials' : {
836
837
'username' : self .user ,
837
838
'password' : self .pwd ,
838
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
839
+ 'host' : urlparse (self .url ). hostname ,
839
840
'port' : 443 ,
840
841
'url' : self .url
841
842
},
@@ -870,7 +871,7 @@ def test_bluemix_constructor_with_iam(self):
870
871
'credentials' : {
871
872
'apikey' : self .iam_api_key ,
872
873
'username' : self .user ,
873
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
874
+ 'host' : urlparse (self .url ). hostname ,
874
875
'port' : 443
875
876
},
876
877
'name' : instance_name
@@ -901,7 +902,7 @@ def test_bluemix_constructor_specify_instance_name(self):
901
902
'credentials' : {
902
903
'username' : self .user ,
903
904
'password' : self .pwd ,
904
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
905
+ 'host' : urlparse (self .url ). hostname ,
905
906
'port' : 443 ,
906
907
'url' : self .url
907
908
},
@@ -934,7 +935,7 @@ def test_bluemix_constructor_with_multiple_services(self):
934
935
{
935
936
'credentials' : {
936
937
'apikey' : '1234api' ,
937
- 'host' : '{0}.cloudant.com' . format (self .account ) ,
938
+ 'host' : urlparse (self .url ). hostname ,
938
939
'port' : 443 ,
939
940
'url' : self .url
940
941
},
@@ -973,10 +974,11 @@ def test_connect_headers(self):
973
974
"""
974
975
try :
975
976
self .client .connect ()
976
- self .assertEqual (
977
- self .client .r_session .headers ['X-Cloudant-User' ],
978
- self .account
979
- )
977
+ if (self .account ):
978
+ self .assertEqual (
979
+ self .client .r_session .headers ['X-Cloudant-User' ],
980
+ self .account
981
+ )
980
982
agent = self .client .r_session .headers .get ('User-Agent' )
981
983
ua_parts = agent .split ('/' )
982
984
self .assertEqual (len (ua_parts ), 6 )
@@ -1413,4 +1415,4 @@ def test_update_cors_configuration(self):
1413
1415
self .client .disconnect ()
1414
1416
1415
1417
if __name__ == '__main__' :
1416
- unittest .main ()
1418
+ unittest .main ()
0 commit comments