Call the task wrapper outcome function in the statistics_tasks file. This wraps the logic around which tasks to creates and simplifies the logic in the Callback classes.

This commit is contained in:
Martyn Inglis
2017-05-09 22:03:57 +01:00
parent a4549a2e26
commit 19c14982a2
3 changed files with 57 additions and 12 deletions

View File

@@ -7,6 +7,8 @@ from app import statsd_client
from app.dao import notifications_dao from app.dao import notifications_dao
from app.clients.sms.firetext import get_firetext_responses from app.clients.sms.firetext import get_firetext_responses
from app.clients.sms.mmg import get_mmg_responses from app.clients.sms.mmg import get_mmg_responses
from app.celery.statistics_tasks import create_outcome_notification_statistic_tasks
sms_response_mapper = { sms_response_mapper = {
'MMG': get_mmg_responses, 'MMG': get_mmg_responses,
@@ -45,8 +47,9 @@ def process_sms_client_response(status, reference, client_name):
# validate status # validate status
try: try:
response_dict = response_parser(status) response_dict = response_parser(status)
current_app.logger.info('{} callback return status of {} for reference: {}'.format(client_name, current_app.logger.info('{} callback return status of {} for reference: {}'.format(
status, reference)) client_name, status, reference)
)
except KeyError: except KeyError:
msg = "{} callback failed: status {} not found.".format(client_name, status) msg = "{} callback failed: status {} not found.".format(client_name, status)
return success, msg return success, msg
@@ -77,5 +80,8 @@ def process_sms_client_response(status, reference, client_name):
datetime.utcnow(), datetime.utcnow(),
notification.sent_at notification.sent_at
) )
create_outcome_notification_statistic_tasks(notification)
success = "{} callback succeeded. reference {} updated".format(client_name, reference) success = "{} callback succeeded. reference {} updated".format(client_name, reference)
return success, errors return success, errors

View File

@@ -21,7 +21,9 @@ def test_ses_callback_should_not_need_auth(client):
def test_ses_callback_should_fail_if_invalid_json(client, mocker): def test_ses_callback_should_fail_if_invalid_json(client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks') stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',
@@ -36,7 +38,9 @@ def test_ses_callback_should_fail_if_invalid_json(client, mocker):
def test_ses_callback_should_autoconfirm_subscriptions(client, rmock, mocker): def test_ses_callback_should_autoconfirm_subscriptions(client, rmock, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks') stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
endpoint = json.loads(ses_confirmation_callback())['SubscribeURL'] endpoint = json.loads(ses_confirmation_callback())['SubscribeURL']
rmock.request( rmock.request(
@@ -61,7 +65,9 @@ def test_ses_callback_should_autoconfirm_subscriptions(client, rmock, mocker):
def test_ses_callback_autoconfirm_raises_exception_if_not_200(client, rmock, mocker): def test_ses_callback_autoconfirm_raises_exception_if_not_200(client, rmock, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks') stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
endpoint = json.loads(ses_confirmation_callback())['SubscribeURL'] endpoint = json.loads(ses_confirmation_callback())['SubscribeURL']
rmock.request( rmock.request(
@@ -84,7 +90,9 @@ def test_ses_callback_autoconfirm_raises_exception_if_not_200(client, rmock, moc
def test_ses_callback_should_fail_if_invalid_notification_type(client, mocker): def test_ses_callback_should_fail_if_invalid_notification_type(client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks') stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',
@@ -99,7 +107,9 @@ def test_ses_callback_should_fail_if_invalid_notification_type(client, mocker):
def test_ses_callback_should_fail_if_missing_message_id(client, mocker): def test_ses_callback_should_fail_if_missing_message_id(client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks') stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',
@@ -114,7 +124,9 @@ def test_ses_callback_should_fail_if_missing_message_id(client, mocker):
def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, notify_db_session, client, mocker): def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, notify_db_session, client, mocker):
stats_mock = mocker.patch('app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks') stats_mock = mocker.patch(
'app.notifications.notifications_ses_callback.create_outcome_notification_statistic_tasks'
)
response = client.post( response = client.post(
path='/notifications/email/ses', path='/notifications/email/ses',

View File

@@ -45,25 +45,52 @@ def test_validate_callback_data_returns_error_for_empty_string():
assert "{} callback failed: {} missing".format(client_name, 'status') in result assert "{} callback failed: {} missing".format(client_name, 'status') in result
def test_process_sms_response_return_success_for_send_sms_code_reference(): def test_outcome_statistics_called_for_successful_callback(sample_notification, mocker):
stats_mock = mocker.patch('app.notifications.process_client_response.create_outcome_notification_statistic_tasks')
mocker.patch(
'app.notifications.process_client_response.notifications_dao.update_notification_status_by_id',
return_value=sample_notification
)
reference = str(uuid.uuid4())
success, error = process_sms_client_response(status='3', reference=reference, client_name='MMG')
assert success == "MMG callback succeeded. reference {} updated".format(str(reference))
assert error is None
stats_mock.assert_called_once_with(sample_notification)
def test_process_sms_response_return_success_for_send_sms_code_reference(mocker):
stats_mock = mocker.patch('app.notifications.process_client_response.create_outcome_notification_statistic_tasks')
success, error = process_sms_client_response(status='000', reference='send-sms-code', client_name='sms-client') success, error = process_sms_client_response(status='000', reference='send-sms-code', client_name='sms-client')
assert success == "{} callback succeeded: send-sms-code".format('sms-client') assert success == "{} callback succeeded: send-sms-code".format('sms-client')
assert error is None assert error is None
stats_mock.assert_not_called()
def test_process_sms_response_returns_error_bad_reference(): def test_process_sms_response_returns_error_bad_reference(mocker):
stats_mock = mocker.patch('app.notifications.process_client_response.create_outcome_notification_statistic_tasks')
success, error = process_sms_client_response(status='000', reference='something-bad', client_name='sms-client') success, error = process_sms_client_response(status='000', reference='something-bad', client_name='sms-client')
assert success is None assert success is None
assert error == "{} callback with invalid reference {}".format('sms-client', 'something-bad') assert error == "{} callback with invalid reference {}".format('sms-client', 'something-bad')
stats_mock.assert_not_called()
def test_process_sms_response_returns_error_for_unknown_sms_client(): def test_process_sms_response_returns_error_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') success, error = process_sms_client_response(status='000', reference=str(uuid.uuid4()), client_name='sms-client')
assert success is None assert success is None
assert error == 'unknown sms client: {}'.format('sms-client') assert error == 'unknown sms client: {}'.format('sms-client')
stats_mock.assert_not_called()
def test_process_sms_response_returns_error_for_unknown_status(): 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')
success, error = 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 success is None
assert error == "{} callback failed: status {} not found.".format('Firetext', '000') assert error == "{} callback failed: status {} not found.".format('Firetext', '000')
stats_mock.assert_not_called()