Merge pull request #242 from alphagov/mmg-client-fix

Update to processing the the response from MMG
This commit is contained in:
Rebecca Law
2016-04-20 11:27:37 +01:00
6 changed files with 52 additions and 13 deletions

View File

@@ -12,6 +12,12 @@ mmg_response_map = {
"success": True,
"notification_status": 'delivered'
},
'0': {
"message": 'Delivered',
"notification_statistics_status": STATISTICS_DELIVERED,
"success": True,
"notification_status": 'delivered'
},
'default': {
"message": 'Declined',
"success": False,

View File

@@ -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

View File

@@ -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:

View File

@@ -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'

View File

@@ -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')

View File

@@ -1318,8 +1318,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
@@ -1345,8 +1344,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
@@ -1387,11 +1385,8 @@ def test_firetext_callback_should_update_multiple_notification_status_sent(notif
def test_process_mmg_response_return_200_when_cid_is_send_sms_code(notify_api):
with notify_api.test_request_context():
data = json.dumps({"reference": "10100164",
"CID": "send-sms-code",
"MSISDN": "447775349060",
"status": "00",
"deliverytime": "2016-04-05 16:01:07"})
data = '{"reference": "10100164", "CID": "send-sms-code", "MSISDN": "447775349060", "status": "00", \
"deliverytime": "2016-04-05 16:01:07"}'
with notify_api.test_client() as client:
response = client.post(path='notifications/sms/mmg',
@@ -1418,6 +1413,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):
@@ -1425,7 +1439,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',