2017-06-20 17:13:40 +01:00
|
|
|
import uuid
|
2017-11-20 15:56:31 +00:00
|
|
|
import base64
|
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
|
|
|
|
2017-08-23 13:03:52 +01:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_inbound_number
|
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-20 17:13:40 +01:00
|
|
|
create_inbound_sms_object,
|
2017-07-05 15:08:53 +01:00
|
|
|
strip_leading_forty_four,
|
|
|
|
|
has_inbound_sms_permissions,
|
2017-11-08 13:32:30 +00:00
|
|
|
unescape_string,
|
2017-05-22 11:26:47 +01:00
|
|
|
)
|
2017-03-06 11:58:49 +00:00
|
|
|
|
2017-06-29 15:33:44 +01:00
|
|
|
from app.models import InboundSms, EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE
|
2017-11-20 15:56:31 +00:00
|
|
|
from tests.conftest import set_config
|
2017-11-07 14:26:18 +00:00
|
|
|
from tests.app.db import create_inbound_number, create_service, create_service_with_inbound_number
|
2017-06-29 15:33:44 +01:00
|
|
|
from tests.app.conftest import sample_service
|
2017-05-30 11:08:59 +01:00
|
|
|
|
2017-03-06 11:58:49 +00:00
|
|
|
|
2017-11-20 15:56:31 +00:00
|
|
|
def firetext_post(client, data, auth=True, password='testkey'):
|
|
|
|
|
headers = [
|
|
|
|
|
('Content-Type', 'application/x-www-form-urlencoded'),
|
|
|
|
|
('X-Forwarded-For', '203.0.113.195, 70.41.3.18, 150.172.238.178')
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if auth:
|
|
|
|
|
auth_value = base64.b64encode("notify:{}".format(password).encode('utf-8')).decode('utf-8')
|
|
|
|
|
headers.append(('Authorization', 'Basic ' + auth_value))
|
|
|
|
|
|
2017-09-26 10:59:09 +01:00
|
|
|
return client.post(
|
|
|
|
|
path='/notifications/sms/receive/firetext',
|
|
|
|
|
data=data,
|
2017-11-20 15:56:31 +00:00
|
|
|
headers=headers
|
|
|
|
|
)
|
2017-09-26 10:59:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def mmg_post(client, data):
|
|
|
|
|
return client.post(
|
|
|
|
|
path='/notifications/sms/receive/mmg',
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
headers=[
|
|
|
|
|
('Content-Type', 'application/json'),
|
|
|
|
|
('X-Forwarded-For', '203.0.113.195, 70.41.3.18, 150.172.238.178')
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
2017-07-04 11:16:16 +01:00
|
|
|
def test_receive_notification_returns_received_to_mmg(client, mocker, sample_service_full_permissions):
|
2017-07-03 18:10:10 +01:00
|
|
|
mocked = mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
|
|
|
|
data = {
|
2017-07-04 11:16:16 +01:00
|
|
|
"ID": "1234",
|
|
|
|
|
"MSISDN": "447700900855",
|
|
|
|
|
"Message": "Some message to notify",
|
|
|
|
|
"Trigger": "Trigger?",
|
2017-08-16 12:27:42 +01:00
|
|
|
"Number": sample_service_full_permissions.get_inbound_number(),
|
2017-07-04 11:16:16 +01:00
|
|
|
"Channel": "SMS",
|
|
|
|
|
"DateRecieved": "2012-06-27 12:33:00"
|
|
|
|
|
}
|
2017-09-26 10:59:09 +01:00
|
|
|
response = mmg_post(client, data)
|
2017-03-06 11:58:49 +00:00
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
2017-07-03 18:10:10 +01:00
|
|
|
assert response.get_data(as_text=True) == 'RECEIVED'
|
2017-06-20 17:13:40 +01:00
|
|
|
inbound_sms_id = InboundSms.query.all()[0].id
|
2017-06-29 15:33:44 +01:00
|
|
|
mocked.assert_called_once_with(
|
|
|
|
|
[str(inbound_sms_id), str(sample_service_full_permissions.id)], queue="notify-internal-tasks")
|
|
|
|
|
|
|
|
|
|
|
2017-08-18 15:39:39 +01:00
|
|
|
@pytest.mark.parametrize('permissions', [
|
|
|
|
|
[SMS_TYPE],
|
|
|
|
|
[INBOUND_SMS_TYPE],
|
2017-07-03 14:10:04 +01:00
|
|
|
])
|
2017-08-18 15:39:39 +01:00
|
|
|
def test_receive_notification_from_mmg_without_permissions_does_not_persist(
|
|
|
|
|
client,
|
|
|
|
|
mocker,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
permissions
|
|
|
|
|
):
|
|
|
|
|
mocked = mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
2017-11-07 14:26:18 +00:00
|
|
|
service = create_service_with_inbound_number(inbound_number='07111111111', service_permissions=permissions)
|
2017-08-18 15:39:39 +01:00
|
|
|
data = {
|
|
|
|
|
"ID": "1234",
|
|
|
|
|
"MSISDN": "07111111111",
|
|
|
|
|
"Message": "Some message to notify",
|
|
|
|
|
"Trigger": "Trigger?",
|
|
|
|
|
"Number": "testing",
|
|
|
|
|
"Channel": "SMS",
|
|
|
|
|
"DateRecieved": "2012-06-27 12:33:00"
|
|
|
|
|
}
|
2017-09-26 10:59:09 +01:00
|
|
|
response = mmg_post(client, data)
|
2017-08-18 15:39:39 +01:00
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.get_data(as_text=True) == 'RECEIVED'
|
|
|
|
|
assert InboundSms.query.count() == 0
|
|
|
|
|
assert mocked.called is False
|
|
|
|
|
|
|
|
|
|
|
2017-06-29 15:33:44 +01:00
|
|
|
@pytest.mark.parametrize('permissions', [
|
2017-08-18 15:39:39 +01:00
|
|
|
[SMS_TYPE],
|
|
|
|
|
[INBOUND_SMS_TYPE],
|
2017-06-29 15:33:44 +01:00
|
|
|
])
|
2017-08-18 15:39:39 +01:00
|
|
|
def test_receive_notification_from_firetext_without_permissions_does_not_persist(
|
|
|
|
|
client,
|
|
|
|
|
mocker,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
permissions
|
|
|
|
|
):
|
2017-11-07 14:26:18 +00:00
|
|
|
service = create_service_with_inbound_number(inbound_number='07111111111', service_permissions=permissions)
|
2017-08-23 13:03:52 +01:00
|
|
|
mocker.patch("app.notifications.receive_notifications.dao_fetch_service_by_inbound_number",
|
|
|
|
|
return_value=service)
|
|
|
|
|
mocked_send_inbound_sms = mocker.patch(
|
|
|
|
|
"app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
|
|
|
|
mocked_has_permissions = mocker.patch(
|
|
|
|
|
"app.notifications.receive_notifications.has_inbound_sms_permissions", return_value=False)
|
2017-07-04 11:16:16 +01:00
|
|
|
|
2017-08-18 15:39:39 +01:00
|
|
|
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
2017-09-26 10:59:09 +01:00
|
|
|
response = firetext_post(client, data)
|
2017-06-29 15:33:44 +01:00
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
2017-08-18 15:39:39 +01:00
|
|
|
result = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
assert result['status'] == 'ok'
|
|
|
|
|
assert InboundSms.query.count() == 0
|
2017-08-23 13:03:52 +01:00
|
|
|
assert not mocked_send_inbound_sms.called
|
2017-07-05 15:08:53 +01:00
|
|
|
|
|
|
|
|
|
2017-08-16 16:28:56 +01:00
|
|
|
def test_receive_notification_without_permissions_does_not_create_inbound_even_with_inbound_number_set(
|
|
|
|
|
client, mocker, notify_db, notify_db_session):
|
|
|
|
|
service = sample_service(notify_db, notify_db_session, permissions=[SMS_TYPE])
|
|
|
|
|
inbound_number = create_inbound_number('1', service_id=service.id, active=True)
|
|
|
|
|
|
|
|
|
|
mocked_send_inbound_sms = mocker.patch(
|
|
|
|
|
"app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
|
|
|
|
mocked_has_permissions = mocker.patch(
|
|
|
|
|
"app.notifications.receive_notifications.has_inbound_sms_permissions", return_value=False)
|
|
|
|
|
|
2017-09-26 10:59:09 +01:00
|
|
|
data = {
|
2017-08-16 16:28:56 +01:00
|
|
|
"ID": "1234",
|
|
|
|
|
"MSISDN": "447700900855",
|
|
|
|
|
"Message": "Some message to notify",
|
|
|
|
|
"Trigger": "Trigger?",
|
|
|
|
|
"Number": inbound_number.number,
|
|
|
|
|
"Channel": "SMS",
|
|
|
|
|
"DateRecieved": "2012-06-27 12:33:00"
|
2017-09-26 10:59:09 +01:00
|
|
|
}
|
2017-08-16 16:28:56 +01:00
|
|
|
|
2017-09-26 10:59:09 +01:00
|
|
|
response = mmg_post(client, data)
|
2017-08-16 16:28:56 +01:00
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert len(InboundSms.query.all()) == 0
|
|
|
|
|
assert mocked_has_permissions.called
|
|
|
|
|
mocked_send_inbound_sms.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
2017-07-05 15:08:53 +01:00
|
|
|
@pytest.mark.parametrize('permissions,expected_response', [
|
|
|
|
|
([SMS_TYPE, INBOUND_SMS_TYPE], True),
|
|
|
|
|
([INBOUND_SMS_TYPE], False),
|
|
|
|
|
([SMS_TYPE], False),
|
|
|
|
|
])
|
|
|
|
|
def test_check_permissions_for_inbound_sms(notify_db, notify_db_session, permissions, expected_response):
|
|
|
|
|
service = create_service(service_permissions=permissions)
|
|
|
|
|
assert has_inbound_sms_permissions(service.permissions) is expected_response
|
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
|
|
|
|
|
|
|
|
|
|
|
2017-11-08 13:32:30 +00:00
|
|
|
@pytest.mark.parametrize('raw, expected', [
|
|
|
|
|
(
|
|
|
|
|
'😬',
|
|
|
|
|
'😬',
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
'1\\n2',
|
|
|
|
|
'1\n2',
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
'\\\'"\\\'',
|
|
|
|
|
'\'"\'',
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
""",
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
""",
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
'\x79 \\x79 \\\\x79', # we should never see the middle one
|
|
|
|
|
'y y \\x79',
|
|
|
|
|
),
|
|
|
|
|
])
|
|
|
|
|
def test_unescape_string(raw, expected):
|
|
|
|
|
assert unescape_string(raw) == expected
|
|
|
|
|
|
|
|
|
|
|
2017-06-02 10:14:01 +01:00
|
|
|
@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-29 15:33:44 +01:00
|
|
|
def test_create_inbound_mmg_sms_object(sample_service_full_permissions):
|
2017-05-22 11:26:47 +01:00
|
|
|
data = {
|
|
|
|
|
'Message': 'hello+there+%F0%9F%93%A9',
|
2017-08-16 12:27:42 +01:00
|
|
|
'Number': sample_service_full_permissions.get_inbound_number(),
|
2017-05-22 11:26:47 +01:00
|
|
|
'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-29 15:33:44 +01:00
|
|
|
inbound_sms = create_inbound_sms_object(sample_service_full_permissions, format_mmg_message(data["Message"]),
|
2017-06-20 17:13:40 +01:00
|
|
|
data["MSISDN"], data["ID"], data["DateRecieved"], "mmg")
|
2017-05-22 11:26:47 +01:00
|
|
|
|
2017-06-29 15:33:44 +01:00
|
|
|
assert inbound_sms.service_id == sample_service_full_permissions.id
|
2017-08-16 12:27:42 +01:00
|
|
|
assert inbound_sms.notify_number == sample_service_full_permissions.get_inbound_number()
|
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
|
|
|
|
|
|
|
|
|
2017-08-14 19:47:09 +01:00
|
|
|
def test_create_inbound_mmg_sms_object_uses_inbound_number_if_set(sample_service_full_permissions):
|
|
|
|
|
sample_service_full_permissions.sms_sender = 'foo'
|
2017-08-16 12:27:42 +01:00
|
|
|
inbound_number = sample_service_full_permissions.get_inbound_number()
|
2017-08-14 19:47:09 +01:00
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'Message': 'hello+there+%F0%9F%93%A9',
|
2017-08-16 12:27:42 +01:00
|
|
|
'Number': sample_service_full_permissions.get_inbound_number(),
|
2017-08-14 19:47:09 +01:00
|
|
|
'MSISDN': '07700 900 001',
|
|
|
|
|
'DateRecieved': '2017-01-02+03%3A04%3A05',
|
|
|
|
|
'ID': 'bar',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inbound_sms = create_inbound_sms_object(
|
|
|
|
|
sample_service_full_permissions,
|
|
|
|
|
format_mmg_message(data["Message"]),
|
|
|
|
|
data["MSISDN"],
|
|
|
|
|
data["ID"],
|
|
|
|
|
data["DateRecieved"],
|
|
|
|
|
"mmg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert inbound_sms.service_id == sample_service_full_permissions.id
|
2017-08-16 12:27:42 +01:00
|
|
|
assert inbound_sms.notify_number == inbound_number
|
2017-08-14 19:47:09 +01:00
|
|
|
|
|
|
|
|
|
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):
|
2017-11-07 14:26:18 +00:00
|
|
|
create_service_with_inbound_number(
|
|
|
|
|
inbound_number='dog',
|
2017-08-16 12:27:42 +01:00
|
|
|
service_name='a',
|
2017-11-07 14:26:18 +00:00
|
|
|
service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
|
2017-08-16 12:27:42 +01:00
|
|
|
)
|
2017-11-07 14:26:18 +00:00
|
|
|
create_service_with_inbound_number(
|
|
|
|
|
inbound_number='bar',
|
2017-08-16 12:27:42 +01:00
|
|
|
service_name='b',
|
2017-11-07 14:26:18 +00:00
|
|
|
service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
|
2017-08-16 12:27:42 +01:00
|
|
|
)
|
2017-05-30 11:08:59 +01:00
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
}
|
2017-09-26 10:59:09 +01:00
|
|
|
response = mmg_post(client, data)
|
2017-05-30 11:08:59 +01:00
|
|
|
|
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):
|
2017-06-20 17:13:40 +01:00
|
|
|
mocked = mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
2017-06-02 15:58:36 +01:00
|
|
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
|
|
|
|
|
2017-11-07 14:26:18 +00:00
|
|
|
service = create_service_with_inbound_number(
|
|
|
|
|
service_name='b', inbound_number='07111111111', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])
|
2017-06-02 15:58:36 +01:00
|
|
|
|
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-09-26 10:59:09 +01:00
|
|
|
response = firetext_post(client, data)
|
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'
|
2017-06-20 17:13:40 +01:00
|
|
|
inbound_sms_id = InboundSms.query.all()[0].id
|
|
|
|
|
mocked.assert_called_once_with([str(inbound_sms_id), str(service.id)], queue="notify-internal-tasks")
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_receive_notification_from_firetext_persists_message(notify_db_session, client, mocker):
|
2017-06-20 17:13:40 +01:00
|
|
|
mocked = mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
2017-06-05 11:51:30 +01:00
|
|
|
mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
2017-06-02 15:58:36 +01:00
|
|
|
|
2017-11-07 14:26:18 +00:00
|
|
|
service = create_service_with_inbound_number(
|
|
|
|
|
inbound_number='07111111111',
|
|
|
|
|
service_name='b',
|
|
|
|
|
service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
2017-09-26 10:59:09 +01:00
|
|
|
response = firetext_post(client, data)
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
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'
|
2017-06-05 11:51:30 +01:00
|
|
|
assert persisted.user_number == '447999999999'
|
2017-06-02 15:58:36 +01:00
|
|
|
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)
|
2017-06-20 17:13:40 +01:00
|
|
|
mocked.assert_called_once_with([str(persisted.id), str(service.id)], queue="notify-internal-tasks")
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_receive_notification_from_firetext_persists_message_with_normalized_phone(notify_db_session, client, mocker):
|
2017-06-20 17:13:40 +01:00
|
|
|
mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
2017-06-02 15:58:36 +01:00
|
|
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
|
|
|
|
|
2017-11-07 14:26:18 +00:00
|
|
|
create_service_with_inbound_number(
|
|
|
|
|
inbound_number='07111111111', service_name='b', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
2017-09-26 10:59:09 +01:00
|
|
|
response = firetext_post(client, data)
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
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):
|
2017-06-20 17:13:40 +01:00
|
|
|
mocked = mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
2017-06-02 15:58:36 +01:00
|
|
|
mock = mocker.patch('app.notifications.receive_notifications.statsd_client.incr')
|
|
|
|
|
|
2017-11-07 14:26:18 +00:00
|
|
|
create_service_with_inbound_number(
|
|
|
|
|
inbound_number='07111111199', service_name='b', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
2017-09-26 10:59:09 +01:00
|
|
|
response = firetext_post(client, data)
|
2017-06-02 15:58:36 +01:00
|
|
|
|
|
|
|
|
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')])
|
2017-06-20 17:13:40 +01:00
|
|
|
mocked.call_count == 0
|
2017-06-06 11:50:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
'number, expected',
|
|
|
|
|
[
|
|
|
|
|
('447123123123', '07123123123'),
|
|
|
|
|
('447123123144', '07123123144'),
|
|
|
|
|
('07123123123', '07123123123'),
|
|
|
|
|
('447444444444', '07444444444')
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
def test_strip_leading_country_code(number, expected):
|
|
|
|
|
assert strip_leading_forty_four(number) == expected
|
2017-11-20 15:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("auth, keys, status_code", [
|
|
|
|
|
["testkey", ["testkey"], 200],
|
|
|
|
|
["", ["testkey"], 401],
|
|
|
|
|
["wrong", ["testkey"], 403],
|
|
|
|
|
["testkey1", ["testkey1", "testkey2"], 200],
|
|
|
|
|
["testkey2", ["testkey1", "testkey2"], 200],
|
|
|
|
|
["wrong", ["testkey1", "testkey2"], 403],
|
|
|
|
|
["", [], 401],
|
|
|
|
|
["testkey", [], 403],
|
|
|
|
|
])
|
|
|
|
|
@pytest.mark.skip(reason="aborts are disabled at the moment")
|
|
|
|
|
def test_firetext_inbound_sms_auth(notify_db_session, notify_api, client, mocker, auth, keys, status_code):
|
|
|
|
|
mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
|
|
|
|
|
|
|
|
|
|
create_service_with_inbound_number(
|
|
|
|
|
service_name='b', inbound_number='07111111111', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
|
|
|
|
|
|
|
|
|
|
with set_config(notify_api, 'FIRETEXT_INBOUND_SMS_AUTH', keys):
|
|
|
|
|
response = firetext_post(client, data, auth=bool(auth), password=auth)
|
|
|
|
|
assert response.status_code == status_code
|
2017-11-23 15:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_inbound_sms_object_works_with_alphanumeric_sender(sample_service_full_permissions):
|
|
|
|
|
data = {
|
|
|
|
|
'Message': 'hello',
|
|
|
|
|
'Number': sample_service_full_permissions.get_inbound_number(),
|
|
|
|
|
'MSISDN': 'ALPHANUM3R1C',
|
|
|
|
|
'DateRecieved': '2017-01-02+03%3A04%3A05',
|
|
|
|
|
'ID': 'bar',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inbound_sms = create_inbound_sms_object(
|
|
|
|
|
service=sample_service_full_permissions,
|
|
|
|
|
content=format_mmg_message(data["Message"]),
|
|
|
|
|
from_number='ALPHANUM3R1C',
|
|
|
|
|
provider_ref='foo',
|
|
|
|
|
date_received=None,
|
|
|
|
|
provider_name="mmg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert inbound_sms.user_number == 'ALPHANUM3R1C'
|