def test_authorization(self): """ Test that when it's given an invalid token, stronglib raises unauthorized exception when making an API request. """ token = "this_is_a_token" unauth_msg = "Get out of here!" data = {'data': 'some_data'} def authorization_required(request): if not ('Authorization' in request.headers and request.headers['Authorization'] == 'Token %s' % token): return (401, {}, json.dumps({'detail': unauth_msg})) return (200, {}, json.dumps(data)) responses.add_callback(responses.GET, self.url, callback=authorization_required, content_type='application/json') # If a wrong token is provided: with self.assertRaises(strongarm.StrongarmUnauthorized) as exp: strongarm.api_key = 'bad_token' request('get', self.url) self.assertEqual(401, exp.exception.status_code) self.assertEqual(unauth_msg, exp.exception.detail) # If the correct token is provided: strongarm.api_key = token self.assertEqual(request('get', self.url), data)
def test_parse_error(self): """ Test that when the request is not json, StrongarmException is raised. """ responses.add(responses.GET, self.url, status=200, content_type='text/html', body='text') with self.assertRaises(strongarm.StrongarmException) as exp: request('get', self.url)
def test_error_code_no_json(self): """ Test that when an HTTP error code is received with non-json data, StrongarmHttpError is raised with the status code and the raw data. """ msg = "<h1>Bad request</h1>" responses.add(responses.GET, self.url, status=400, content_type='text/html', body=msg) with self.assertRaises(strongarm.StrongarmHttpError) as exp: request('get', self.url) self.assertEqual(exp.exception.status_code, 400) self.assertEqual(exp.exception.detail, msg)
def test_error_code_wrong_json(self): """ Test that when an HTTP error code is received with non-json data as json, StrongarmHttpError is raised with the status code and the raw data. """ msg = "Bad request" responses.add(responses.GET, self.url, status=400, content_type='application/json', json=msg) with self.assertRaises(strongarm.StrongarmHttpError) as exp: request('get', self.url) self.assertEqual(exp.exception.status_code, 400) self.assertEqual(exp.exception.detail, json.dumps(msg))
def test_error_code_msg(self): """ Test that when an HTTP error code is received, StrongarmHttpError is raised with the status code and the provided error message. """ msg = "Something something does not exist." responses.add(responses.GET, self.url, status=404, content_type='application/json', body=json.dumps({'detail': msg})) with self.assertRaises(strongarm.StrongarmHttpError) as exp: request('get', self.url) self.assertEqual(exp.exception.status_code, 404) self.assertEqual(exp.exception.detail, msg)
def test_no_content(self): """ Test that when the request does not have any content, None is returned. """ responses.add(responses.GET, self.url, status=204, content_type='application/json') self.assertIsNone(request('get', self.url))
def test_version_header(self): """ Test that the API version is explicitly included in the Accept header. """ correct_header = 'application/json; version=%s' % strongarm.api_version def assert_header(request): self.assertIn('Accept', request.headers) self.assertEqual(request.headers['Accept'], correct_header) return (200, {}, '') responses.add_callback(responses.GET, self.url, callback=assert_header, content_type='application/json') # Make a random request to trigger the test in the above callback. request('get', self.url)