We are not handling the case of an unknown status code sent by the SMS provider. This PR attempts to fix that.

- If the SMS client sends a status code that we do not recognize raise a ClientException and set the notification status to technical-failure
- Simplified the code in process_client_response, using a simple map.
This commit is contained in:
Rebecca Law
2018-02-07 17:49:15 +00:00
parent a5343fb837
commit b2dfa59b1b
7 changed files with 60 additions and 120 deletions

View File

@@ -2,10 +2,12 @@ import uuid
from datetime import datetime
import pytest
from flask import json
from freezegun import freeze_time
import app.celery.tasks
from app.clients import ClientException
from app.dao.notifications_dao import (
get_notification_by_id
)
@@ -161,15 +163,17 @@ def test_firetext_callback_should_return_400_if_no_status(client, mocker):
assert json_resp['message'] == ['Firetext callback failed: status missing']
def test_firetext_callback_should_return_400_if_unknown_status(client, mocker):
def test_firetext_callback_should_set_status_technical_failure_if_status_unknown(
client, notify_db, notify_db_session, mocker):
notification = create_sample_notification(
notify_db, notify_db_session, status='sending', sent_at=datetime.utcnow()
)
mocker.patch('app.statsd_client.incr')
data = 'mobile=441234123123&status=99&time=2016-03-10 14:17:00&reference={}'.format(uuid.uuid4())
response = firetext_post(client, data)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Firetext callback failed: status 99 not found.'
data = 'mobile=441234123123&status=99&time=2016-03-10 14:17:00&reference={}'.format(notification.id)
with pytest.raises(ClientException) as e:
firetext_post(client, data)
assert get_notification_by_id(notification.id).status == 'technical-failure'
assert 'Firetext callback failed: status 99 not found.' in str(e.value)
def test_firetext_callback_returns_200_when_notification_id_not_found_or_already_updated(client, mocker):
@@ -389,7 +393,7 @@ def test_process_mmg_response_status_4_updates_notification_with_temporary_faile
assert get_notification_by_id(notification.id).status == 'temporary-failure'
def test_process_mmg_response_unknown_status_updates_notification_with_failed(
def test_process_mmg_response_unknown_status_updates_notification_with_technical_failure(
notify_db, notify_db_session, client, mocker
):
send_mock = mocker.patch(
@@ -403,12 +407,10 @@ def test_process_mmg_response_unknown_status_updates_notification_with_failed(
"MSISDN": "447777349060",
"status": 10})
create_service_callback_api(service=notification.service, url="https://original_url.com")
response = mmg_post(client, data)
assert response.status_code == 200
json_data = json.loads(response.data)
assert json_data['result'] == 'success'
assert json_data['message'] == 'MMG callback succeeded. reference {} updated'.format(notification.id)
assert get_notification_by_id(notification.id).status == 'failed'
with pytest.raises(ClientException) as e:
mmg_post(client, data)
assert 'MMG callback failed: status 10 not found.' in str(e.value)
assert get_notification_by_id(notification.id).status == 'technical-failure'
assert send_mock.called