Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mchlwellman committed Dec 2, 2024
1 parent ac913fa commit b807a5f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
13 changes: 6 additions & 7 deletions app/clients/sms/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def send_sms(
Return: a string containing the Twilio message.sid
"""

start_time = monotonic()
from app.dao.service_sms_sender_dao import (
dao_get_service_sms_sender_by_service_id_and_number,
Expand Down Expand Up @@ -239,12 +238,12 @@ def send_sms(
if e.status == 400 and 'phone number' in e.msg:
self.logger.exception('Twilio send SMS request for %s failed', reference)
raise InvalidProviderException from e
# elif e.status == 400 and e.code == 21617: # Twilio error code for max length exceeded
# self.logger.exception(
# 'Twilio send SMS request for %s failed, message content max length exceeded.', reference
# )
# self.logger.debug('Twilio error details for %s - %s: %s', reference, e.code, e.msg)
# raise NonRetryableException('Twilio request failed') from e
elif e.status == 400 and e.code == 21617: # Twilio error code for max length exceeded
self.logger.exception(
'Twilio send SMS request for %s failed, message content max length exceeded.', reference
)
self.logger.debug('Twilio error details for %s - %s: %s', reference, e.code, e.msg)
raise NonRetryableException('Twilio request failed') from e
else:
raise
except:
Expand Down
34 changes: 29 additions & 5 deletions tests/app/clients/test_twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,19 +599,43 @@ def test_send_sms_twilio_callback(
assert response_dict['sid'] == twilio_sid


@pytest.mark.parametrize(
(
'response_dict',
'exception',
),
(
(
{
'code': 21606,
'message': "The 'From' phone number provided (+61412345678) is not a valid message-capable Twilio phone number for this destination",
},
InvalidProviderException,
),
(
{
'code': 21617,
'message': 'Unable to create record: The concatenated message body exceeds the 1600 character limit',
},
NonRetryableException,
),
),
ids=(
'invalid-from-number',
'message-too-long',
),
)
def test_send_sms_raises_invalid_provider_error_with_invalid_twilio_number(
notify_api,
mocker,
response_dict,
exception,
):
to = '+61412345678'
content = 'my message'
reference = 'my reference'
response_dict = {
'code': 21606,
'message': "The 'From' phone number provided (+61412345678) is not a valid message-capable Twilio phone number for this destination",
}

with pytest.raises(InvalidProviderException):
with pytest.raises(exception):
with requests_mock.Mocker() as r_mock:
r_mock.post(
f'https://api.twilio.com/2010-04-01/Accounts/{twilio_sms_client._account_sid}/Messages.json',
Expand Down

0 comments on commit b807a5f

Please sign in to comment.