From d9bdacb5cd400c1c33ba073ba723b95abea64cb8 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 2 Jun 2017 10:14:01 +0100 Subject: [PATCH] parse datetimes from mmg inbound sms the DateRecieved field from MMG comes in with +s instead of spaces, and uriencoded (the same as how they format their messages) Make sure we decode this, and then convert to a UTC timestamp --- app/notifications/receive_notifications.py | 26 +++++++++++++++---- .../test_receive_notification.py | 17 +++++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/notifications/receive_notifications.py b/app/notifications/receive_notifications.py index ad2dcf9d0..cbe17e09e 100644 --- a/app/notifications/receive_notifications.py +++ b/app/notifications/receive_notifications.py @@ -1,7 +1,7 @@ from urllib.parse import unquote -from flask import jsonify -from flask import Blueprint, current_app, request +import iso8601 +from flask import jsonify, Blueprint, current_app, request from notifications_utils.recipients import normalise_phone_number from app import statsd_client @@ -9,6 +9,7 @@ from app.dao.services_dao import dao_fetch_services_by_sms_sender from app.dao.inbound_sms_dao import dao_create_inbound_sms from app.models import InboundSms from app.errors import register_errors +from app.utils import convert_bst_to_utc receive_notifications_blueprint = Blueprint('receive_notifications', __name__) register_errors(receive_notifications_blueprint) @@ -48,18 +49,33 @@ def receive_mmg_sms(): return 'RECEIVED', 200 -def format_message(message): +def format_mmg_message(message): return unquote(message.replace('+', ' ')) +def format_mmg_datetime(date): + """ + We expect datetimes in format 2017-05-21+11%3A56%3A11 - ie, spaces replaced with pluses, and URI encoded + (the same as UTC) + """ + orig_date = format_mmg_message(date) + parsed_datetime = iso8601.parse_date(orig_date).replace(tzinfo=None) + return convert_bst_to_utc(parsed_datetime) + + def create_inbound_mmg_sms_object(service, json): - message = format_message(json['Message']) + message = format_mmg_message(json['Message']) user_number = normalise_phone_number(json['MSISDN']) + + provider_date = json.get('DateRecieved') + if provider_date: + provider_date = format_mmg_datetime(provider_date) + inbound = InboundSms( service=service, notify_number=service.sms_sender, user_number=user_number, - provider_date=json.get('DateRecieved'), + provider_date=provider_date, provider_reference=json.get('ID'), content=message, ) diff --git a/tests/app/notifications/test_receive_notification.py b/tests/app/notifications/test_receive_notification.py index 5c583a17f..5d93e5fd3 100644 --- a/tests/app/notifications/test_receive_notification.py +++ b/tests/app/notifications/test_receive_notification.py @@ -4,7 +4,8 @@ import pytest from flask import json from app.notifications.receive_notifications import ( - format_message, + format_mmg_message, + format_mmg_datetime, create_inbound_mmg_sms_object ) @@ -36,8 +37,16 @@ def test_receive_notification_returns_received_to_mmg(client, sample_service): ('%F0%9F%93%A9+%F0%9F%93%A9+%F0%9F%93%A9', '📩 📩 📩'), ('x+%2B+y', 'x + y') ]) -def test_format_message(message, expected_output): - assert format_message(message) == expected_output +def test_format_mmg_message(message, expected_output): + assert format_mmg_message(message) == expected_output + + +@pytest.mark.parametrize('provider_date, expected_output', [ + ('2017-01-21+11%3A56%3A11', datetime(2017, 1, 21, 11, 56, 11)), + ('2017-05-21+11%3A56%3A11', datetime(2017, 5, 21, 10, 56, 11)) +]) +def test_format_mmg_datetime(provider_date, expected_output): + assert format_mmg_datetime(provider_date) == expected_output def test_create_inbound_mmg_sms_object(sample_service): @@ -46,7 +55,7 @@ def test_create_inbound_mmg_sms_object(sample_service): 'Message': 'hello+there+%F0%9F%93%A9', 'Number': 'foo', 'MSISDN': '07700 900 001', - 'DateRecieved': '2017-01-02 03:04:05', + 'DateRecieved': '2017-01-02+03%3A04%3A05', 'ID': 'bar', }