Skip to content

Commit 60f49f2

Browse files
committed
feat: add more type check for the disable_ssl_verificaton parameter with tests
1 parent d71c2b8 commit 60f49f2

9 files changed

+116
-3
lines changed

ibm_cloud_sdk_core/authenticators/container_authenticator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class ContainerAuthenticator(IAMRequestBasedAuthenticator):
5656
from the endpoint specified by the url.
5757
5858
Raises:
59+
TypeError: The `disable_ssl_verification` is not a bool.
5960
ValueError: Neither of iam_profile_name or iam_profile_idk are set,
6061
or client_id, and/or client_secret are not valid for IAM token requests.
6162
"""
@@ -72,10 +73,15 @@ def __init__(self,
7273
scope: Optional[str] = None,
7374
proxies: Optional[Dict[str, str]] = None,
7475
headers: Optional[Dict[str, str]] = None) -> None:
76+
# Check the type of `disable_ssl_verification`. Must be a bool.
77+
if not isinstance(disable_ssl_verification, bool):
78+
raise TypeError('disable_ssl_verification must be a bool')
79+
7580
self.token_manager = ContainerTokenManager(
7681
cr_token_filename=cr_token_filename, iam_profile_name=iam_profile_name, iam_profile_id=iam_profile_id,
7782
url=url, client_id=client_id, client_secret=client_secret,
7883
disable_ssl_verification=disable_ssl_verification, scope=scope, proxies=proxies, headers=headers)
84+
7985
self.validate()
8086

8187
def validate(self) -> None:

ibm_cloud_sdk_core/authenticators/cp4d_authenticator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class CloudPakForDataAuthenticator(Authenticator):
4747
token_manager (CP4DTokenManager): Retrieves and manages CP4D tokens from the endpoint specified by the url.
4848
4949
Raises:
50+
TypeError: The `disable_ssl_verification` is not a bool.
5051
ValueError: The username, password/apikey, and/or url are not valid for CP4D token requests.
5152
"""
5253
authenticationdict = 'cp4d'
@@ -60,9 +61,14 @@ def __init__(self,
6061
disable_ssl_verification: bool = False,
6162
headers: Optional[Dict[str, str]] = None,
6263
proxies: Optional[Dict[str, str]] = None) -> None:
64+
# Check the type of `disable_ssl_verification`. Must be a bool.
65+
if not isinstance(disable_ssl_verification, bool):
66+
raise TypeError('disable_ssl_verification must be a bool')
67+
6368
self.token_manager = CP4DTokenManager(
6469
username=username, password=password, apikey=apikey, url=url,
6570
disable_ssl_verification=disable_ssl_verification, headers=headers, proxies=proxies)
71+
6672
self.validate()
6773

6874
def validate(self) -> None:
@@ -116,6 +122,9 @@ def set_disable_ssl_verification(self, status: bool = False) -> None:
116122
117123
Args:
118124
status: Set to true in order to disable SSL certificate verification. Defaults to False.
125+
126+
Raises:
127+
TypeError: The `status` is not a bool.
119128
"""
120129
self.token_manager.set_disable_ssl_verification(status)
121130

ibm_cloud_sdk_core/authenticators/iam_authenticator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class IAMAuthenticator(IAMRequestBasedAuthenticator):
5151
token_manager (IAMTokenManager): Retrieves and manages IAM tokens from the endpoint specified by the url.
5252
5353
Raises:
54+
TypeError: The `disable_ssl_verification` is not a bool.
5455
ValueError: The apikey, client_id, and/or client_secret are not valid for IAM token requests.
5556
"""
5657
authentication_type = 'iam'
@@ -65,10 +66,15 @@ def __init__(self,
6566
headers: Optional[Dict[str, str]] = None,
6667
proxies: Optional[Dict[str, str]] = None,
6768
scope: Optional[str] = None) -> None:
69+
# Check the type of `disable_ssl_verification`. Must be a bool.
70+
if not isinstance(disable_ssl_verification, bool):
71+
raise TypeError('disable_ssl_verification must be a bool')
72+
6873
self.token_manager = IAMTokenManager(
6974
apikey, url=url, client_id=client_id, client_secret=client_secret,
7075
disable_ssl_verification=disable_ssl_verification,
7176
headers=headers, proxies=proxies, scope=scope)
77+
7278
self.validate()
7379

7480
def validate(self) -> None:

ibm_cloud_sdk_core/authenticators/iam_request_based_authenticator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ def set_disable_ssl_verification(self, status: bool = False) -> None:
8080
"""Set the flag that indicates whether verification of the server's SSL certificate should be
8181
disabled or not. Defaults to False.
8282
83-
Keyword Arguments:
84-
status: Headers to be sent with every IAM token request. Defaults to None.
83+
Args:
84+
status: Headers to be sent with every IAM token request. Defaults to None
85+
86+
Raises:
87+
TypeError: The `status` is not a bool.
8588
"""
8689
self.token_manager.set_disable_ssl_verification(status)
8790

ibm_cloud_sdk_core/token_managers/token_manager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,14 @@ def set_disable_ssl_verification(self, status: bool = False) -> None:
9191
9292
Args:
9393
status: the flag to be used for determining status.
94+
95+
Raises:
96+
TypeError: The `status` is not a bool.
9497
"""
95-
self.disable_ssl_verification = status
98+
if isinstance(status, bool):
99+
self.disable_ssl_verification = status
100+
else:
101+
raise TypeError('status must be a bool')
96102

97103
def paced_request_token(self) -> None:
98104
"""

test/test_container_authenticator.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ def test_container_authenticator():
5151
assert authenticator.token_manager.proxies == {'dummy': 'proxies'}
5252

5353

54+
def test_disable_ssl_verification():
55+
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123', disable_ssl_verification=True)
56+
assert authenticator.token_manager.disable_ssl_verification is True
57+
58+
authenticator.set_disable_ssl_verification(False)
59+
assert authenticator.token_manager.disable_ssl_verification is False
60+
61+
62+
def test_invalid_disable_ssl_verification_type():
63+
with pytest.raises(TypeError) as err:
64+
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123', disable_ssl_verification='True')
65+
assert str(err.value) == 'disable_ssl_verification must be a bool'
66+
67+
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123')
68+
assert authenticator.token_manager.disable_ssl_verification is False
69+
70+
with pytest.raises(TypeError) as err:
71+
authenticator.set_disable_ssl_verification('True')
72+
assert str(err.value) == 'status must be a bool'
73+
74+
5475
def test_container_authenticator_with_scope():
5576
authenticator = ContainerAuthenticator(iam_profile_name='iam-user-123', scope='scope1 scope2')
5677
assert authenticator is not None

test/test_cp4d_authenticator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ def test_cp4d_authenticator():
3838
assert authenticator.token_manager.proxies == {'dummy': 'proxies'}
3939

4040

41+
def test_disable_ssl_verification():
42+
authenticator = CloudPakForDataAuthenticator(
43+
'my_username', 'my_password', 'http://my_url', disable_ssl_verification=True)
44+
assert authenticator.token_manager.disable_ssl_verification is True
45+
46+
authenticator.set_disable_ssl_verification(False)
47+
assert authenticator.token_manager.disable_ssl_verification is False
48+
49+
50+
def test_invalid_disable_ssl_verification_type():
51+
with pytest.raises(TypeError) as err:
52+
authenticator = CloudPakForDataAuthenticator(
53+
'my_username', 'my_password', 'http://my_url', disable_ssl_verification='True')
54+
assert str(err.value) == 'disable_ssl_verification must be a bool'
55+
56+
authenticator = CloudPakForDataAuthenticator(
57+
'my_username', 'my_password', 'http://my_url')
58+
assert authenticator.token_manager.disable_ssl_verification is False
59+
60+
with pytest.raises(TypeError) as err:
61+
authenticator.set_disable_ssl_verification('True')
62+
assert str(err.value) == 'status must be a bool'
63+
64+
4165
def test_cp4d_authenticator_validate_failed():
4266
with pytest.raises(ValueError) as err:
4367
CloudPakForDataAuthenticator('my_username', None, 'my_url')

test/test_iam_authenticator.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ def test_iam_authenticator():
4545
assert authenticator.token_manager.disable_ssl_verification
4646

4747

48+
def test_disable_ssl_verification():
49+
authenticator = IAMAuthenticator(apikey='my_apikey', disable_ssl_verification=True)
50+
assert authenticator.token_manager.disable_ssl_verification is True
51+
52+
authenticator.set_disable_ssl_verification(False)
53+
assert authenticator.token_manager.disable_ssl_verification is False
54+
55+
56+
def test_invalid_disable_ssl_verification_type():
57+
with pytest.raises(TypeError) as err:
58+
authenticator = IAMAuthenticator(apikey='my_apikey', disable_ssl_verification='True')
59+
assert str(err.value) == 'disable_ssl_verification must be a bool'
60+
61+
authenticator = IAMAuthenticator(apikey='my_apikey')
62+
assert authenticator.token_manager.disable_ssl_verification is False
63+
64+
with pytest.raises(TypeError) as err:
65+
authenticator.set_disable_ssl_verification('True')
66+
assert str(err.value) == 'status must be a bool'
67+
68+
4869
def test_iam_authenticator_with_scope():
4970
authenticator = IAMAuthenticator(apikey='my_apikey', scope='scope1 scope2')
5071
assert authenticator is not None

test/test_token_manager.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,20 @@ def test_request_raises_for_non_2xx(request): # pylint: disable=unused-argument
6565
mock_token_manager = MockTokenManager(url="https://example.com", disable_ssl_verification=True)
6666
with pytest.raises(ApiException):
6767
mock_token_manager.request_token()
68+
69+
70+
def test_set_disable_ssl_verification_success():
71+
token_manager = MockTokenManager(None)
72+
assert token_manager.disable_ssl_verification is False
73+
74+
token_manager.set_disable_ssl_verification(True)
75+
assert token_manager.disable_ssl_verification is True
76+
77+
78+
def test_set_disable_ssl_verification_fail():
79+
token_manager = MockTokenManager(None)
80+
81+
with pytest.raises(TypeError) as err:
82+
token_manager.set_disable_ssl_verification('True')
83+
assert str(err.value) == 'status must be a bool'
84+
assert token_manager.disable_ssl_verification is False

0 commit comments

Comments
 (0)