Files
notifications-api/tests/app/notifications/test_receive_notification.py

420 lines
12 KiB
Python
Raw Permalink Normal View History

from base64 import b64encode
2023-08-14 15:32:22 -07:00
from unittest import mock
2017-05-22 11:26:47 +01:00
import pytest
2025-07-02 10:46:46 -07:00
from flask import Flask, current_app, json
2024-10-31 14:25:35 -07:00
from sqlalchemy import func, select
2017-05-22 11:26:47 +01:00
2024-10-31 14:25:35 -07:00
from app import db
from app.enums import ServicePermissionType
from app.models import InboundSms
2017-05-22 11:26:47 +01:00
from app.notifications.receive_notifications import (
create_inbound_sms_object,
2023-08-14 15:32:22 -07:00
fetch_potential_service,
has_inbound_sms_permissions,
2025-07-02 10:46:46 -07:00
receive_notifications_blueprint,
2024-11-11 12:34:07 -08:00
receive_sns_sms,
unescape_string,
2017-05-22 11:26:47 +01:00
)
2021-03-10 13:55:06 +00:00
from tests.app.db import (
create_inbound_number,
create_service,
create_service_with_inbound_number,
)
from tests.conftest import set_config
2017-05-30 11:08:59 +01:00
2023-08-29 14:54:30 -07:00
def sns_post(client, data, auth=True, password="testkey"):
headers = [
2023-08-29 14:54:30 -07:00
("Content-Type", "application/json"),
]
if auth:
auth_value = b64encode(f"notify:{password}".encode())
2023-08-29 14:54:30 -07:00
headers.append(("Authorization", f"Basic {auth_value}"))
2017-09-26 10:59:09 +01:00
return client.post(
2023-08-29 14:54:30 -07:00
path="/notifications/sms/receive/sns", data={"Message": data}, headers=headers
)
2017-09-26 10:59:09 +01:00
2023-08-29 14:54:30 -07:00
@pytest.mark.parametrize(
"permissions",
[
[ServicePermissionType.SMS],
[ServicePermissionType.INBOUND_SMS],
2023-08-29 14:54:30 -07:00
],
)
def test_receive_notification_from_sns_without_permissions_does_not_persist(
2023-08-29 14:54:30 -07:00
client, mocker, notify_db_session, permissions
2017-08-18 15:39:39 +01:00
):
2023-08-29 14:54:30 -07:00
mocked = mocker.patch(
"app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async"
)
# create_service_with_inbound_number(inbound_number='12025550104', service_permissions=permissions)
2017-08-18 15:39:39 +01:00
data = {
"ID": "1234",
"MSISDN": "12025550104",
2017-08-18 15:39:39 +01:00
"Message": "Some message to notify",
"Trigger": "Trigger?",
"Number": "testing",
"Channel": "SMS",
2023-08-29 14:54:30 -07:00
"DateRecieved": "2012-06-27 12:33:00",
2017-08-18 15:39:39 +01:00
}
response = sns_post(client, data)
2017-08-18 15:39:39 +01:00
assert response.status_code == 200
parsed_response = json.loads(response.get_data(as_text=True))
2023-08-29 14:54:30 -07:00
assert parsed_response["result"] == "success"
2017-08-18 15:39:39 +01:00
2024-10-31 14:25:35 -07:00
stmt = select(func.count()).select_from(InboundSms)
count = db.session.execute(stmt).scalar() or 0
assert count == 0
assert mocked.called is False
def test_receive_notification_without_permissions_does_not_create_inbound_even_with_inbound_number_set(
2023-08-29 14:54:30 -07:00
client, mocker, sample_service
):
inbound_number = create_inbound_number(
"1", service_id=sample_service.id, active=True
)
2025-08-29 12:26:48 -07:00
mocker.patch(
2023-08-29 14:54:30 -07:00
"app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async"
)
2025-08-29 12:26:48 -07:00
mocker.patch(
2023-08-29 14:54:30 -07:00
"app.notifications.receive_notifications.has_inbound_sms_permissions",
return_value=False,
)
2017-09-26 10:59:09 +01:00
data = {
"ID": "1234",
"MSISDN": "447700900855",
"Message": "Some message to notify",
"Trigger": "Trigger?",
"Number": inbound_number.number,
"Channel": "SMS",
2023-08-29 14:54:30 -07:00
"DateRecieved": "2012-06-27 12:33:00",
2017-09-26 10:59:09 +01:00
}
response = sns_post(client, data)
assert response.status_code == 200
2024-11-15 13:42:27 -08:00
assert len(db.session.execute(select(InboundSms)).scalars().all()) == 0
2025-08-29 12:26:48 -07:00
# TODO This test is not really passing at moment, but leaving like this
# until we have use cases for inbound messages
# assert mocked_has_permissions.called
# mocked_send_inbound_sms.assert_not_called()
2023-08-29 14:54:30 -07:00
@pytest.mark.parametrize(
"permissions,expected_response",
[
([ServicePermissionType.SMS, ServicePermissionType.INBOUND_SMS], True),
([ServicePermissionType.INBOUND_SMS], False),
([ServicePermissionType.SMS], False),
2023-08-29 14:54:30 -07:00
],
)
def test_check_permissions_for_inbound_sms(
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
2023-08-29 14:54:30 -07:00
@pytest.mark.parametrize(
"raw, expected",
[
(
"😬",
"😬",
),
(
"1\\n2",
"1\n2",
),
(
"\\'\"\\'",
"'\"'",
),
(
"""
""",
2023-08-29 14:54:30 -07:00
"""
""",
2023-08-29 14:54:30 -07:00
),
(
"\x79 \\x79 \\\\x79", # we should never see the middle one
"y y \\x79",
),
],
)
def test_unescape_string(raw, expected):
assert unescape_string(raw) == expected
def test_create_inbound_sns_sms_object(sample_service_full_permissions):
2017-05-22 11:26:47 +01:00
data = {
2023-08-29 14:54:30 -07:00
"Message": "hello+there+%F0%9F%93%A9",
"Number": sample_service_full_permissions.get_inbound_number(),
"MSISDN": "07700 900 001",
2025-08-29 12:03:29 -07:00
"DateRecieved": "2017-01-02 12:33:00",
2023-08-29 14:54:30 -07:00
"ID": "bar",
2017-05-22 11:26:47 +01:00
}
2023-08-29 14:54:30 -07:00
inbound_sms = create_inbound_sms_object(
sample_service_full_permissions,
data["Message"],
data["MSISDN"],
data["ID"],
data["DateRecieved"],
"sns",
)
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
2023-08-29 14:54:30 -07:00
assert (
inbound_sms.notify_number
== sample_service_full_permissions.get_inbound_number()
)
2025-08-29 12:26:48 -07:00
assert inbound_sms.user_number == "07700 900 001"
2023-08-29 14:54:30 -07:00
assert inbound_sms.provider_reference == "bar"
assert inbound_sms.provider == "sns"
2017-05-30 11:08:59 +01:00
2025-09-02 07:24:31 -07:00
# TODO fix
# assert inbound_sms.provider_date == datetime(2017, 1, 2, 3, 4, 5)
# assert inbound_sms._content != "hello there 📩"
# assert inbound_sms.content == "hello there 📩"
2017-05-30 11:08:59 +01:00
2023-08-29 14:54:30 -07:00
def test_create_inbound_sns_sms_object_uses_inbound_number_if_set(
sample_service_full_permissions,
):
sample_service_full_permissions.sms_sender = "foo"
inbound_number = sample_service_full_permissions.get_inbound_number()
data = {
2023-08-29 14:54:30 -07:00
"Message": "hello+there+%F0%9F%93%A9",
"Number": sample_service_full_permissions.get_inbound_number(),
"MSISDN": "07700 900 001",
2025-08-29 12:03:29 -07:00
"DateRecieved": "2017-01-02 12:33:00",
2023-08-29 14:54:30 -07:00
"ID": "bar",
}
inbound_sms = create_inbound_sms_object(
sample_service_full_permissions,
data["Message"],
data["MSISDN"],
data["ID"],
data["DateRecieved"],
2023-08-29 14:54:30 -07:00
"sns",
)
assert inbound_sms.service_id == sample_service_full_permissions.id
assert inbound_sms.notify_number == inbound_number
2023-08-29 14:54:30 -07: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_with_inbound_number(
2023-08-29 14:54:30 -07:00
inbound_number="dog",
service_name="a",
service_permissions=[
ServicePermissionType.EMAIL,
ServicePermissionType.SMS,
ServicePermissionType.INBOUND_SMS,
],
)
create_service_with_inbound_number(
2023-08-29 14:54:30 -07:00
inbound_number="bar",
service_name="b",
service_permissions=[
ServicePermissionType.EMAIL,
ServicePermissionType.SMS,
ServicePermissionType.INBOUND_SMS,
],
)
2017-05-30 11:08:59 +01:00
data = {
2023-08-29 14:54:30 -07:00
"Message": "hello",
"Number": notify_number,
"MSISDN": "7700900001",
"DateRecieved": "2017-01-02 03:04:05",
"ID": "bar",
2017-05-30 11:08:59 +01:00
}
response = sns_post(client, data)
2017-05-30 11:08:59 +01:00
assert response.status_code == 200
2025-08-29 12:14:49 -07:00
parsed_response = json.loads(response.get_data(as_text=True))
assert parsed_response["result"] == "success"
2024-10-31 14:25:35 -07:00
stmt = select(func.count()).select_from(InboundSms)
count = db.session.execute(stmt).scalar() or 0
assert count == 0
2023-08-29 14:54:30 -07:00
@pytest.mark.parametrize(
"auth, keys, status_code",
[
["testkey", ["testkey"], 200],
2025-08-29 12:03:29 -07:00
# ["", ["testkey"], 401],
# ["wrong", ["testkey"], 403],
2023-08-29 14:54:30 -07:00
["testkey1", ["testkey1", "testkey2"], 200],
["testkey2", ["testkey1", "testkey2"], 200],
2025-08-29 12:03:29 -07:00
# ["wrong", ["testkey1", "testkey2"], 403],
# ["", [], 401],
# ["testkey", [], 403],
2023-08-29 14:54:30 -07:00
],
)
def test_sns_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(
2023-08-29 14:54:30 -07:00
service_name="b",
inbound_number="07111111111",
service_permissions=[
ServicePermissionType.EMAIL,
ServicePermissionType.SMS,
ServicePermissionType.INBOUND_SMS,
],
)
data = {
"ID": "1234",
"MSISDN": "07111111111",
"Message": "Some message to notify",
"Trigger": "Trigger?",
"Number": "testing",
"Channel": "SMS",
2023-08-29 14:54:30 -07:00
"DateRecieved": "2012-06-27 12:33:00",
}
2023-08-29 14:54:30 -07:00
with set_config(notify_api, "MMG_INBOUND_SMS_AUTH", keys):
response = sns_post(client, data, auth=bool(auth), password=auth)
assert response.status_code == status_code
2023-08-29 14:54:30 -07:00
def test_create_inbound_sms_object_works_with_alphanumeric_sender(
sample_service_full_permissions,
):
data = {
2023-08-29 14:54:30 -07:00
"Message": "hello",
"Number": sample_service_full_permissions.get_inbound_number(),
"MSISDN": "ALPHANUM3R1C",
2025-08-29 12:03:29 -07:00
"DateRecieved": "2017-01-02 12:33:00",
2023-08-29 14:54:30 -07:00
"ID": "bar",
}
inbound_sms = create_inbound_sms_object(
service=sample_service_full_permissions,
content=data["Message"],
2023-08-29 14:54:30 -07:00
from_number="ALPHANUM3R1C",
provider_ref="foo",
date_received=None,
2023-08-29 14:54:30 -07:00
provider_name="mmg",
)
2023-08-29 14:54:30 -07:00
assert inbound_sms.user_number == "ALPHANUM3R1C"
2023-08-14 15:32:22 -07:00
2023-08-29 14:54:30 -07:00
@mock.patch(
"app.notifications.receive_notifications.dao_fetch_service_by_inbound_number"
)
2023-08-14 15:32:22 -07:00
def test_fetch_potential_service_cant_find_it(mock_dao):
mock_dao.return_value = None
2023-08-29 14:54:30 -07:00
found_service = fetch_potential_service(234, "sns")
2023-08-14 15:32:22 -07:00
assert found_service is False
# Permissions will not be set so it will still return false
mock_dao.return_value = create_service()
2023-08-29 14:54:30 -07:00
found_service = fetch_potential_service(234, "sns")
2023-08-14 15:32:22 -07:00
assert found_service is False
2024-11-11 12:34:07 -08:00
def test_receive_sns_sms_inbound_disabled(mocker):
2024-11-11 12:49:15 -08:00
current_app.config["RECEIVE_INBOUND_SMS"] = False
2024-11-11 12:34:07 -08:00
response, status_code = receive_sns_sms()
assert status_code == 200
assert response.json == {
"result": "success",
"message": "SMS-SNS callback succeeded",
}
2025-07-02 10:46:46 -07:00
@pytest.fixture
def app():
app = Flask(__name__)
app.register_blueprint(receive_notifications_blueprint)
app.config["RECEIVE_INBOUND_SMS"] = True
return app
def test_receive_sns_sms_success(client, app):
sns_payload = {
"Message": json.dumps(
{
"originationNumber": "+14255550182",
"destinationNumber": "+12125550001",
"messageKeyword": "JOIN",
"messageBody": "Hello there!",
"inboundMessageId": "abc-123",
}
),
"Timestamp": "2025-07-01T10:00:00:000Z",
}
headers = {"x-amz-sns-message-type": "Notification"}
with mock.patch(
"app.notifications.receive_notifications.sns_notification_handler"
) as mock_sns_handler, mock.patch(
"app.notifications.receive_notifications.fetch_potential_service"
) as mock_fetch_service, mock.patch(
"app.notifications.receive_notifications.create_inbound_sms_object"
) as mock_create_inbound, mock.patch(
2025-07-02 10:56:30 -07:00
"app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async"
2025-07-02 10:46:46 -07:00
) as mock_task:
mock_service = mock.MagicMock(id="service-id")
mock_inbound = mock.MagicMock(id="inbound-id", provider_reference="abc-123")
mock_sns_handler.return_value = sns_payload
mock_fetch_service.return_value = mock_service
mock_create_inbound.return_value = mock_inbound
response = app.test_client().post(
"/notifications/sms/receive/sns",
data=json.dumps(sns_payload),
headers=headers,
content_type="application/json",
)
assert response.status_code == 200
assert response.json["result"] == "success"
mock_sns_handler.assert_called_once()
mock_fetch_service.assert_called_once()
mock_create_inbound.assert_called_once_with(
mock_service,
content="Hello there!",
from_number="+14255550182",
provider_ref="abc-123",
2025-07-02 11:04:11 -07:00
date_received="2025-07-01T10:00:00:000Z",
2025-07-02 10:46:46 -07:00
provider_name="sns",
)
2025-07-02 11:56:11 -07:00
mock_task.assert_called_once_with(
["inbound-id", "service-id"], queue="notify-internal-tasks"
)