def test_init(self): env = TNCOEnvironment(host='test', port=80, protocol='https', secure=True, username='******', password='******') session_config = env.create_session_config() session = LmSession(session_config) self.assertEqual(session.env, env) self.assertEqual(session.username, 'user') self.assertEqual(session.password, 'secret')
def test_security_ctrl_builds_client_with_legacy_auth_on_default_address(self, mock_client_builder_init): env = TNCOEnvironment(address='http://api:80', secure=True, username='******', password='******') session_config = env.create_session_config() session = LmSession(session_config) driver = session.descriptor_driver #Force LmSecurityCtrl to be created mock_client_builder = mock_client_builder_init.return_value mock_client_builder.legacy_user_pass_auth.assert_called_once_with(username='******', password='******', legacy_auth_address='http://api:80') mock_client_builder.user_pass_auth.assert_not_called() mock_client_builder.client_credentials_auth.assert_not_called() mock_client_builder.zen_api_key_auth.assert_not_called()
def test_security_ctrl_builds_client_with_token_auth(self, mock_client_builder_init): env = TNCOEnvironment(address='http://api:80', secure=True, auth_mode='token', token='123') session_config = env.create_session_config() session = LmSession(session_config) driver = session.descriptor_driver #Force LmSecurityCtrl to be created mock_client_builder = mock_client_builder_init.return_value mock_client_builder.address.assert_called_once_with(None) mock_client_builder.legacy_user_pass_auth.assert_not_called() mock_client_builder.user_pass_auth.assert_not_called() mock_client_builder.client_credentials_auth.assert_not_called() mock_client_builder.token_auth.assert_called_once_with(token='123')
def test_security_ctrl_builds_client_with_client_auth(self, mock_client_builder_init): env = TNCOEnvironment(address='http://api:80', secure=True, client_id='UserClient', client_secret='c_secret', auth_address='http://auth:81') session_config = env.create_session_config() session = LmSession(session_config) driver = session.descriptor_driver #Force LmSecurityCtrl to be created mock_client_builder = mock_client_builder_init.return_value mock_client_builder.address.assert_called_once_with('http://auth:81') mock_client_builder.legacy_user_pass_auth.assert_not_called() mock_client_builder.user_pass_auth.assert_not_called() mock_client_builder.client_credentials_auth.assert_called_once_with(client_id='UserClient', client_secret='c_secret') mock_client_builder.zen_api_key_auth.assert_not_called()
def test_infrastructure_keys_driver_with_security(self, infrastructure_keys_driver_init, mock_security_ctrl_init): session = LmSession(LmSessionConfig(TNCOEnvironment(host='test', port=80, protocol='https', secure=True, username='******', auth_host='auth', auth_port=81, auth_protocol='http'), 'user', 'secret', auth_mode='oauth')) driver = session.infrastructure_keys_driver mock_security_ctrl_init.assert_called_once_with('http://auth:81', username='******', password='******', client_id=None, client_secret=None, token=None, api_key=None, auth_mode='oauth') infrastructure_keys_driver_init.assert_called_once_with('https://test:80', mock_security_ctrl_init.return_value) self.assertEqual(driver, infrastructure_keys_driver_init.return_value)
def test_infrastructure_keys_driver(self, infrastructure_keys_driver_init): session = LmSession(LmSessionConfig(TNCOEnvironment(host='test', port=80, protocol='https'), None)) driver = session.infrastructure_keys_driver infrastructure_keys_driver_init.assert_called_once_with('https://test:80', None) self.assertEqual(driver, infrastructure_keys_driver_init.return_value)
def test_deployment_location_driver(self, deployment_location_driver_init): session = LmSession(LmSessionConfig(TNCOEnvironment(host='test', port=80, protocol='https'), None)) driver = session.deployment_location_driver deployment_location_driver_init.assert_called_once_with('https://test:80', None) self.assertEqual(driver, deployment_location_driver_init.return_value)
def test_init_fails_without_config(self): with self.assertRaises(ValueError) as context: LmSession(None) self.assertEqual(str(context.exception), 'config not provided to session')