diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index 38c1e0015..4dd2892cb 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -25,6 +25,7 @@ from app.models import ( BRANDING_ORG_BANNER, BRANDING_GOVUK, EMAIL_TYPE, + NOTIFICATION_CREATED, NOTIFICATION_TECHNICAL_FAILURE, NOTIFICATION_SENT, NOTIFICATION_SENDING @@ -54,8 +55,17 @@ def send_sms_to_provider(notification): if service.research_mode or notification.key_type == KEY_TYPE_TEST: notification.billable_units = 0 - send_sms_response(provider.get_name(), str(notification.id), notification.to) update_notification(notification, provider) + try: + send_sms_response(provider.get_name(), str(notification.id), notification.to) + except: + # when we retry, we only do anything if the notification is in created - it's currently in sending, + # so set it back so that we actually attempt the callback again + notification.sent_at = None + notification.sent_by = None + notification.status = NOTIFICATION_CREATED + dao_update_notification(notification) + raise else: try: provider.send_sms( diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 99fffc002..00b75f873 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -6,6 +6,7 @@ from unittest.mock import ANY, call import pytest from notifications_utils.recipients import validate_and_format_phone_number from flask import current_app +from requests import HTTPError import app from app import mmg_client, firetext_client @@ -235,6 +236,20 @@ def test_should_call_send_sms_response_task_if_research_mode( assert not persisted_notification.personalisation +def test_should_leave_as_created_if_fake_callback_function_fails(sample_notification, mocker): + mocker.patch('app.delivery.send_to_providers.send_sms_response', side_effect=HTTPError) + + sample_notification.key_type = KEY_TYPE_TEST + + with pytest.raises(HTTPError): + send_to_providers.send_sms_to_provider( + sample_notification + ) + assert sample_notification.status == 'created' + assert sample_notification.sent_at is None + assert sample_notification.sent_by is None + + @pytest.mark.parametrize('research_mode,key_type', [ (True, KEY_TYPE_NORMAL), (False, KEY_TYPE_TEST) diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index 9e9847dc1..390ccac08 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -181,7 +181,7 @@ def test_post_email_notification_returns_201(client, sample_email_template_with_ assert response.status_code == 201 resp_json = json.loads(response.get_data(as_text=True)) assert validate(resp_json, post_email_response) == resp_json - notification = Notification.query.first() + notification = Notification.query.one() assert resp_json['id'] == str(notification.id) assert resp_json['reference'] == reference assert notification.reference is None