Skip to content

Commit 9c2d663

Browse files
committed
DEVX-10537: Updating Verify v1 implementation and tests for Basic Auth support
1 parent 340291c commit 9c2d663

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

testutils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .mock_auth import get_mock_api_key_auth, get_mock_jwt_auth
1+
from .mock_auth import get_mock_api_key_auth, get_mock_jwt_auth, get_base64_encoded_api_key_and_secret
22
from .testutils import build_response
33

4-
__all__ = ['build_response', 'get_mock_api_key_auth', 'get_mock_jwt_auth']
4+
__all__ = ['build_response', 'get_mock_api_key_auth', 'get_mock_jwt_auth', 'get_base64_encoded_api_key_and_secret']

testutils/mock_auth.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from os.path import dirname, join
2+
from base64 import b64encode
23

34
from vonage_http_client.auth import Auth
45

6+
test_api_key = 'test_api_key'
7+
test_api_secret = 'test_api_secret'
8+
59

610
def read_file(path):
711
"""Read a file from the testutils/data directory."""
@@ -10,10 +14,16 @@ def read_file(path):
1014
return input_file.read()
1115

1216

17+
def get_base64_encoded_api_key_and_secret():
18+
"""Return a base64 encoded string of the API key and secret."""
19+
20+
return b64encode(f'{test_api_key}:{test_api_secret}'.encode('utf-8')).decode('ascii')
21+
22+
1323
def get_mock_api_key_auth():
1424
"""Return an Auth object with an API key and secret."""
1525

16-
return Auth(api_key='test_api_key', api_secret='test_api_secret')
26+
return Auth(api_key=test_api_key, api_secret=test_api_secret)
1727

1828

1929
def get_mock_jwt_auth():

verify_legacy/src/vonage_verify_legacy/verify_legacy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class VerifyLegacy:
3131
def __init__(self, http_client: HttpClient) -> None:
3232
self._http_client = http_client
3333
self._sent_data_type = 'form'
34-
self._auth_type = 'body'
34+
self._auth_type = 'basic'
3535

3636
@property
3737
def http_client(self) -> HttpClient:

verify_legacy/tests/test_verify_legacy.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from vonage_verify_legacy.responses import NetworkUnblockStatus, VerifyControlStatus
1111
from vonage_verify_legacy.verify_legacy import VerifyLegacy
1212

13-
from testutils import build_response, get_mock_api_key_auth
13+
from testutils import build_response, get_mock_api_key_auth, get_base64_encoded_api_key_and_secret
1414

1515
path = abspath(__file__)
1616

@@ -32,6 +32,12 @@ def test_http_client_property():
3232
assert isinstance(verify.http_client, HttpClient)
3333

3434

35+
@responses.activate
36+
def test_default_auth_type():
37+
verify = VerifyLegacy(HttpClient(get_mock_api_key_auth()))
38+
assert verify._auth_type == 'basic'
39+
40+
3541
def test_create_verify_request_model():
3642
params = {'brand': 'Acme Inc.', 'sender_id': 'Acme', 'lg': LanguageCode.en_us, **data}
3743
request = VerifyRequest(**params)
@@ -67,6 +73,9 @@ def test_make_verify_request():
6773
assert response.request_id == 'abcdef0123456789abcdef0123456789'
6874
assert response.status == '0'
6975

76+
request_headers = responses.calls[0].request.headers
77+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
78+
7079

7180
@responses.activate
7281
def test_make_psd2_request():
@@ -80,6 +89,9 @@ def test_make_psd2_request():
8089
assert response.request_id == 'abcdef0123456789abcdef0123456789'
8190
assert response.status == '0'
8291

92+
request_headers = responses.calls[0].request.headers
93+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
94+
8395

8496
@responses.activate
8597
def test_verify_request_error():
@@ -129,6 +141,9 @@ def test_check_code():
129141
assert response.currency == 'EUR'
130142
assert response.estimated_price_messages_sent == '0.04675'
131143

144+
request_headers = responses.calls[0].request.headers
145+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
146+
132147

133148
@responses.activate
134149
def test_check_code_error():
@@ -170,6 +185,9 @@ def test_search():
170185
assert response.events[0].type == 'sms'
171186
assert response.events[0].id == '23f3a13d-6d03-4262-8f4d-67f12a56e1c8'
172187

188+
request_headers = responses.calls[0].request.headers
189+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
190+
173191

174192
@responses.activate
175193
def test_search_list_of_ids():
@@ -187,6 +205,9 @@ def test_search_list_of_ids():
187205
assert response1.status == 'SUCCESS'
188206
assert response1.checks[0].status == 'VALID'
189207

208+
request_headers = responses.calls[0].request.headers
209+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
210+
190211

191212
@responses.activate
192213
def test_search_error():
@@ -217,6 +238,9 @@ def test_cancel_verification():
217238
assert response.status == '0'
218239
assert response.command == 'cancel'
219240

241+
request_headers = responses.calls[0].request.headers
242+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
243+
220244

221245
@responses.activate
222246
def test_cancel_verification_error():
@@ -249,6 +273,9 @@ def test_trigger_next_event():
249273
assert response.status == '0'
250274
assert response.command == 'trigger_next_event'
251275

276+
request_headers = responses.calls[0].request.headers
277+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
278+
252279

253280
@responses.activate
254281
def test_trigger_next_event_error():
@@ -283,6 +310,9 @@ def test_request_network_unblock():
283310
assert response.network == '23410'
284311
assert response.unblocked_until == '2024-04-22T08:34:58Z'
285312

313+
request_headers = responses.calls[0].request.headers
314+
assert request_headers["Authorization"] == "Basic " + get_base64_encoded_api_key_and_secret()
315+
286316

287317
@responses.activate
288318
def test_request_network_unblock_error():

0 commit comments

Comments
 (0)