def test_request_raises_on_connection_error(self, open_url_mock): open_url_mock.side_effect = ConnectionError('Unknown connection error') client = ManifoldApiClient('token-123') with self.assertRaises(ApiError) as context: client.request('test', 'endpoint') self.assertEqual('Error connecting to https://api.test.manifold.co/v1/endpoint: Unknown connection error', str(context.exception))
def test_request_sends_default_headers(self, open_url_mock): mock_fixture(open_url_mock, data='hello') client = ManifoldApiClient('token-123') client.request('test', 'endpoint') open_url_mock.assert_called_with('https://api.test.manifold.co/v1/endpoint', headers={'Accept': '*/*', 'Authorization': 'Bearer token-123'}, http_agent='python-manifold-ansible-1.0.0')
def test_get_credentials(self, open_url_mock): url = 'https://api.marketplace.manifold.co/v1/credentials?resource_id=rid-1' mock_fixture(open_url_mock, fixture=url) client = ManifoldApiClient('token-123') self.assertListEqual(API_FIXTURES[url], client.get_credentials(resource_id='rid-1')) open_url_mock.assert_called_with(url, headers={'Accept': '*/*', 'Authorization': 'Bearer token-123'}, http_agent='python-manifold-ansible-1.0.0')
def test_get_projects_filter_label(self, open_url_mock): url = 'https://api.marketplace.manifold.co/v1/projects?label=project-2' mock_fixture(open_url_mock, fixture=url) client = ManifoldApiClient('token-123') self.assertListEqual(API_FIXTURES[url], client.get_projects(label='project-2')) open_url_mock.assert_called_with(url, headers={'Accept': '*/*', 'Authorization': 'Bearer token-123'}, http_agent='python-manifold-ansible-1.0.0')
def test_get_teams_get_all(self, open_url_mock): url = 'https://api.identity.manifold.co/v1/teams' mock_fixture(open_url_mock, fixture=url) client = ManifoldApiClient('token-123') self.assertListEqual(API_FIXTURES[url], client.get_teams()) open_url_mock.assert_called_with(url, headers={'Accept': '*/*', 'Authorization': 'Bearer token-123'}, http_agent='python-manifold-ansible-1.0.0')
def test_request_raises_on_ssl_error(self, open_url_mock): open_url_mock.side_effect = SSLValidationError('SSL Error') client = ManifoldApiClient('token-123') with self.assertRaises(ApiError) as context: client.request('test', 'endpoint') self.assertEqual('Error validating the server\'s certificate for https://api.test.manifold.co/v1/endpoint: ' 'SSL Error', str(context.exception))
def test_request_raises_on_bad_url(self, open_url_mock): open_url_mock.side_effect = URLError('URL is invalid') client = ManifoldApiClient('token-123') with self.assertRaises(ApiError) as context: client.request('test', 'endpoint') self.assertEqual('Failed lookup url for https://api.test.manifold.co/v1/endpoint : <url' 'open error URL is invalid>', str(context.exception))
def test_request_raises_on_incorrect_json(self, open_url_mock): mock_fixture(open_url_mock, data='noJson', headers={'content-type': "application/json"}) client = ManifoldApiClient('token-123') with self.assertRaises(ApiError) as context: client.request('test', 'endpoint') self.assertEqual('JSON response can\'t be parsed while requesting https://api.test.manifold.co/v1/endpoint:\n' 'noJson', str(context.exception))
def test_request_passes_arbitrary_parameters(self, open_url_mock): mock_fixture(open_url_mock, data='hello') client = ManifoldApiClient('token-123') client.request('test', 'endpoint', use_proxy=False, timeout=5) open_url_mock.assert_called_with('https://api.test.manifold.co/v1/endpoint', headers={'Accept': '*/*', 'Authorization': 'Bearer token-123'}, http_agent='python-manifold-ansible-1.0.0', use_proxy=False, timeout=5)
def test_request_processes_parameterized_headers(self, open_url_mock): mock_fixture(open_url_mock, data='hello') client = ManifoldApiClient('token-123') client.request('test', 'endpoint', headers={'X-HEADER': 'MANIFOLD'}) open_url_mock.assert_called_with('https://api.test.manifold.co/v1/endpoint', headers={'Accept': '*/*', 'Authorization': 'Bearer token-123', 'X-HEADER': 'MANIFOLD'}, http_agent='python-manifold-ansible-1.0.0')
def test_request_raises_on_status_500(self, open_url_mock): open_url_mock.side_effect = HTTPError('https://api.test.manifold.co/v1/endpoint', 500, 'Server error', {}, six.StringIO('ERROR')) client = ManifoldApiClient('token-123') with self.assertRaises(ApiError) as context: client.request('test', 'endpoint') self.assertEqual('Server returned: HTTP Error 500: Server error while requesting ' 'https://api.test.manifold.co/v1/endpoint:\nERROR', str(context.exception))
def test_get_resources_filter_team_and_project(self, open_url_mock): url = 'https://api.marketplace.manifold.co/v1/resources?team_id=tid-1&project_id=pid-1' mock_fixture(open_url_mock, fixture=url) client = ManifoldApiClient('token-123') self.assertListEqual(API_FIXTURES[url], client.get_resources(team_id='tid-1', project_id='pid-1')) args, kwargs = open_url_mock.call_args url_called = args[0] # Dict order is not guaranteed, so an url may have querystring parameters order randomized self.assertIn('team_id=tid-1', url_called) self.assertIn('project_id=pid-1', url_called)
def test_request_streams_text(self, open_url_mock): mock_fixture(open_url_mock, data='hello', headers={'content-type': "text/plain"}) client = ManifoldApiClient('token-123') self.assertEqual('hello', client.request('test', 'endpoint'))
def test_request_decodes_json(self, open_url_mock): mock_fixture(open_url_mock, fixture='https://api.marketplace.manifold.co/v1/resources') client = ManifoldApiClient('token-123') self.assertIsInstance(client.request('marketplace', 'resources'), list)