diff --git a/app/notifications/process_client_response.py b/app/notifications/process_client_response.py index f07366422..534408ce6 100644 --- a/app/notifications/process_client_response.py +++ b/app/notifications/process_client_response.py @@ -7,6 +7,8 @@ from app import statsd_client from app.dao import notifications_dao from app.clients.sms.firetext import get_firetext_responses from app.clients.sms.mmg import get_mmg_responses +from app.celery.statistics_tasks import create_outcome_notification_statistic_tasks + sms_response_mapper = { 'MMG': get_mmg_responses, @@ -45,8 +47,9 @@ def process_sms_client_response(status, reference, client_name): # validate status try: response_dict = response_parser(status) - current_app.logger.info('{} callback return status of {} for reference: {}'.format(client_name, - status, reference)) + current_app.logger.info('{} callback return status of {} for reference: {}'.format( + client_name, status, reference) + ) except KeyError: msg = "{} callback failed: status {} not found.".format(client_name, status) return success, msg @@ -77,5 +80,8 @@ def process_sms_client_response(status, reference, client_name): datetime.utcnow(), notification.sent_at ) + + create_outcome_notification_statistic_tasks(notification) + success = "{} callback succeeded. reference {} updated".format(client_name, reference) return success, errors diff --git a/tests/app/notifications/test_notifications_ses_callback.py b/tests/app/notifications/test_notifications_ses_callback.py index c1bf8c78e..0a02b5d58 100644 --- a/tests/app/notifications/test_notifications_ses_callback.py +++ b/tests/app/notifications/test_notifications_ses_callback.py @@ -21,7 +21,9 @@ def test_ses_callback_should_not_need_auth(client): 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( 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): - 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'] 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): - 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'] 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): - 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( 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): - 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( 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): - 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( path='/notifications/email/ses', diff --git a/tests/app/notifications/test_process_client_response.py b/tests/app/notifications/test_process_client_response.py index 57bb659f9..a4e3d2413 100644 --- a/tests/app/notifications/test_process_client_response.py +++ b/tests/app/notifications/test_process_client_response.py @@ -45,25 +45,52 @@ def test_validate_callback_data_returns_error_for_empty_string(): 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') assert success == "{} callback succeeded: send-sms-code".format('sms-client') 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') assert success is None 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') + assert success is None 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') assert success is None assert error == "{} callback failed: status {} not found.".format('Firetext', '000') + stats_mock.assert_not_called()