mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-16 18:22:17 -05:00
Pass notification ID to fire text as our reference
- also handle fire text errors, non-zero response code means error.
This commit is contained in:
@@ -203,8 +203,9 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
|
|||||||
)
|
)
|
||||||
|
|
||||||
client.send_sms(
|
client.send_sms(
|
||||||
notification['to'],
|
to=notification['to'],
|
||||||
template.replaced
|
content=template.replaced,
|
||||||
|
notification_id=notification_id
|
||||||
)
|
)
|
||||||
except FiretextClientException as e:
|
except FiretextClientException as e:
|
||||||
current_app.logger.exception(e)
|
current_app.logger.exception(e)
|
||||||
|
|||||||
@@ -11,7 +11,12 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class FiretextClientException(SmsClientException):
|
class FiretextClientException(SmsClientException):
|
||||||
pass
|
def __init__(self, response):
|
||||||
|
self.code = response['code']
|
||||||
|
self.description = response['description']
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Code {} description {}".format(self.code, self.description)
|
||||||
|
|
||||||
|
|
||||||
class FiretextClient(SmsClient):
|
class FiretextClient(SmsClient):
|
||||||
@@ -28,7 +33,7 @@ class FiretextClient(SmsClient):
|
|||||||
def get_name(self):
|
def get_name(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def send_sms(self, to, content):
|
def send_sms(self, to, content, notification_id=None):
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"apiKey": self.api_key,
|
"apiKey": self.api_key,
|
||||||
@@ -37,13 +42,21 @@ class FiretextClient(SmsClient):
|
|||||||
"message": content
|
"message": content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if notification_id:
|
||||||
|
data.update({
|
||||||
|
"reference": notification_id
|
||||||
|
})
|
||||||
|
|
||||||
|
start_time = monotonic()
|
||||||
try:
|
try:
|
||||||
start_time = monotonic()
|
|
||||||
response = request(
|
response = request(
|
||||||
"POST",
|
"POST",
|
||||||
"https://www.firetext.co.uk/api/sendsms",
|
"https://www.firetext.co.uk/api/sendsms/json",
|
||||||
data=data
|
data=data
|
||||||
)
|
)
|
||||||
|
firetext_response = response.json()
|
||||||
|
if firetext_response['code'] != 0:
|
||||||
|
raise FiretextClientException(firetext_response)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except RequestException as e:
|
except RequestException as e:
|
||||||
api_error = HTTPError.create(e)
|
api_error = HTTPError.create(e)
|
||||||
|
|||||||
@@ -260,7 +260,11 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
|||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
|
|
||||||
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: Hello Jo")
|
firetext_client.send_sms.assert_called_once_with(
|
||||||
|
to="+441234123123",
|
||||||
|
content="Sample service: Hello Jo",
|
||||||
|
notification_id=notification_id
|
||||||
|
)
|
||||||
persisted_notification = notifications_dao.get_notification(
|
persisted_notification = notifications_dao.get_notification(
|
||||||
sample_template_with_placeholders.service_id, notification_id
|
sample_template_with_placeholders.service_id, notification_id
|
||||||
)
|
)
|
||||||
@@ -292,7 +296,11 @@ def test_should_send_sms_without_personalisation(sample_template, mocker):
|
|||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
|
|
||||||
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
|
firetext_client.send_sms.assert_called_once_with(
|
||||||
|
to="+441234123123",
|
||||||
|
content="Sample service: This is a template",
|
||||||
|
notification_id=notification_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notify_db_session, mocker):
|
def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notify_db_session, mocker):
|
||||||
@@ -317,7 +325,11 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
|
|||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
|
|
||||||
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
|
firetext_client.send_sms.assert_called_once_with(
|
||||||
|
to="+441234123123",
|
||||||
|
content="Sample service: This is a template",
|
||||||
|
notification_id=notification_id
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, notify_db_session, mocker):
|
def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, notify_db_session, mocker):
|
||||||
@@ -394,7 +406,11 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
|
|||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
|
firetext_client.send_sms.assert_called_once_with(
|
||||||
|
to="+441234123123",
|
||||||
|
content="Sample service: This is a template",
|
||||||
|
notification_id=notification_id
|
||||||
|
)
|
||||||
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
|
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
|
||||||
assert persisted_notification.id == notification_id
|
assert persisted_notification.id == notification_id
|
||||||
assert persisted_notification.to == '+441234123123'
|
assert persisted_notification.to == '+441234123123'
|
||||||
@@ -490,7 +506,10 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
|
|||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
)
|
)
|
||||||
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
|
firetext_client.send_sms.assert_called_once_with(
|
||||||
|
to="+441234123123",
|
||||||
|
content="Sample service: This is a template",
|
||||||
|
notification_id=notification_id)
|
||||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||||
assert persisted_notification.id == notification_id
|
assert persisted_notification.id == notification_id
|
||||||
assert persisted_notification.to == '+441234123123'
|
assert persisted_notification.to == '+441234123123'
|
||||||
|
|||||||
Reference in New Issue
Block a user