def test_query_api_key_and_multiple_authentication_can_be_combined( token_cache, responses: RequestsMock): api_key_auth = requests_auth.QueryApiKey("my_provided_api_key") api_key_auth2 = requests_auth.QueryApiKey("my_provided_api_key2", query_parameter_name="api_key2") api_key_auth3 = requests_auth.HeaderApiKey("my_provided_api_key3", header_name="X-Api-Key3") # Mock a dummy response responses.add(responses.GET, "http://authorized_only") # Send a request to this dummy URL with authentication response = requests.get("http://authorized_only", auth=api_key_auth + (api_key_auth2 + api_key_auth3)) # Return headers received on this dummy URL assert (response.request.path_url == "/?api_key=my_provided_api_key&api_key2=my_provided_api_key2") assert response.request.headers.get("X-Api-Key3") == "my_provided_api_key3"
def test_query_api_key_can_be_sent_in_a_custom_field_name( responses: RequestsMock): auth = requests_auth.QueryApiKey("my_provided_api_key", "X-API-QUERY-KEY") assert get_query_args(responses, auth) == "/?X-API-QUERY-KEY=my_provided_api_key"
def test_query_api_key_is_sent_in_api_key_by_default(responses: RequestsMock): auth = requests_auth.QueryApiKey("my_provided_api_key") assert get_query_args(responses, auth) == "/?api_key=my_provided_api_key"
def test_query_api_key_requires_an_api_key(): with pytest.raises(Exception) as exception_info: requests_auth.QueryApiKey(None) assert str(exception_info.value) == "API Key is mandatory."
def test_query_api_key_can_be_sent_in_a_custom_field_name(self): auth = requests_auth.QueryApiKey('my_provided_api_key', 'X-API-QUERY-KEY') self.assertEqual(get_query_args(auth).get('X-API-QUERY-KEY'), 'my_provided_api_key')
def test_query_api_key_is_sent_in_api_key_by_default(self): auth = requests_auth.QueryApiKey('my_provided_api_key') self.assertEqual(get_query_args(auth).get('api_key'), 'my_provided_api_key')
def test_query_api_key_requires_an_api_key(self): with self.assertRaises(Exception) as cm: requests_auth.QueryApiKey(None) self.assertEqual('API Key is mandatory.', str(cm.exception))