diff --git a/src/core/zowe/core_for_zowe_sdk/session.py b/src/core/zowe/core_for_zowe_sdk/session.py index 83efab4a..31e0018e 100644 --- a/src/core/zowe/core_for_zowe_sdk/session.py +++ b/src/core/zowe/core_for_zowe_sdk/session.py @@ -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: @@ -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") diff --git a/tests/unit/core/test_sdk_api.py b/tests/unit/core/test_sdk_api.py index 9398dd92..97f7d2e5 100644 --- a/tests/unit/core/test_sdk_api.py +++ b/tests/unit/core/test_sdk_api.py @@ -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 @@ -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."""