Skip to content

Commit

Permalink
Update for cert
Browse files Browse the repository at this point in the history
Signed-off-by: pem70 <[email protected]>
  • Loading branch information
pem70 committed Jul 5, 2024
1 parent 4e6e7f3 commit 5767a78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ISession:
type: Optional[str] = None
tokenType: Optional[str] = None
tokenValue: Optional[str] = None
cert: Optional[str] = None
certKey: Optional[str] = None


class Session:
Expand Down Expand Up @@ -64,6 +66,10 @@ def __init__(self, props: dict) -> None:
elif props.get("tokenValue") is not None:
self.session.tokenValue = props.get("tokenValue")
self.session.type = session_constants.AUTH_TYPE_BEARER
elif props.get("cert") is not None and props.get("certKey") is not None:
self.session.cert = props.get("cert")
self.session.certKey = props.get("certKey")
self.session.type = session_constants.AUTH_TYPE_CERT_PEM
else:
self.session.type = session_constants.AUTH_TYPE_NONE
self.__logger.info("Authentication method not supplied")
Expand Down
29 changes: 14 additions & 15 deletions tests/unit/core/test_sdk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ def setUp(self):
common_props = {"host": "mock-url.com", "port": 443, "protocol": "https", "rejectUnauthorized": True}
self.basic_props = {**common_props, "user": "Username", "password": "Password"}
self.bearer_props = {**common_props, "tokenValue": "BearerToken"}
self.token_props = {
**common_props,
"tokenType": "MyToken",
"tokenValue": "TokenValue",
}
self.token_props = {**common_props, "tokenType": "MyToken", "tokenValue": "TokenValue"}
self.cert_props = {**common_props, "cert": "cert", "certKey": "certKey"}
self.default_url = "https://default-api.com/"

def test_object_should_be_instance_of_class(self):
"""Created object should be instance of SdkApi class."""
sdk_api = SdkApi(self.basic_props, self.default_url)
self.assertIsInstance(sdk_api, SdkApi)
@mock.patch('requests.Session.close')

@mock.patch("requests.Session.close")
def test_context_manager_closes_session(self, mock_close_request):

mock_close_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200)
with SdkApi(self.basic_props, self.default_url) as api:
pass
Expand All @@ -46,14 +43,16 @@ def test_session_no_host_logger(self, mock_logger_error: mock.MagicMock):
mock_logger_error.assert_called()
self.assertIn("Host", mock_logger_error.call_args[0][0])

@mock.patch("logging.Logger.error")
def test_session_no_authentication_logger(self, mock_logger_error: mock.MagicMock):
def test_should_handle_none_auth(self):
props = {"host": "test"}
try:
sdk_api = SdkApi(props, self.default_url)
except Exception:
mock_logger_error.assert_called()
self.assertIn("Authentication", mock_logger_error.call_args[0][0])
sdk_api = SdkApi(props, self.default_url)
self.assertEqual(sdk_api.session.password, None)

def test_should_handle_cert_auth(self):
props = self.cert_props
sdk_api = SdkApi(props, self.default_url)
self.assertEqual(sdk_api.session.cert, self.cert_props["cert"])
self.assertEqual(sdk_api.session.certKey, self.cert_props["certKey"])

def test_should_handle_basic_auth(self):
"""Created object should handle basic authentication."""
Expand Down

0 comments on commit 5767a78

Please sign in to comment.