diff --git a/testutils/__init__.py b/testutils/__init__.py index 4cfb4d9d..ad4d21dd 100644 --- a/testutils/__init__.py +++ b/testutils/__init__.py @@ -1,4 +1,13 @@ -from .mock_auth import get_mock_api_key_auth, get_mock_jwt_auth +from .mock_auth import ( + get_base64_encoded_api_key_and_secret, + get_mock_api_key_auth, + get_mock_jwt_auth, +) from .testutils import build_response -__all__ = ['build_response', 'get_mock_api_key_auth', 'get_mock_jwt_auth'] +__all__ = [ + 'build_response', + 'get_mock_api_key_auth', + 'get_mock_jwt_auth', + 'get_base64_encoded_api_key_and_secret', +] diff --git a/testutils/mock_auth.py b/testutils/mock_auth.py index ac724e81..4975ed4f 100644 --- a/testutils/mock_auth.py +++ b/testutils/mock_auth.py @@ -1,7 +1,11 @@ +from base64 import b64encode from os.path import dirname, join from vonage_http_client.auth import Auth +test_api_key = 'test_api_key' +test_api_secret = 'test_api_secret' + def read_file(path): """Read a file from the testutils/data directory.""" @@ -10,10 +14,16 @@ def read_file(path): return input_file.read() +def get_base64_encoded_api_key_and_secret(): + """Return a base64 encoded string of the API key and secret.""" + + return b64encode(f'{test_api_key}:{test_api_secret}'.encode('utf-8')).decode('ascii') + + def get_mock_api_key_auth(): """Return an Auth object with an API key and secret.""" - return Auth(api_key='test_api_key', api_secret='test_api_secret') + return Auth(api_key=test_api_key, api_secret=test_api_secret) def get_mock_jwt_auth(): diff --git a/verify_legacy/src/vonage_verify_legacy/verify_legacy.py b/verify_legacy/src/vonage_verify_legacy/verify_legacy.py index 4ee46860..8c8f2583 100644 --- a/verify_legacy/src/vonage_verify_legacy/verify_legacy.py +++ b/verify_legacy/src/vonage_verify_legacy/verify_legacy.py @@ -31,7 +31,7 @@ class VerifyLegacy: def __init__(self, http_client: HttpClient) -> None: self._http_client = http_client self._sent_data_type = 'form' - self._auth_type = 'body' + self._auth_type = 'basic' @property def http_client(self) -> HttpClient: diff --git a/verify_legacy/tests/test_verify_legacy.py b/verify_legacy/tests/test_verify_legacy.py index 21482b2f..297f0d65 100644 --- a/verify_legacy/tests/test_verify_legacy.py +++ b/verify_legacy/tests/test_verify_legacy.py @@ -10,7 +10,11 @@ from vonage_verify_legacy.responses import NetworkUnblockStatus, VerifyControlStatus from vonage_verify_legacy.verify_legacy import VerifyLegacy -from testutils import build_response, get_mock_api_key_auth +from testutils import ( + build_response, + get_base64_encoded_api_key_and_secret, + get_mock_api_key_auth, +) path = abspath(__file__) @@ -32,6 +36,12 @@ def test_http_client_property(): assert isinstance(verify.http_client, HttpClient) +@responses.activate +def test_default_auth_type(): + verify = VerifyLegacy(HttpClient(get_mock_api_key_auth())) + assert verify._auth_type == 'basic' + + def test_create_verify_request_model(): params = {'brand': 'Acme Inc.', 'sender_id': 'Acme', 'lg': LanguageCode.en_us, **data} request = VerifyRequest(**params) @@ -67,6 +77,12 @@ def test_make_verify_request(): assert response.request_id == 'abcdef0123456789abcdef0123456789' assert response.status == '0' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_make_psd2_request(): @@ -80,6 +96,12 @@ def test_make_psd2_request(): assert response.request_id == 'abcdef0123456789abcdef0123456789' assert response.status == '0' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_verify_request_error(): @@ -129,6 +151,12 @@ def test_check_code(): assert response.currency == 'EUR' assert response.estimated_price_messages_sent == '0.04675' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_check_code_error(): @@ -170,6 +198,12 @@ def test_search(): assert response.events[0].type == 'sms' assert response.events[0].id == '23f3a13d-6d03-4262-8f4d-67f12a56e1c8' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_search_list_of_ids(): @@ -187,6 +221,12 @@ def test_search_list_of_ids(): assert response1.status == 'SUCCESS' assert response1.checks[0].status == 'VALID' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_search_error(): @@ -217,6 +257,12 @@ def test_cancel_verification(): assert response.status == '0' assert response.command == 'cancel' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_cancel_verification_error(): @@ -249,6 +295,12 @@ def test_trigger_next_event(): assert response.status == '0' assert response.command == 'trigger_next_event' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_trigger_next_event_error(): @@ -283,6 +335,12 @@ def test_request_network_unblock(): assert response.network == '23410' assert response.unblocked_until == '2024-04-22T08:34:58Z' + request_headers = responses.calls[0].request.headers + assert ( + request_headers["Authorization"] + == "Basic " + get_base64_encoded_api_key_and_secret() + ) + @responses.activate def test_request_network_unblock_error():