mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
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.
This commit is contained in:
@@ -6,7 +6,7 @@ from app.clients import (STATISTICS_DELIVERED, STATISTICS_FAILURE)
|
|||||||
from app.clients.sms import (SmsClient, SmsClientException)
|
from app.clients.sms import (SmsClient, SmsClientException)
|
||||||
|
|
||||||
mmg_response_map = {
|
mmg_response_map = {
|
||||||
'00': {
|
'0': {
|
||||||
"message": 'Delivered',
|
"message": 'Delivered',
|
||||||
"notification_statistics_status": STATISTICS_DELIVERED,
|
"notification_statistics_status": STATISTICS_DELIVERED,
|
||||||
"success": True,
|
"success": True,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ sms_response_mapper = {'MMG': get_mmg_responses,
|
|||||||
def validate_callback_data(data, fields, client_name):
|
def validate_callback_data(data, fields, client_name):
|
||||||
errors = []
|
errors = []
|
||||||
for f in fields:
|
for f in fields:
|
||||||
if len(data.get(f, '')) <= 0:
|
if not str(data.get(f, '')):
|
||||||
error = "{} callback failed: {} missing".format(client_name, f)
|
error = "{} callback failed: {} missing".format(client_name, f)
|
||||||
errors.append(error)
|
errors.append(error)
|
||||||
return errors if len(errors) > 0 else None
|
return errors if len(errors) > 0 else None
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ def process_mmg_response():
|
|||||||
[current_app.logger.info(e) for e in validation_errors]
|
[current_app.logger.info(e) for e in validation_errors]
|
||||||
return jsonify(result='error', message=validation_errors), 400
|
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'),
|
reference=data.get('CID'),
|
||||||
client_name='MMG')
|
client_name='MMG')
|
||||||
if errors:
|
if errors:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from app.clients.sms.mmg import get_mmg_responses
|
|||||||
|
|
||||||
|
|
||||||
def test_should_return_correct_details_for_delivery():
|
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['message'] == 'Delivered'
|
||||||
assert response_dict['notification_status'] == 'delivered'
|
assert response_dict['notification_status'] == 'delivered'
|
||||||
assert response_dict['notification_statistics_status'] == 'delivered'
|
assert response_dict['notification_statistics_status'] == 'delivered'
|
||||||
|
|||||||
@@ -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
|
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():
|
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')
|
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')
|
||||||
|
|||||||
@@ -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(
|
assert json_resp['message'] == 'Firetext callback succeeded. reference {} updated'.format(
|
||||||
sample_notification.id
|
sample_notification.id
|
||||||
)
|
)
|
||||||
updated = get_notification_by_id(sample_notification.id)
|
assert get_notification_by_id(sample_notification.id).status == 'failed'
|
||||||
assert updated.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_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_requested == 1
|
||||||
assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_failed == 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(
|
assert json_resp['message'] == 'Firetext callback succeeded. reference {} updated'.format(
|
||||||
notification.id
|
notification.id
|
||||||
)
|
)
|
||||||
updated = get_notification_by_id(notification.id)
|
assert get_notification_by_id(notification.id).status == 'delivered'
|
||||||
assert updated.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_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_requested == 1
|
||||||
assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_failed == 0
|
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",
|
data = json.dumps({"reference": "10100164",
|
||||||
"CID": "send-sms-code",
|
"CID": "send-sms-code",
|
||||||
"MSISDN": "447775349060",
|
"MSISDN": "447775349060",
|
||||||
"status": "00",
|
"status": 00,
|
||||||
"deliverytime": "2016-04-05 16:01:07"})
|
"deliverytime": "2016-04-05 16:01:07"})
|
||||||
|
|
||||||
with notify_api.test_client() as client:
|
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",
|
data = json.dumps({"reference": "mmg_reference",
|
||||||
"CID": str(sample_notification.id),
|
"CID": str(sample_notification.id),
|
||||||
"MSISDN": "447777349060",
|
"MSISDN": "447777349060",
|
||||||
"status": "00",
|
"status": 00,
|
||||||
"deliverytime": "2016-04-05 16:01:07"})
|
"deliverytime": "2016-04-05 16:01:07"})
|
||||||
|
|
||||||
response = client.post(path='notifications/sms/mmg',
|
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)
|
json_data = json.loads(response.data)
|
||||||
assert json_data['result'] == 'success'
|
assert json_data['result'] == 'success'
|
||||||
assert json_data['message'] == 'MMG callback succeeded. reference {} updated'.format(sample_notification.id)
|
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):
|
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",
|
data = json.dumps({"reference": "mmg_reference",
|
||||||
"monkey": 'random thing',
|
"monkey": 'random thing',
|
||||||
"MSISDN": "447777349060",
|
"MSISDN": "447777349060",
|
||||||
"no_status": "00",
|
"no_status": 00,
|
||||||
"deliverytime": "2016-04-05 16:01:07"})
|
"deliverytime": "2016-04-05 16:01:07"})
|
||||||
|
|
||||||
response = client.post(path='notifications/sms/mmg',
|
response = client.post(path='notifications/sms/mmg',
|
||||||
|
|||||||
Reference in New Issue
Block a user