diff --git a/app/celery/tasks.py b/app/celery/tasks.py index b3f090127..8f609ed3f 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -203,8 +203,9 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at): ) client.send_sms( - notification['to'], - template.replaced + to=notification['to'], + content=template.replaced, + notification_id=notification_id ) except FiretextClientException as e: current_app.logger.exception(e) diff --git a/app/clients/sms/firetext.py b/app/clients/sms/firetext.py index 3a71ac048..d10f0f875 100644 --- a/app/clients/sms/firetext.py +++ b/app/clients/sms/firetext.py @@ -11,7 +11,12 @@ logger = logging.getLogger(__name__) 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): @@ -28,7 +33,7 @@ class FiretextClient(SmsClient): def get_name(self): return self.name - def send_sms(self, to, content): + def send_sms(self, to, content, notification_id=None): data = { "apiKey": self.api_key, @@ -37,13 +42,21 @@ class FiretextClient(SmsClient): "message": content } + if notification_id: + data.update({ + "reference": notification_id + }) + + start_time = monotonic() try: - start_time = monotonic() response = request( "POST", - "https://www.firetext.co.uk/api/sendsms", + "https://www.firetext.co.uk/api/sendsms/json", data=data ) + firetext_response = response.json() + if firetext_response['code'] != 0: + raise FiretextClientException(firetext_response) response.raise_for_status() except RequestException as e: api_error = HTTPError.create(e) diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index bb2a7bb3b..65abc977e 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -260,7 +260,11 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat 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( 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) ) - 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): @@ -317,7 +325,11 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif 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): @@ -394,7 +406,11 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa "encrypted-in-reality", 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) assert persisted_notification.id == notification_id 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", 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) assert persisted_notification.id == notification_id assert persisted_notification.to == '+441234123123'