reset notification to created if fake callback throws exception

else we ignore it when retrying
This commit is contained in:
Leo Hemsted
2017-10-16 15:33:21 +01:00
parent fd5aa7dd0a
commit 19bb0d157d
3 changed files with 27 additions and 2 deletions

View File

@@ -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(

View File

@@ -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)

View File

@@ -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