mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Merge pull request #1009 from alphagov/mmg-inbound-datetimes
parse datetimes from mmg inbound sms
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from urllib.parse import unquote
|
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 notifications_utils.recipients import normalise_phone_number
|
||||||
|
|
||||||
from app import statsd_client
|
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.dao.inbound_sms_dao import dao_create_inbound_sms
|
||||||
from app.models import InboundSms
|
from app.models import InboundSms
|
||||||
from app.errors import register_errors
|
from app.errors import register_errors
|
||||||
|
from app.utils import convert_bst_to_utc
|
||||||
|
|
||||||
receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
|
receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
|
||||||
register_errors(receive_notifications_blueprint)
|
register_errors(receive_notifications_blueprint)
|
||||||
@@ -48,18 +49,33 @@ def receive_mmg_sms():
|
|||||||
return 'RECEIVED', 200
|
return 'RECEIVED', 200
|
||||||
|
|
||||||
|
|
||||||
def format_message(message):
|
def format_mmg_message(message):
|
||||||
return unquote(message.replace('+', ' '))
|
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):
|
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'])
|
user_number = normalise_phone_number(json['MSISDN'])
|
||||||
|
|
||||||
|
provider_date = json.get('DateRecieved')
|
||||||
|
if provider_date:
|
||||||
|
provider_date = format_mmg_datetime(provider_date)
|
||||||
|
|
||||||
inbound = InboundSms(
|
inbound = InboundSms(
|
||||||
service=service,
|
service=service,
|
||||||
notify_number=service.sms_sender,
|
notify_number=service.sms_sender,
|
||||||
user_number=user_number,
|
user_number=user_number,
|
||||||
provider_date=json.get('DateRecieved'),
|
provider_date=provider_date,
|
||||||
provider_reference=json.get('ID'),
|
provider_reference=json.get('ID'),
|
||||||
content=message,
|
content=message,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import pytest
|
|||||||
from flask import json
|
from flask import json
|
||||||
|
|
||||||
from app.notifications.receive_notifications import (
|
from app.notifications.receive_notifications import (
|
||||||
format_message,
|
format_mmg_message,
|
||||||
|
format_mmg_datetime,
|
||||||
create_inbound_mmg_sms_object
|
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', '📩 📩 📩'),
|
('%F0%9F%93%A9+%F0%9F%93%A9+%F0%9F%93%A9', '📩 📩 📩'),
|
||||||
('x+%2B+y', 'x + y')
|
('x+%2B+y', 'x + y')
|
||||||
])
|
])
|
||||||
def test_format_message(message, expected_output):
|
def test_format_mmg_message(message, expected_output):
|
||||||
assert format_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):
|
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',
|
'Message': 'hello+there+%F0%9F%93%A9',
|
||||||
'Number': 'foo',
|
'Number': 'foo',
|
||||||
'MSISDN': '07700 900 001',
|
'MSISDN': '07700 900 001',
|
||||||
'DateRecieved': '2017-01-02 03:04:05',
|
'DateRecieved': '2017-01-02+03%3A04%3A05',
|
||||||
'ID': 'bar',
|
'ID': 'bar',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user