mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
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:
@@ -9,27 +9,15 @@ from app.clients.sms.firetext import get_firetext_responses, SmsClientResponseEx
|
||||
|
||||
|
||||
def test_should_return_correct_details_for_delivery():
|
||||
response_dict = get_firetext_responses('0')
|
||||
assert response_dict['message'] == 'Delivered'
|
||||
assert response_dict['notification_status'] == 'delivered'
|
||||
assert response_dict['notification_statistics_status'] == 'delivered'
|
||||
assert response_dict['success']
|
||||
get_firetext_responses('0') == 'delivered'
|
||||
|
||||
|
||||
def test_should_return_correct_details_for_bounced():
|
||||
response_dict = get_firetext_responses('1')
|
||||
assert response_dict['message'] == 'Declined'
|
||||
assert response_dict['notification_status'] == 'permanent-failure'
|
||||
assert response_dict['notification_statistics_status'] == 'failure'
|
||||
assert not response_dict['success']
|
||||
get_firetext_responses('1') == 'permanent-failure'
|
||||
|
||||
|
||||
def test_should_return_correct_details_for_complaint():
|
||||
response_dict = get_firetext_responses('2')
|
||||
assert response_dict['message'] == 'Undelivered (Pending with Network)'
|
||||
assert response_dict['notification_status'] == 'pending'
|
||||
assert response_dict['notification_statistics_status'] is None
|
||||
assert response_dict['success']
|
||||
get_firetext_responses('2') == 'pending'
|
||||
|
||||
|
||||
def test_should_be_none_if_unrecognised_status_code():
|
||||
|
||||
@@ -10,27 +10,22 @@ from app.clients.sms.mmg import get_mmg_responses, MMGClientResponseException
|
||||
|
||||
|
||||
def test_should_return_correct_details_for_delivery():
|
||||
response_dict = get_mmg_responses('3')
|
||||
assert response_dict['message'] == 'Delivered'
|
||||
assert response_dict['notification_status'] == 'delivered'
|
||||
assert response_dict['notification_statistics_status'] == 'delivered'
|
||||
assert response_dict['success']
|
||||
get_mmg_responses('3') == 'delivered'
|
||||
|
||||
|
||||
def test_should_return_correct_details_for_bounced():
|
||||
response_dict = get_mmg_responses('50')
|
||||
assert response_dict['message'] == 'Declined'
|
||||
assert response_dict['notification_status'] == 'failed'
|
||||
assert response_dict['notification_statistics_status'] == 'failure'
|
||||
assert not response_dict['success']
|
||||
def test_should_return_correct_details_for_temporary_failure():
|
||||
get_mmg_responses('4') == 'temporary-failure'
|
||||
|
||||
|
||||
def test_should_be_none_if_unrecognised_status_code():
|
||||
response_dict = get_mmg_responses('blah')
|
||||
assert response_dict['message'] == 'Declined'
|
||||
assert response_dict['notification_status'] == 'failed'
|
||||
assert response_dict['notification_statistics_status'] == 'failure'
|
||||
assert not response_dict['success']
|
||||
@pytest.mark.parametrize('status', ['5', '2'])
|
||||
def test_should_return_correct_details_for_bounced(status):
|
||||
get_mmg_responses(status) == 'permanent-failure'
|
||||
|
||||
|
||||
def test_should_be_raise_if_unrecognised_status_code():
|
||||
with pytest.raises(KeyError) as e:
|
||||
get_mmg_responses('99')
|
||||
assert '99' in str(e.value)
|
||||
|
||||
|
||||
def test_send_sms_successful_returns_mmg_response(notify_api, mocker):
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
|
||||
from app.clients import ClientException
|
||||
from app.notifications.process_client_response import (
|
||||
validate_callback_data,
|
||||
process_sms_client_response
|
||||
@@ -96,7 +99,7 @@ def test_process_sms_response_returns_error_bad_reference(mocker):
|
||||
stats_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_process_sms_response_returns_error_for_unknown_sms_client(mocker):
|
||||
def test_process_sms_response_raises_client_exception_for_unknown_sms_client(mocker):
|
||||
stats_mock = mocker.patch('app.notifications.process_client_response.create_outcome_notification_statistic_tasks')
|
||||
success, error = process_sms_client_response(status='000', reference=str(uuid.uuid4()), client_name='sms-client')
|
||||
|
||||
@@ -105,10 +108,8 @@ def test_process_sms_response_returns_error_for_unknown_sms_client(mocker):
|
||||
stats_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_process_sms_response_returns_error_for_unknown_status(mocker):
|
||||
stats_mock = mocker.patch('app.notifications.process_client_response.create_outcome_notification_statistic_tasks')
|
||||
def test_process_sms_response_raises_client_exception_for_unknown_status(mocker):
|
||||
with pytest.raises(ClientException) as e:
|
||||
process_sms_client_response(status='000', reference=str(uuid.uuid4()), client_name='Firetext')
|
||||
|
||||
success, error = process_sms_client_response(status='000', reference=str(uuid.uuid4()), client_name='Firetext')
|
||||
assert success is None
|
||||
assert error == "{} callback failed: status {} not found.".format('Firetext', '000')
|
||||
stats_mock.assert_not_called()
|
||||
assert "{} callback failed: status {} not found.".format('Firetext', '000') in str(e.value)
|
||||
|
||||
Reference in New Issue
Block a user