From 41ce69170483841b24346ec460fa6919714782cf Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Wed, 20 Apr 2016 09:45:13 +0100 Subject: [PATCH] Update to processing the the response from MMG MMG changed the datatype and the status codes they send us for the delivery receipts. This PR accounts for that change. --- app/clients/sms/mmg.py | 2 +- app/notifications/process_client_response.py | 2 +- app/notifications/rest.py | 2 +- tests/app/clients/test_mmg.py | 2 +- .../test_process_client_response.py | 19 ++++++++++++ tests/app/notifications/test_rest.py | 31 ++++++++++++++----- 6 files changed, 47 insertions(+), 11 deletions(-) diff --git a/app/clients/sms/mmg.py b/app/clients/sms/mmg.py index bf74358f3..2c9f66bda 100644 --- a/app/clients/sms/mmg.py +++ b/app/clients/sms/mmg.py @@ -6,7 +6,7 @@ from app.clients import (STATISTICS_DELIVERED, STATISTICS_FAILURE) from app.clients.sms import (SmsClient, SmsClientException) mmg_response_map = { - '00': { + '0': { "message": 'Delivered', "notification_statistics_status": STATISTICS_DELIVERED, "success": True, diff --git a/app/notifications/process_client_response.py b/app/notifications/process_client_response.py index a9afe00b7..bbed09d1f 100644 --- a/app/notifications/process_client_response.py +++ b/app/notifications/process_client_response.py @@ -12,7 +12,7 @@ sms_response_mapper = {'MMG': get_mmg_responses, def validate_callback_data(data, fields, client_name): errors = [] for f in fields: - if len(data.get(f, '')) <= 0: + if not str(data.get(f, '')): error = "{} callback failed: {} missing".format(client_name, f) errors.append(error) return errors if len(errors) > 0 else None diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 8dfd1faff..3d454489d 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -147,7 +147,7 @@ def process_mmg_response(): [current_app.logger.info(e) for e in validation_errors] return jsonify(result='error', message=validation_errors), 400 - success, errors = process_sms_client_response(status=data.get('status'), + success, errors = process_sms_client_response(status=str(data.get('status')), reference=data.get('CID'), client_name='MMG') if errors: diff --git a/tests/app/clients/test_mmg.py b/tests/app/clients/test_mmg.py index 48ed402ef..0ed3fea3c 100644 --- a/tests/app/clients/test_mmg.py +++ b/tests/app/clients/test_mmg.py @@ -2,7 +2,7 @@ from app.clients.sms.mmg import get_mmg_responses def test_should_return_correct_details_for_delivery(): - response_dict = get_mmg_responses('00') + response_dict = get_mmg_responses('0') assert response_dict['message'] == 'Delivered' assert response_dict['notification_status'] == 'delivered' assert response_dict['notification_statistics_status'] == 'delivered' diff --git a/tests/app/notifications/test_process_client_response.py b/tests/app/notifications/test_process_client_response.py index 8e4e1580a..6e2f5d46d 100644 --- a/tests/app/notifications/test_process_client_response.py +++ b/tests/app/notifications/test_process_client_response.py @@ -27,6 +27,25 @@ def test_validate_callback_data_return_errors_when_fields_are_empty(): assert "{} callback failed: {} missing".format(client_name, 'cid') in errors +def test_validate_callback_data_can_handle_integers(): + form = {'status': 00, 'cid': 'fsdfadfsdfas'} + fields = ['status', 'cid'] + client_name = 'sms client' + + result = validate_callback_data(form, fields, client_name) + assert result is None + + +def test_validate_callback_data_returns_error_for_empty_string(): + form = {'status': '', 'cid': 'fsdfadfsdfas'} + fields = ['status', 'cid'] + client_name = 'sms client' + + result = validate_callback_data(form, fields, client_name) + assert result is not None + assert "{} callback failed: {} missing".format(client_name, 'status') in result + + def test_process_sms_response_return_success_for_send_sms_code_reference(): 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') diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index e35c06bea..8502bbbc6 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -1293,8 +1293,7 @@ def test_firetext_callback_should_update_notification_status_failed(notify_api, assert json_resp['message'] == 'Firetext callback succeeded. reference {} updated'.format( sample_notification.id ) - updated = get_notification_by_id(sample_notification.id) - assert updated.status == 'failed' + assert get_notification_by_id(sample_notification.id).status == 'failed' assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_delivered == 0 assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_requested == 1 assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_failed == 1 @@ -1320,8 +1319,7 @@ def test_firetext_callback_should_update_notification_status_sent(notify_api, no assert json_resp['message'] == 'Firetext callback succeeded. reference {} updated'.format( notification.id ) - updated = get_notification_by_id(notification.id) - assert updated.status == 'delivered' + assert get_notification_by_id(notification.id).status == 'delivered' assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_delivered == 1 assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_requested == 1 assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_failed == 0 @@ -1365,7 +1363,7 @@ def test_process_mmg_response_return_200_when_cid_is_send_sms_code(notify_api): data = json.dumps({"reference": "10100164", "CID": "send-sms-code", "MSISDN": "447775349060", - "status": "00", + "status": 00, "deliverytime": "2016-04-05 16:01:07"}) with notify_api.test_client() as client: @@ -1383,7 +1381,7 @@ def test_process_mmg_response_returns_200_when_cid_is_valid_notification_id(noti data = json.dumps({"reference": "mmg_reference", "CID": str(sample_notification.id), "MSISDN": "447777349060", - "status": "00", + "status": 00, "deliverytime": "2016-04-05 16:01:07"}) response = client.post(path='notifications/sms/mmg', @@ -1393,6 +1391,25 @@ def test_process_mmg_response_returns_200_when_cid_is_valid_notification_id(noti json_data = json.loads(response.data) assert json_data['result'] == 'success' assert json_data['message'] == 'MMG callback succeeded. reference {} updated'.format(sample_notification.id) + assert get_notification_by_id(sample_notification.id).status == 'delivered' + + +def test_process_mmg_response_updates_notification_with_failed_status(notify_api, sample_notification): + with notify_api.test_client() as client: + data = json.dumps({"reference": "mmg_reference", + "CID": str(sample_notification.id), + "MSISDN": "447777349060", + "status": 5, + "deliverytime": "2016-04-05 16:01:07"}) + + response = client.post(path='notifications/sms/mmg', + data=data, + headers=[('Content-Type', 'application/json')]) + 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(sample_notification.id) + assert get_notification_by_id(sample_notification.id).status == 'failed' def test_process_mmg_response_returns_400_for_malformed_data(notify_api): @@ -1400,7 +1417,7 @@ def test_process_mmg_response_returns_400_for_malformed_data(notify_api): data = json.dumps({"reference": "mmg_reference", "monkey": 'random thing', "MSISDN": "447777349060", - "no_status": "00", + "no_status": 00, "deliverytime": "2016-04-05 16:01:07"}) response = client.post(path='notifications/sms/mmg',