def test_base_api_uri_used_instead_of_default(self): # Requests to the default BASE_API_URI will noticeably fail by raising an # AssertionError. Requests to the new URL will respond HTTP 200. new_base_api_uri = 'http://example.com/api/v1/' # If any error is raised by the server, the test suite will never exit when # using Python 3. This strange technique is used to raise the errors # outside of the mocked server environment. errors_in_server = [] def server_response(request, uri, headers): try: self.assertEqual(uri, new_base_api_uri) except AssertionError as e: errors_in_server.append(e) return (200, headers, "") hp.register_uri(hp.GET, OAuthClient.BASE_API_URI, body=server_response) hp.register_uri(hp.GET, new_base_api_uri, body=server_response) client = OAuthClient(client_id, client_secret, access_token, refresh_token) with self.assertRaises(AssertionError): client._get() if errors_in_server: raise errors_in_server.pop() client2 = OAuthClient(client_id, client_secret, access_token, refresh_token, base_api_uri=new_base_api_uri) self.assertEqual(client2._get().status_code, 200)
def test_base_api_uri_used_instead_of_default(self): # Requests to the default BASE_API_URI will noticeably fail by raising an # AssertionError. Requests to the new URL will respond HTTP 200. new_base_api_uri = 'http://example.com/api/v1/' # If any error is raised by the server, the test suite will never exit when # using Python 3. This strange technique is used to raise the errors # outside of the mocked server environment. errors_in_server = [] def server_response(request, uri, headers): try: self.assertEqual(uri, new_base_api_uri) except AssertionError as e: errors_in_server.append(e) return (200, headers, "") hp.register_uri(hp.GET, OAuthClient.BASE_API_URI, body=server_response) hp.register_uri(hp.GET, new_base_api_uri, body=server_response) client = OAuthClient(client_id, client_secret, access_token, refresh_token) with self.assertRaises(AssertionError): client._get() if errors_in_server: raise errors_in_server.pop() client2 = OAuthClient( client_id, client_secret, access_token, refresh_token, base_api_uri=new_base_api_uri) self.assertEqual(client2._get().status_code, 200)
def test_response_handling(self): resp200 = lambda r, u, h: (200, h, '') resp400 = lambda r, u, h: (400, h, '') header_template = ( 'Bearer realm="Doorkeeper" error="{id}" error_description="{error}"' ) def resp401_expired(request, uri, headers): error_data = { 'id': 'invalid_token', 'error': 'The access token expired', } headers.update( {'www-authenticate': header_template.format(**error_data)}) return (401, headers, json.dumps(error_data)) def resp401_invalid(request, uri, headers): error_data = { 'id': 'invalid_token', 'error': 'The access token is invalid', } headers.update( {'www-authenticate': header_template.format(**error_data)}) return (401, headers, json.dumps(error_data)) def resp401_generic(request, uri, headers): error_data = { 'id': 'some_error', 'error': 'Some description', } headers.update( {'www-authenticate': header_template.format(**error_data)}) return (401, headers, json.dumps(error_data)) def resp401_nobody(request, uri, headers): return (401, headers, '') def resp401_header_expired(request, uri, headers): code, headers, _ = resp401_expired(request, uri, headers) return (code, headers, '') def resp401_header_invalid(request, uri, headers): code, headers, _ = resp401_invalid(request, uri, headers) return (code, headers, '') def resp401_header_generic(request, uri, headers): code, headers, _ = resp401_generic(request, uri, headers) return (code, headers, '') resp402 = lambda r, u, h: (402, h, '') hp.register_uri(hp.GET, re.compile('.*200$'), resp200) hp.register_uri(hp.GET, re.compile('.*400$'), resp400) hp.register_uri(hp.GET, re.compile('.*401_expired$'), resp401_expired) hp.register_uri(hp.GET, re.compile('.*401_invalid$'), resp401_invalid) hp.register_uri(hp.GET, re.compile('.*401_generic$'), resp401_generic) hp.register_uri(hp.GET, re.compile('.*401_nobody$'), resp401_nobody) hp.register_uri(hp.GET, re.compile('.*401_header_expired$'), resp401_header_expired) hp.register_uri(hp.GET, re.compile('.*401_header_invalid$'), resp401_header_invalid) hp.register_uri(hp.GET, re.compile('.*401_header_generic$'), resp401_header_generic) hp.register_uri(hp.GET, re.compile('.*402$'), resp402) client = OAuthClient(client_id, client_secret, access_token, refresh_token) assert client._get('200').status_code == 200 with self.assertRaises(APIError): client._get('400') with self.assertRaises(AuthenticationError): client._get('401_generic') with self.assertRaises(AuthenticationError): client._get('401_header_generic') with self.assertRaises(InvalidAccessToken): client._get('401_invalid') with self.assertRaises(InvalidAccessToken): client._get('401_header_invalid') with self.assertRaises(ExpiredAccessToken): client._get('401_expired') with self.assertRaises(ExpiredAccessToken): client._get('401_header_expired') with self.assertRaises(AuthenticationError): client._get('401_nobody') with self.assertRaises(TwoFactorTokenRequired): client._get('402')
def test_response_handling(self): resp200 = lambda r, u, h: (200, h, '') resp400 = lambda r, u, h: (400, h, '') header_template = ( 'Bearer realm="Doorkeeper" error="{id}" error_description="{error}"') def resp401_expired(request, uri, headers): error_data = { 'id': 'invalid_token', 'error': 'The access token expired', } headers.update({'www-authenticate': header_template.format(**error_data)}) return (401, headers, json.dumps(error_data)) def resp401_invalid(request, uri, headers): error_data = { 'id': 'invalid_token', 'error': 'The access token is invalid', } headers.update({'www-authenticate': header_template.format(**error_data)}) return (401, headers, json.dumps(error_data)) def resp401_generic(request, uri, headers): error_data = { 'id': 'some_error', 'error': 'Some description', } headers.update({'www-authenticate': header_template.format(**error_data)}) return (401, headers, json.dumps(error_data)) def resp401_nobody(request, uri, headers): return (401, headers, '') def resp401_header_expired(request, uri, headers): code, headers, _ = resp401_expired(request, uri, headers) return (code, headers, '') def resp401_header_invalid(request, uri, headers): code, headers, _ = resp401_invalid(request, uri, headers) return (code, headers, '') def resp401_header_generic(request, uri, headers): code, headers, _ = resp401_generic(request, uri, headers) return (code, headers, '') resp402 = lambda r, u, h: (402, h, '') hp.register_uri(hp.GET, re.compile('.*200$'), resp200) hp.register_uri(hp.GET, re.compile('.*400$'), resp400) hp.register_uri(hp.GET, re.compile('.*401_expired$'), resp401_expired) hp.register_uri(hp.GET, re.compile('.*401_invalid$'), resp401_invalid) hp.register_uri(hp.GET, re.compile('.*401_generic$'), resp401_generic) hp.register_uri(hp.GET, re.compile('.*401_nobody$'), resp401_nobody) hp.register_uri(hp.GET, re.compile('.*401_header_expired$'), resp401_header_expired) hp.register_uri(hp.GET, re.compile('.*401_header_invalid$'), resp401_header_invalid) hp.register_uri(hp.GET, re.compile('.*401_header_generic$'), resp401_header_generic) hp.register_uri(hp.GET, re.compile('.*402$'), resp402) client = OAuthClient(client_id, client_secret, access_token, refresh_token) assert client._get('200').status_code == 200 with self.assertRaises(APIError): client._get('400') with self.assertRaises(AuthenticationError): client._get('401_generic') with self.assertRaises(AuthenticationError): client._get('401_header_generic') with self.assertRaises(InvalidAccessToken): client._get('401_invalid') with self.assertRaises(InvalidAccessToken): client._get('401_header_invalid') with self.assertRaises(ExpiredAccessToken): client._get('401_expired') with self.assertRaises(ExpiredAccessToken): client._get('401_header_expired') with self.assertRaises(AuthenticationError): client._get('401_nobody') with self.assertRaises(TwoFactorTokenRequired): client._get('402')