Skip to content

Commit bd4fcb0

Browse files
committed
add support for basic header auth for messages and verify, move coverage config
1 parent 0937cc2 commit bd4fcb0

File tree

13 files changed

+158
-34
lines changed

13 files changed

+158
-34
lines changed

messages/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.3.0
2+
- Add support for API key/secret header authentication
3+
14
# 1.2.3
25
- Update dependency versions
36

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.2.3'
1+
__version__ = '1.3.0'

messages/src/vonage_messages/messages.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Messages:
1616

1717
def __init__(self, http_client: HttpClient) -> None:
1818
self._http_client = http_client
19+
self._auth_type = 'jwt'
20+
21+
if self._http_client.auth.application_id is None:
22+
self._auth_type = 'basic'
1923

2024
@property
2125
def http_client(self) -> HttpClient:
@@ -42,7 +46,9 @@ def send(self, message: BaseMessage) -> SendMessageResponse:
4246
self._http_client.api_host,
4347
'/v1/messages',
4448
message.model_dump(by_alias=True, exclude_none=True) or message,
49+
self._auth_type,
4550
)
51+
4652
return SendMessageResponse(**response)
4753

4854
@validate_call
@@ -63,6 +69,7 @@ def mark_whatsapp_message_read(self, message_uuid: str) -> None:
6369
self._http_client.api_host,
6470
f'/v1/messages/{message_uuid}',
6571
{'status': 'read'},
72+
self._auth_type,
6673
)
6774

6875
@validate_call
@@ -83,4 +90,5 @@ def revoke_rcs_message(self, message_uuid: str) -> None:
8390
self._http_client.api_host,
8491
f'/v1/messages/{message_uuid}',
8592
{'status': 'revoked'},
93+
self._auth_type,
8694
)

messages/tests/test_messages.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import responses
44
from pytest import raises
5+
from vonage_http_client.auth import Auth
56
from vonage_http_client.errors import HttpRequestError
67
from vonage_http_client.http_client import HttpClient, HttpClientOptions
78
from vonage_messages.messages import Messages
@@ -13,14 +14,29 @@
1314
)
1415
from vonage_messages.responses import SendMessageResponse
1516

16-
from testutils import build_response, get_mock_jwt_auth
17+
from testutils import build_response, get_mock_api_key_auth, get_mock_jwt_auth
1718

1819
path = abspath(__file__)
1920

2021

2122
messages = Messages(HttpClient(get_mock_jwt_auth()))
2223

2324

25+
@responses.activate
26+
def test_default_auth_type():
27+
messages = Messages(
28+
HttpClient(
29+
Auth(
30+
api_key='asdf',
31+
api_secret='asdf',
32+
application_id='asdf',
33+
private_key='-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDZz9Zz\n-----END PRIVATE-KEY----',
34+
)
35+
)
36+
)
37+
assert messages._auth_type == 'jwt'
38+
39+
2440
@responses.activate
2541
def test_send_message():
2642
build_response(
@@ -34,6 +50,24 @@ def test_send_message():
3450
response = messages.send(sms)
3551
assert type(response) == SendMessageResponse
3652
assert response.message_uuid == 'd8f86df1-dec6-442f-870a-2241be27d721'
53+
assert messages._auth_type == 'jwt'
54+
55+
56+
@responses.activate
57+
def test_send_message_basic_auth():
58+
build_response(
59+
path, 'POST', 'https://api.nexmo.com/v1/messages', 'send_message.json', 202
60+
)
61+
messages = Messages(HttpClient(get_mock_api_key_auth()))
62+
sms = Sms(
63+
from_='Vonage APIs',
64+
to='1234567890',
65+
text='Hello, World!',
66+
)
67+
response = messages.send(sms)
68+
assert type(response) == SendMessageResponse
69+
assert response.message_uuid == 'd8f86df1-dec6-442f-870a-2241be27d721'
70+
assert messages._auth_type == 'basic'
3771

3872

3973
@responses.activate

pants.toml

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[GLOBAL]
2-
pants_version = '2.23.0rc1'
2+
pants_version = '2.23.0'
33

44
backend_packages = [
55
'pants.backend.python',
@@ -29,30 +29,6 @@ args = ['-vv', '--no-header']
2929
[coverage-py]
3030
interpreter_constraints = ['>=3.8']
3131
report = ['html', 'console']
32-
filter = [
33-
'vonage/src',
34-
'http_client/src',
35-
'account/src',
36-
'application/src',
37-
'jwt/src',
38-
'messages/src',
39-
'network_auth/src',
40-
'network_number_verification/src',
41-
'network_sim_swap/src',
42-
'number_insight/src',
43-
'number_insight_v2/src',
44-
'number_management/src',
45-
'sms/src',
46-
'subaccounts/src',
47-
'users/src',
48-
'utils/src',
49-
'testutils',
50-
'verify/src',
51-
'verify_legacy/src',
52-
'video/src',
53-
'voice/src',
54-
'vonage_utils/src',
55-
]
5632

5733
[black]
5834
args = ['--line-length=90', '--skip-string-normalization']

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.coverage.run]
2+
omit = ['**/tests/*', '**/src/**/_version.py']

verify/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 2.1.0
2+
- Add support for API key/secret header authentication
3+
14
# 2.0.0
25
- Rename `vonage-verify-v2` package -> `vonage-verify`, `VerifyV2` -> `Verify`, etc. This package now contains code for the Verify v2 API
36
- Update dependency versions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.0.0'
1+
__version__ = '2.1.0'

verify/src/vonage_verify/verify.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class Verify:
1010

1111
def __init__(self, http_client: HttpClient) -> None:
1212
self._http_client = http_client
13+
self._auth_type = 'jwt'
14+
15+
if self._http_client.auth.application_id is None:
16+
self._auth_type = 'basic'
1317

1418
@property
1519
def http_client(self) -> HttpClient:
@@ -37,6 +41,7 @@ def start_verification(
3741
self._http_client.api_host,
3842
'/v2/verify',
3943
verify_request.model_dump(by_alias=True, exclude_none=True),
44+
self._auth_type,
4045
)
4146

4247
return StartVerificationResponse(**response)
@@ -53,7 +58,10 @@ def check_code(self, request_id: str, code: str) -> CheckCodeResponse:
5358
CheckCodeResponse: The response object containing the verification result.
5459
"""
5560
response = self._http_client.post(
56-
self._http_client.api_host, f'/v2/verify/{request_id}', {'code': code}
61+
self._http_client.api_host,
62+
f'/v2/verify/{request_id}',
63+
{'code': code},
64+
self._auth_type,
5765
)
5866
return CheckCodeResponse(**response)
5967

@@ -64,7 +72,11 @@ def cancel_verification(self, request_id: str) -> None:
6472
Args:
6573
request_id (str): The request ID.
6674
"""
67-
self._http_client.delete(self._http_client.api_host, f'/v2/verify/{request_id}')
75+
self._http_client.delete(
76+
self._http_client.api_host,
77+
f'/v2/verify/{request_id}',
78+
auth_type=self._auth_type,
79+
)
6880

6981
@validate_call
7082
def trigger_next_workflow(self, request_id: str) -> None:
@@ -77,4 +89,5 @@ def trigger_next_workflow(self, request_id: str) -> None:
7789
self._http_client.post(
7890
self._http_client.api_host,
7991
f'/v2/verify/{request_id}/next_workflow',
92+
auth_type=self._auth_type,
8093
)

verify/tests/test_verify.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22

33
import responses
44
from pytest import raises
5+
from vonage_http_client.auth import Auth
56
from vonage_http_client.errors import HttpRequestError
67
from vonage_http_client.http_client import HttpClient
78
from vonage_verify.requests import *
89
from vonage_verify.verify import Verify
910

10-
from testutils import build_response, get_mock_jwt_auth
11+
from testutils import build_response, get_mock_api_key_auth, get_mock_jwt_auth
1112

1213
path = abspath(__file__)
1314

1415

1516
verify = Verify(HttpClient(get_mock_jwt_auth()))
1617

1718

19+
@responses.activate
20+
def test_default_auth_type():
21+
verify = Verify(
22+
HttpClient(
23+
Auth(
24+
api_key='asdf',
25+
api_secret='asdf',
26+
application_id='asdf',
27+
private_key='-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDZz9Zz\n-----END PRIVATE-KEY----',
28+
)
29+
)
30+
)
31+
assert verify._auth_type == 'jwt'
32+
33+
1834
@responses.activate
1935
def test_make_verify_request():
2036
build_response(
@@ -37,6 +53,27 @@ def test_make_verify_request():
3753
== 'https://api-eu-3.vonage.com/v2/verify/cfbc9a3b-27a2-40d4-a4e0-0c59b3b41901/silent-auth/redirect'
3854
)
3955
assert verify._http_client.last_response.status_code == 202
56+
assert verify._auth_type == 'jwt'
57+
58+
59+
@responses.activate
60+
def test_make_verify_request_basic_auth():
61+
build_response(
62+
path, 'POST', 'https://api.nexmo.com/v2/verify', 'verify_request.json', 202
63+
)
64+
sms_channel = SmsChannel(channel=ChannelType.SMS, to='1234567890', from_='Vonage')
65+
params = {
66+
'brand': 'Vonage',
67+
'workflow': [sms_channel],
68+
}
69+
request = VerifyRequest(**params)
70+
71+
verify = Verify(HttpClient(get_mock_api_key_auth()))
72+
73+
response = verify.start_verification(request)
74+
assert response.request_id == '2c59e3f4-a047-499f-a14f-819cd1989d2e'
75+
assert verify._http_client.last_response.status_code == 202
76+
assert verify._auth_type == 'basic'
4077

4178

4279
@responses.activate
@@ -108,6 +145,23 @@ def test_check_code():
108145
assert response.status == 'completed'
109146

110147

148+
@responses.activate
149+
def test_check_code_basic_auth():
150+
build_response(
151+
path,
152+
'POST',
153+
'https://api.nexmo.com/v2/verify/36e7060d-2b23-4257-bad0-773ab47f85ef',
154+
'check_code.json',
155+
)
156+
verify = Verify(HttpClient(get_mock_api_key_auth()))
157+
response = verify.check_code(
158+
request_id='36e7060d-2b23-4257-bad0-773ab47f85ef', code='1234'
159+
)
160+
assert response.request_id == '36e7060d-2b23-4257-bad0-773ab47f85ef'
161+
assert response.status == 'completed'
162+
assert verify._auth_type == 'basic'
163+
164+
111165
@responses.activate
112166
def test_check_code_invalid_code_error():
113167
build_response(
@@ -153,6 +207,20 @@ def test_cancel_verification():
153207
assert verify._http_client.last_response.status_code == 204
154208

155209

210+
@responses.activate
211+
def test_cancel_verification_basic_auth():
212+
responses.add(
213+
responses.DELETE,
214+
'https://api.nexmo.com/v2/verify/36e7060d-2b23-4257-bad0-773ab47f85ef',
215+
status=204,
216+
)
217+
218+
verify = Verify(HttpClient(get_mock_api_key_auth()))
219+
assert verify.cancel_verification('36e7060d-2b23-4257-bad0-773ab47f85ef') is None
220+
assert verify._http_client.last_response.status_code == 204
221+
assert verify._auth_type == 'basic'
222+
223+
156224
@responses.activate
157225
def test_trigger_next_workflow():
158226
responses.add(
@@ -164,6 +232,20 @@ def test_trigger_next_workflow():
164232
assert verify._http_client.last_response.status_code == 200
165233

166234

235+
@responses.activate
236+
def test_trigger_next_workflow_basic_auth():
237+
responses.add(
238+
responses.POST,
239+
'https://api.nexmo.com/v2/verify/36e7060d-2b23-4257-bad0-773ab47f85ef/next_workflow',
240+
status=200,
241+
)
242+
243+
verify = Verify(HttpClient(get_mock_api_key_auth()))
244+
assert verify.trigger_next_workflow('36e7060d-2b23-4257-bad0-773ab47f85ef') is None
245+
assert verify._http_client.last_response.status_code == 200
246+
assert verify._auth_type == 'basic'
247+
248+
167249
@responses.activate
168250
def test_trigger_next_event_error():
169251
build_response(

0 commit comments

Comments
 (0)