def test_auth_token_call(self): """Test whether the API token is included on the calls when it was set""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_bugs.json') # Set up a mock HTTP server httpretty.register_uri(httpretty.GET, BUGZILLA_LOGIN_URL, body='{"token": "786-OLaWfBisMY", "id": "786"}', status=200) httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_URL, body=body, status=200) httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"version":"5.1.2"}', status=200) # Test API token login client = BugzillaRESTClient(BUGZILLA_SERVER_URL, user='******', password='******') self.assertEqual(client.api_token, '786-OLaWfBisMY') # Check whether it is included on the calls _ = client.bugs() # Check request params expected = { 'last_change_time': ['1970-01-01T00:00:00Z'], 'limit': ['500'], 'order': ['changeddate'], 'include_fields': ['_all'], 'token': ['786-OLaWfBisMY'] } req = httpretty.last_request() self.assertDictEqual(req.querystring, expected) # Test API token initialization client = BugzillaRESTClient(BUGZILLA_SERVER_URL, api_token='ABCD') _ = client.bugs() expected = { 'last_change_time': ['1970-01-01T00:00:00Z'], 'limit': ['500'], 'order': ['changeddate'], 'include_fields': ['_all'], 'token': ['ABCD'] } req = httpretty.last_request() self.assertDictEqual(req.querystring, expected)
def test_bugs(self): """Test bugs API call""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_bugs.json') httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_URL, body=body, status=200) httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"version":"5.1.2"}', status=200) # Call API client = BugzillaRESTClient(BUGZILLA_SERVER_URL) response = client.bugs() self.assertEqual(response, body) # Check request params expected = { 'last_change_time': ['1970-01-01T00:00:00Z'], 'limit': ['500'], 'order': ['changeddate'], 'include_fields': ['_all'] } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/rest/bug') self.assertDictEqual(req.querystring, expected) # Call API with parameters from_date = datetime.datetime(2016, 6, 7, 0, 0, 0) client = BugzillaRESTClient(BUGZILLA_SERVER_URL) response = client.bugs(from_date=from_date, offset=100, max_bugs=5) self.assertEqual(response, body) expected = { 'last_change_time': ['2016-06-07T00:00:00Z'], 'offset': ['100'], 'limit': ['5'], 'order': ['changeddate'], 'include_fields': ['_all'] } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/rest/bug') self.assertDictEqual(req.querystring, expected)
def test_init(self): """Test initialization""" client = BugzillaRESTClient(BUGZILLA_SERVER_URL) self.assertEqual(client.base_url, BUGZILLA_SERVER_URL) self.assertEqual(client.api_token, None) self.assertTrue(client.ssl_verify) client = BugzillaRESTClient(BUGZILLA_SERVER_URL, ssl_verify=False) self.assertEqual(client.base_url, BUGZILLA_SERVER_URL) self.assertEqual(client.api_token, None) self.assertFalse(client.ssl_verify)
def test_init_auth(self): """Test initialization with authentication""" # Set up a mock HTTP server httpretty.register_uri(httpretty.GET, BUGZILLA_LOGIN_URL, body='{"token": "786-OLaWfBisMY", "id": "786"}', status=200) client = BugzillaRESTClient(BUGZILLA_SERVER_URL, user='******', password='******') self.assertEqual(client.api_token, '786-OLaWfBisMY') # Check request params expected = { 'login': ['*****@*****.**'], 'password': ['1234'], } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/rest/login') self.assertEqual(req.querystring, expected)
def test_attachments(self): """Test attachments API call""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_bugs_attachments.json') httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_ATTACHMENTS_1273442_URL, body=body, status=200) # Call API client = BugzillaRESTClient(BUGZILLA_SERVER_URL) response = client.attachments('1273442', '1273439') self.assertEqual(response, body) # Check request params expected = { 'ids': ['1273442', '1273439'], 'exclude_fields': ['data'] } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/rest/bug/1273442/attachment') self.assertDictEqual(req.querystring, expected)
def test_history(self): """Test history API call""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_bugs_history.json') httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_HISTORY_1273442_URL, body=body, status=200) # Call API client = BugzillaRESTClient(BUGZILLA_SERVER_URL) response = client.history('1273442', '1273439') self.assertEqual(response, body) # Check request params expected = { 'ids': ['1273442', '1273439'] } req = httpretty.last_request() self.assertEqual(req.method, 'GET') self.assertRegex(req.path, '/rest/bug/1273442/history') self.assertDictEqual(req.querystring, expected)
def test_user_agent_header(self): """Test if the User-Agent header is included on every API call""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_bugs_history.json') httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_HISTORY_1273442_URL, body=body, status=200) httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"version":"5.1.2"}', status=200) # Call API client = BugzillaRESTClient(BUGZILLA_SERVER_URL) response = client.history('1273442', '1273439') self.assertEqual(response, body) # Check request params expected = { 'ids': ['1273442', '1273439'] } req = httpretty.last_request() user_agent = req.headers['User-Agent'] self.assertEqual(user_agent.startswith('Perceval/'), True)
def test_bugzilla_api_key(self): """Test Bugzilla API key authentication""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_bugs.json') httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"version":"5.1.2"}', status=200) httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_URL, body=body, status=200) # Test API token login client = BugzillaRESTClient(BUGZILLA_SERVER_URL, api_key='abcdef') # Check whether it is included on the calls _ = client.bugs() # Check the URL containing the API key req = httpretty.last_request() expected_url = 'http://example.com/rest/bug?last_change_time=1970-01-01T00%3A00%3A00Z&limit=500&' \ 'order=changeddate&include_fields=_all&api_key=abcdef' self.assertEqual(req.url, expected_url) # Check the header that does not contain the API key expected_header = "Authorization: Bearer abcdef" self.assertNotIn(expected_header, req.raw_headers.decode("utf-8"))
def test_init(self): """Test initialization""" httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"version":"5.1.2"}', status=200) client = BugzillaRESTClient(BUGZILLA_SERVER_URL) self.assertEqual(client.base_url, BUGZILLA_SERVER_URL) self.assertEqual(client.api_token, None) self.assertEqual(client.api_key, None) self.assertTrue(client.ssl_verify) client = BugzillaRESTClient(BUGZILLA_SERVER_URL, ssl_verify=False) self.assertEqual(client.base_url, BUGZILLA_SERVER_URL) self.assertEqual(client.api_token, None) self.assertEqual(client.api_key, None) self.assertFalse(client.ssl_verify)
def test_invalid_auth(self): """Test whether it fails when the authentication goes wrong""" # Set up a mock HTTP server httpretty.register_uri(httpretty.GET, BUGZILLA_LOGIN_URL, body="401 Client Error: Authorization Required", status=401) with self.assertRaises(BackendError): _ = BugzillaRESTClient(BUGZILLA_SERVER_URL, user='******', password='******')
def test_check_bugzilla_type(self): """Test Check if the type of the bugzilla is standard or custom""" # Set up a mock HTTP server httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"version":"5.1.2"}', status=200) # Test standard Bugzilla client = BugzillaRESTClient(BUGZILLA_SERVER_URL, api_key='abcdef') self.assertFalse(client.bugzilla_custom) # Set up a mock HTTP server httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"code":32000,"error":true,"message":"You have attempted to access..."}', status=200) # Test custom Bugzilla version https://bugzilla.redhat.com client = BugzillaRESTClient(BUGZILLA_SERVER_URL, api_key='abcdef') self.assertTrue(client.bugzilla_custom)
def test_rest_error(self): """Test if an exception is raised when the server returns an error""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_error.json') httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_URL, body=body, status=200) client = BugzillaRESTClient(BUGZILLA_SERVER_URL) with self.assertRaises(BugzillaRESTError) as e: _ = client.call('bug', {}) self.assertEqual(e.exception.code, 32000) self.assertEqual(e.exception.error, "API key authentication is required.")
def test_bugzilla_api_key_custom_instance(self): """Test Bugzilla API Key authentication on a custom instance https://bugzilla.redhat.com""" # Set up a mock HTTP server body = read_file('data/bugzilla/bugzilla_rest_bugs.json') httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"code":32000,"error":true,"message":"You have attempted to access..."}', status=200) httpretty.register_uri(httpretty.GET, BUGZILLA_BUGS_URL, body=body, status=200) # Test API token login client = BugzillaRESTClient(BUGZILLA_SERVER_URL, api_key='abcdef') self.assertEqual(client.api_key, 'abcdef') # Check whether it is included on the calls _ = client.bugs() # Check request params expected = { 'last_change_time': ['1970-01-01T00:00:00Z'], 'limit': ['500'], 'order': ['changeddate'], 'include_fields': ['_all'] } req = httpretty.last_request() self.assertDictEqual(req.querystring, expected) # Check URL that does not contain the API key expected_url = 'http://example.com/rest/bug?last_change_time=1970-01-01T00%3A00%3A00Z&limit=500&' \ 'order=changeddate&include_fields=_all' self.assertEqual(req.url, expected_url) # Check the header containing the API key expected_header = "Authorization: Bearer abcdef" self.assertIn(expected_header, req.raw_headers.decode("utf-8"))
def test_init(self): """Test initialization""" client = BugzillaRESTClient(BUGZILLA_SERVER_URL) self.assertEqual(client.base_url, BUGZILLA_SERVER_URL) self.assertEqual(client.api_token, None)