def test_login_execute_logout(self, mock_delete, mock_post): client = Impersonator() credentials = {'username': '******', 'password': '******'} payload = {'token': MOCK_TOKEN, 'command': MOCK_COMMAND_SUCCESS} login_endpoint = "http://%s:%d/%s" % (client.host, client.port, client.token_endpoint) command_endpoint = "http://%s:%d/%s" % (client.host, client.port, client.command_endpoint) logout_endpoint = "%s/%s" % (login_endpoint, MOCK_TOKEN) token = client.login(**credentials) self.assertIn(mock.call(login_endpoint, json=credentials), mock_post.call_args_list) self.assertEqual(token, MOCK_TOKEN) data = client.execute(MOCK_COMMAND_SUCCESS) self.assertIn(mock.call(command_endpoint, json=payload), mock_post.call_args_list) self.assertEqual(data, MOCK_OUTPUT) client.logout() self.assertIn(mock.call(logout_endpoint), mock_delete.call_args_list) self.assertEqual(client.token, None)
def _get_scheduler(config, token): impersonator = Impersonator(config["impersonator"]["host"], config["impersonator"]["port"]) scheduler_name = config["scheduler"]["name"] Scheduler = schedulers[scheduler_name]["scheduler"] scheduler = Scheduler(config, impersonator) scheduler.set_credentials(token) return scheduler
def test_login_failure(self, mock_post): client = Impersonator() credentials = {'username': '******', 'password': '******'} endpoint = "http://%s:%d/%s" % (client.host, client.port, client.token_endpoint) with self.assertRaises(Exception) as context: token = client.login(**credentials) self.assertIn(mock.call(endpoint, json=credentials), mock_post.call_args_list) self.assertEqual(MOCK_ERROR, str(context.exception))
def test_login_with_private_key(self, mock_post): client = Impersonator() credentials = {'username': '******', 'privateKey': 'privateKey'} login_endpoint = "http://%s:%d/%s" % (client.host, client.port, client.token_endpoint) token = client.login(username=credentials['username'], private_key=credentials['privateKey']) self.assertIn(mock.call(login_endpoint, json=credentials), mock_post.call_args_list) self.assertEqual(token, MOCK_TOKEN)
def test_login_without_password_or_key_throws_exception(self): client = Impersonator() credentials = {'username': '******'} endpoint = "http://%s:%d/%s" % (client.host, client.port, client.token_endpoint) with self.assertRaises(Exception) as context: token = client.login(**credentials) self.assertEqual( "Either password or private_key must be provided to login", str(context.exception))
def configure_daemon(config, Impersonator, SchedulerDaemon): scheduler_name = config["scheduler"]["name"] Scheduler = schedulers[scheduler_name]["scheduler"] username = config["scheduler"]["daemon_credentials"]["username"] password = config["scheduler"]["daemon_credentials"]["password"] impersonator = Impersonator(config["impersonator"]["host"], config["impersonator"]["port"]) cache = config["cache"] scheduler = Scheduler(config, impersonator) service_token = impersonator.login(username, password) poll_interval = config["scheduler"]["poll_interval"] impersonator.token = None return SchedulerDaemon(PID_FILE, cache, scheduler, service_token, poll_interval)
def test_execute_failure(self, mock_post): client = Impersonator() credentials = {'username': '******', 'password': '******'} payload = {'token': MOCK_TOKEN, 'command': MOCK_COMMAND_FAILURE} login_endpoint = "http://%s:%d/%s" % (client.host, client.port, client.token_endpoint) command_endpoint = "http://%s:%d/%s" % (client.host, client.port, client.command_endpoint) token = client.login(**credentials) with self.assertRaises(Exception) as context: client.execute(MOCK_COMMAND_FAILURE) self.assertIn(mock.call(command_endpoint, json=payload), mock_post.call_args_list) self.assertEqual(MOCK_ERROR, str(context.exception))