2017-05-22 11:26:47 +01:00
|
|
|
from datetime import datetime
|
2017-06-02 15:58:36 +01:00
|
|
|
from unittest.mock import call
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
import pytest
|
2017-03-06 11:58:49 +00:00
|
|
|
from flask import json
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
from app.notifications.receive_notifications import (
|
2017-06-02 10:14:01 +01:00
|
|
|
format_mmg_message,
|
|
|
|
|
format_mmg_datetime,
|
2017-06-01 17:27:09 +01:00
|
|
|
create_inbound_mmg_sms_object
|
2017-05-22 11:26:47 +01:00
|
|
|
)
|
2017-03-06 11:58:49 +00:00
|
|
|
|
2017-06-01 13:13:51 +01:00
|
|
|
from app.models import InboundSms
|
2017-06-02 15:58:36 +01:00
|
|
|
from tests.app.conftest import sample_service
|
2017-05-30 11:08:59 +01:00
|
|
|
from tests.app.db import create_service
|
|
|
|
|
|
2017-03-06 11:58:49 +00:00
|
|
|
|
2017-05-22 11:26:47 +01:00
|
|
|
def test_receive_notification_returns_received_to_mmg(client, sample_service):
|
2017-03-06 11:58:49 +00:00
|
|
|
data = {"ID": "1234",
|
|
|
|
|
"MSISDN": "447700900855",
|
|
|
|
|
"Message": "Some message to notify",
|
|
|
|
|
"Trigger": "Trigger?",
|
2017-05-22 11:26:47 +01:00
|
|
|
"Number": "testing",
|
2017-03-06 11:58:49 +00:00
|
|
|
"Channel": "SMS",
|
2017-06-01 17:27:09 +01:00
|
|
|
"DateRecieved": "2012-06-27 12:33:00"
|
2017-03-06 11:58:49 +00:00
|
|
|
}
|
|
|
|
|
response = client.post(path='/notifications/sms/receive/mmg',
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=[('Content-Type', 'application/json')])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.get_data(as_text=True) == 'RECEIVED'
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('message, expected_output', [
|
|
|
|
|
('abc', 'abc'),
|
|
|
|
|
('', ''),
|
|
|
|
|
('lots+of+words', 'lots of words'),
|
|
|
|
|
('%F0%9F%93%A9+%F0%9F%93%A9+%F0%9F%93%A9', '📩 📩 📩'),
|
|
|
|
|
('x+%2B+y', 'x + y')
|
|
|
|
|
])
|
2017-06-02 10:14:01 +01:00
|
|
|
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
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
|
2017-06-01 17:27:09 +01:00
|
|
|
def test_create_inbound_mmg_sms_object(sample_service):
|
2017-05-22 11:26:47 +01:00
|
|
|
sample_service.sms_sender = 'foo'
|
|
|
|
|
data = {
|
|
|
|
|
'Message': 'hello+there+%F0%9F%93%A9',
|
|
|
|
|
'Number': 'foo',
|
|
|
|
|
'MSISDN': '07700 900 001',
|
2017-06-02 10:14:01 +01:00
|
|
|
'DateRecieved': '2017-01-02+03%3A04%3A05',
|
2017-05-22 11:26:47 +01:00
|
|
|
'ID': 'bar',
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 17:27:09 +01:00
|
|
|
inbound_sms = create_inbound_mmg_sms_object(sample_service, data)
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
assert inbound_sms.service_id == sample_service.id
|
|
|
|
|
assert inbound_sms.notify_number == 'foo'
|
2017-06-02 12:57:28 +01:00
|
|
|
assert inbound_sms.user_number == '447700900001'
|
2017-05-22 11:26:47 +01:00
|
|
|
assert inbound_sms.provider_date == datetime(2017, 1, 2, 3, 4, 5)
|
|
|
|
|
assert inbound_sms.provider_reference == 'bar'
|
|
|
|
|
assert inbound_sms._content != 'hello there 📩'
|
|
|
|
|
assert inbound_sms.content == 'hello there 📩'
|
2017-06-02 16:37:57 +01:00
|
|
|
assert inbound_sms.provider == 'mmg'
|
2017-05-30 11:08:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('notify_number', ['foo', 'baz'], ids=['two_matching_services', 'no_matching_services'])
|
|
|
|
|
def test_receive_notification_error_if_not_single_matching_service(client, notify_db_session, notify_number):
|
|
|
|
|
create_service(service_name='a', sms_sender='foo')
|
|
|
|
|
create_service(service_name='b', sms_sender='foo')
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'Message': 'hello',
|
|
|
|
|
'Number': notify_number,
|
|
|
|
|
'MSISDN': '7700900001',
|
2017-06-01 17:27:09 +01:00
|
|
|
'DateRecieved': '2017-01-02 03:04:05',
|
2017-05-30 11:08:59 +01:00
|
|
|
'ID': 'bar',
|
|
|
|
|
}
|
|
|
|
|
response = client.post(path='/notifications/sms/receive/mmg',
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=[('Content-Type', 'application/json')])
|
|
|
|
|
|
2017-06-01 13:13:51 +01:00
|
|
|
# we still return 'RECEIVED' to MMG
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.get_data(as_text=True) == 'RECEIVED'
|
|
|
|
|
assert InboundSms.query.count() == 0
|
2017-06-01 15:56:33 +01:00
|
|
|
|
|
|
|
|
|
2017-06-02 15:58:36 +01:00
|
|
|
def test_receive_notification_returns_received_to_firetext(notify_db_session, client, mocker):
|
|
|
|
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
|
|
|
|
|
|
|
|
|
create_service(service_name='b', sms_sender='07111111111')
|
|
|
|
|
|
2017-06-01 08:21:18 +01:00
|
|
|
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
2017-05-31 16:15:25 +01:00
|
|
|
response = client.post(
|
|
|
|
|
path='/notifications/sms/receive/firetext',
|
2017-06-01 08:21:18 +01:00
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
2017-05-31 16:15:25 +01:00
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
result = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
2017-06-02 15:58:36 +01:00
|
|
|
mock.assert_has_calls([call('inbound.firetext.successful')])
|
|
|
|
|
|
|
|
|
|
assert result['status'] == 'ok'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_receive_notification_from_firetext_persists_message(notify_db_session, client, mocker):
|
|
|
|
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
|
|
|
|
|
|
|
|
|
service = create_service(service_name='b', sms_sender='07111111111')
|
|
|
|
|
|
|
|
|
|
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
path='/notifications/sms/receive/firetext',
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
result = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
persisted = InboundSms.query.first()
|
|
|
|
|
|
|
|
|
|
assert result['status'] == 'ok'
|
|
|
|
|
assert persisted.notify_number == '07111111111'
|
|
|
|
|
assert persisted.user_number == '7999999999'
|
|
|
|
|
assert persisted.service == service
|
|
|
|
|
assert persisted.content == 'this is a message'
|
2017-06-02 16:37:57 +01:00
|
|
|
assert persisted.provider == 'firetext'
|
2017-06-02 15:58:36 +01:00
|
|
|
assert persisted.provider_date == datetime(2017, 1, 1, 12, 0, 0, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_receive_notification_from_firetext_persists_message_with_normalized_phone(notify_db_session, client, mocker):
|
|
|
|
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
|
|
|
|
|
|
|
|
|
create_service(service_name='b', sms_sender='07111111111')
|
|
|
|
|
|
|
|
|
|
data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
path='/notifications/sms/receive/firetext',
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
result = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
persisted = InboundSms.query.first()
|
|
|
|
|
|
|
|
|
|
assert result['status'] == 'ok'
|
|
|
|
|
assert persisted.user_number == '447999999999'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_returns_ok_to_firetext_if_mismatched_sms_sender(notify_db_session, client, mocker):
|
|
|
|
|
|
|
|
|
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
|
|
|
|
|
|
|
|
|
create_service(service_name='b', sms_sender='07111111199')
|
|
|
|
|
|
|
|
|
|
data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
path='/notifications/sms/receive/firetext',
|
|
|
|
|
data=data,
|
|
|
|
|
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
result = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
assert not InboundSms.query.all()
|
2017-05-31 16:15:25 +01:00
|
|
|
assert result['status'] == 'ok'
|
2017-06-02 15:58:36 +01:00
|
|
|
mock.assert_has_calls([call('inbound.firetext.failed')])
|