예제 #1
0
    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)
예제 #2
0
    def test_logout_failure(self, mock_delete, mock_post):
        client = Impersonator()

        credentials = {'username': '******', 'password': '******'}
        login_endpoint = "http://%s:%d/%s" % (client.host, client.port,
                                              client.token_endpoint)
        logout_endpoint = "%s/%s" % (login_endpoint, MOCK_TOKEN)

        token = client.login(**credentials)

        with self.assertRaises(Exception) as context:
            client.logout()

        self.assertIn(mock.call(logout_endpoint), mock_delete.call_args_list)
        self.assertEqual(MOCK_ERROR, str(context.exception))