def test_sanitize_for_archive_token(self): """Test whether the sanitize method works properly for token-signed requests""" payload = { 'limit': 2, 'order': 'changeddate', 'token': '786-OLaWfBisMY', 'include_fields': '_all', 'last_change_time': '1970-01-01T00:00:00Z' } url, headers, payload = BugzillaRESTClient.sanitize_for_archive( None, None, payload) with self.assertRaises(KeyError): payload.pop('token')
def test_sanitize_for_archive_login(self): """Test whether the sanitize method works properly when login""" url = "http://example.com" headers = "headers-information" payload = {'login': '******', 'password': '******'} s_url, s_headers, s_payload = BugzillaRESTClient.sanitize_for_archive( url, headers, copy.deepcopy(payload)) payload.pop('login') payload.pop('password') self.assertEqual(url, s_url) self.assertEqual(headers, s_headers) self.assertEqual(payload, s_payload)
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_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_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_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) # 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_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) httpretty.register_uri(httpretty.GET, BUGZILLA_VERSION_URL, body='{"version":"5.1.2"}', status=200) with self.assertRaises(BackendError): _ = BugzillaRESTClient(BUGZILLA_SERVER_URL, user='******', password='******')
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)