2016-10-25 18:04:03 +01:00
|
|
|
|
import pytest
|
2017-04-25 09:54:09 +01:00
|
|
|
|
from flask import current_app
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from freezegun import freeze_time
|
2018-08-16 16:25:58 +01:00
|
|
|
|
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
2017-12-04 11:12:26 +00:00
|
|
|
|
|
2016-11-11 16:47:52 +00:00
|
|
|
|
import app
|
2019-11-08 13:44:27 +00:00
|
|
|
|
from app.dao import templates_dao
|
2024-01-16 14:46:17 -05:00
|
|
|
|
from app.enums import KeyType, NotificationType, ServicePermissionType, TemplateType
|
2023-08-25 08:10:33 -07:00
|
|
|
|
from app.notifications.process_notifications import create_content_for_notification
|
2023-08-14 15:32:22 -07:00
|
|
|
|
from app.notifications.sns_cert_validator import (
|
|
|
|
|
|
VALID_SNS_TOPICS,
|
|
|
|
|
|
get_string_to_sign,
|
|
|
|
|
|
validate_sns_cert,
|
|
|
|
|
|
)
|
2016-11-11 16:47:52 +00:00
|
|
|
|
from app.notifications.validators import (
|
2023-07-13 07:25:19 -07:00
|
|
|
|
check_application_over_retention_limit,
|
2020-02-25 16:11:53 +00:00
|
|
|
|
check_if_service_can_send_files_by_email,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
check_is_message_too_long,
|
2019-11-19 17:05:50 +00:00
|
|
|
|
check_notification_content_is_not_empty,
|
2021-06-21 16:03:24 +01:00
|
|
|
|
check_rate_limiting,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
check_reply_to,
|
2017-10-30 13:36:49 +00:00
|
|
|
|
check_service_email_reply_to_id,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
check_service_over_api_rate_limit,
|
2023-09-12 10:15:07 -07:00
|
|
|
|
check_service_over_total_message_limit,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
check_service_sms_sender_id,
|
|
|
|
|
|
check_template_is_active,
|
|
|
|
|
|
check_template_is_for_notification_type,
|
2019-11-19 17:05:50 +00:00
|
|
|
|
service_can_send_to_recipient,
|
|
|
|
|
|
validate_and_format_recipient,
|
2020-03-04 17:04:11 +00:00
|
|
|
|
validate_template,
|
2017-12-15 16:51:40 +00:00
|
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.serialised_models import (
|
|
|
|
|
|
SerialisedAPIKeyCollection,
|
|
|
|
|
|
SerialisedService,
|
|
|
|
|
|
SerialisedTemplate,
|
|
|
|
|
|
)
|
2020-03-04 17:04:11 +00:00
|
|
|
|
from app.utils import get_template_instance
|
2023-07-12 14:52:40 -07:00
|
|
|
|
from app.v2.errors import BadRequestError, RateLimitError, TotalRequestsError
|
2019-10-31 15:02:30 +00:00
|
|
|
|
from tests.app.db import (
|
|
|
|
|
|
create_api_key,
|
|
|
|
|
|
create_reply_to_email,
|
|
|
|
|
|
create_service,
|
2020-07-28 11:22:19 +01:00
|
|
|
|
create_service_guest_list,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
create_service_sms_sender,
|
2019-10-31 15:02:30 +00:00
|
|
|
|
create_template,
|
|
|
|
|
|
)
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from tests.conftest import set_config
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-04 11:12:26 +00:00
|
|
|
|
# all of these tests should have redis enabled (except where we specifically disable it)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2017-12-04 11:12:26 +00:00
|
|
|
|
def enable_redis(notify_api):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
with set_config(notify_api, "REDIS_ENABLED", True):
|
2017-12-04 11:12:26 +00:00
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.TEAM, KeyType.NORMAL])
|
2023-09-21 13:26:27 -06:00
|
|
|
|
def test_check_service_over_total_message_limit_fails(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
key_type, mocker, notify_db_session
|
|
|
|
|
|
):
|
2023-03-14 16:28:38 -04:00
|
|
|
|
service = create_service()
|
2023-09-21 13:26:27 -06:00
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.redis_store.get",
|
|
|
|
|
|
return_value="250001",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TotalRequestsError) as e:
|
|
|
|
|
|
check_service_over_total_message_limit(key_type, service)
|
|
|
|
|
|
assert e.value.status_code == 429
|
|
|
|
|
|
assert e.value.message == "Exceeded total application limits (250000) for today"
|
|
|
|
|
|
assert e.value.fields == []
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.TEAM, KeyType.NORMAL])
|
2023-09-21 13:26:27 -06:00
|
|
|
|
def test_check_application_over_retention_limit_fails(
|
|
|
|
|
|
key_type, mocker, notify_db_session
|
|
|
|
|
|
):
|
|
|
|
|
|
service = create_service()
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.dao_get_notification_count_for_service",
|
2023-11-02 17:43:30 -04:00
|
|
|
|
return_value="10001",
|
2023-09-21 13:26:27 -06:00
|
|
|
|
)
|
2023-03-14 16:28:38 -04:00
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TotalRequestsError) as e:
|
2023-07-13 07:25:19 -07:00
|
|
|
|
check_application_over_retention_limit(key_type, service)
|
2023-03-14 16:28:38 -04:00
|
|
|
|
assert e.value.status_code == 429
|
2023-11-02 17:43:30 -04:00
|
|
|
|
assert e.value.message == "Exceeded total application limits (10000) for today"
|
2023-03-14 16:28:38 -04:00
|
|
|
|
assert e.value.fields == []
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type, notification_type",
|
|
|
|
|
|
[
|
|
|
|
|
|
(TemplateType.EMAIL, NotificationType.EMAIL),
|
|
|
|
|
|
(TemplateType.SMS, NotificationType.SMS),
|
2024-01-18 10:27:31 -05:00
|
|
|
|
],
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
2016-10-25 18:04:03 +01:00
|
|
|
|
def test_check_template_is_for_notification_type_pass(template_type, notification_type):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
check_template_is_for_notification_type(
|
|
|
|
|
|
notification_type=notification_type, template_type=template_type
|
|
|
|
|
|
)
|
|
|
|
|
|
is None
|
|
|
|
|
|
)
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type, notification_type",
|
|
|
|
|
|
[
|
|
|
|
|
|
(TemplateType.SMS, NotificationType.EMAIL),
|
|
|
|
|
|
(TemplateType.EMAIL, NotificationType.SMS),
|
2024-01-18 10:27:31 -05:00
|
|
|
|
],
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
2016-10-25 18:04:03 +01:00
|
|
|
|
def test_check_template_is_for_notification_type_fails_when_template_type_does_not_match_notification_type(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
template_type, notification_type
|
|
|
|
|
|
):
|
2016-10-31 12:22:26 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2023-08-23 10:35:43 -07:00
|
|
|
|
check_template_is_for_notification_type(
|
|
|
|
|
|
notification_type=notification_type, template_type=template_type
|
|
|
|
|
|
)
|
2016-11-11 10:50:38 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2024-02-21 10:24:18 -05:00
|
|
|
|
error_message = (
|
|
|
|
|
|
f"{template_type} template is not suitable for {notification_type} notification"
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
2016-10-31 12:22:26 +00:00
|
|
|
|
assert e.value.message == error_message
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert e.value.fields == [{"template": error_message}]
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_template_is_active_passes(sample_template):
|
|
|
|
|
|
assert check_template_is_active(sample_template) is None
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-28 17:10:00 +01:00
|
|
|
|
def test_check_template_is_active_fails(sample_template):
|
2016-10-25 18:04:03 +01:00
|
|
|
|
sample_template.archived = True
|
|
|
|
|
|
from app.dao.templates_dao import dao_update_template
|
2023-08-23 10:35:43 -07:00
|
|
|
|
|
2016-10-25 18:04:03 +01:00
|
|
|
|
dao_update_template(sample_template)
|
2016-10-31 12:22:26 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2016-10-25 18:04:03 +01:00
|
|
|
|
check_template_is_active(sample_template)
|
2016-10-31 12:22:26 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert e.value.message == "Template has been deleted"
|
|
|
|
|
|
assert e.value.fields == [{"template": "Template has been deleted"}]
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.TEST, KeyType.NORMAL])
|
2019-10-31 15:02:30 +00:00
|
|
|
|
def test_service_can_send_to_recipient_passes(key_type, notify_db_session):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
trial_mode_service = create_service(service_name="trial mode", restricted=True)
|
2020-06-26 13:38:58 +01:00
|
|
|
|
serialised_service = SerialisedService.from_id(trial_mode_service.id)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
service_can_send_to_recipient(
|
|
|
|
|
|
trial_mode_service.users[0].email_address, key_type, serialised_service
|
|
|
|
|
|
)
|
|
|
|
|
|
is None
|
|
|
|
|
|
)
|
|
|
|
|
|
assert (
|
|
|
|
|
|
service_can_send_to_recipient(
|
|
|
|
|
|
trial_mode_service.users[0].mobile_number, key_type, serialised_service
|
|
|
|
|
|
)
|
|
|
|
|
|
is None
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"user_number, recipient_number",
|
|
|
|
|
|
[
|
|
|
|
|
|
["+12028675309", "202-867-5309"],
|
|
|
|
|
|
["+447513332413", "+44 (07513) 332413"],
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_service_can_send_to_recipient_passes_with_non_normalized_number(
|
|
|
|
|
|
sample_service, user_number, recipient_number
|
|
|
|
|
|
):
|
2021-05-12 10:45:51 +01:00
|
|
|
|
sample_service.users[0].mobile_number = user_number
|
|
|
|
|
|
|
|
|
|
|
|
serialised_service = SerialisedService.from_id(sample_service.id)
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
2024-02-16 15:49:46 -05:00
|
|
|
|
service_can_send_to_recipient(
|
|
|
|
|
|
recipient_number,
|
|
|
|
|
|
KeyType.TEAM,
|
|
|
|
|
|
serialised_service,
|
|
|
|
|
|
)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
is None
|
|
|
|
|
|
)
|
2021-05-12 10:45:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"user_email, recipient_email",
|
|
|
|
|
|
[
|
|
|
|
|
|
["test@example.com", "TeSt@EXAMPLE.com"],
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_service_can_send_to_recipient_passes_with_non_normalized_email(
|
|
|
|
|
|
sample_service, user_email, recipient_email
|
|
|
|
|
|
):
|
2021-05-12 10:45:51 +01:00
|
|
|
|
sample_service.users[0].email_address = user_email
|
|
|
|
|
|
|
|
|
|
|
|
serialised_service = SerialisedService.from_id(sample_service.id)
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
2024-02-16 15:49:46 -05:00
|
|
|
|
service_can_send_to_recipient(recipient_email, KeyType.TEAM, serialised_service)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
is None
|
|
|
|
|
|
)
|
2021-05-12 10:45:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.TEST, KeyType.NORMAL])
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(
|
|
|
|
|
|
key_type, sample_service
|
|
|
|
|
|
):
|
2020-06-26 13:38:58 +01:00
|
|
|
|
serialised_service = SerialisedService.from_id(sample_service.id)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
service_can_send_to_recipient(
|
|
|
|
|
|
"some_other_email@test.com", key_type, serialised_service
|
|
|
|
|
|
)
|
|
|
|
|
|
is None
|
|
|
|
|
|
)
|
|
|
|
|
|
assert (
|
|
|
|
|
|
service_can_send_to_recipient("07513332413", key_type, serialised_service)
|
|
|
|
|
|
is None
|
|
|
|
|
|
)
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_service_can_send_to_recipient_passes_for_guest_list_recipient_passes(
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
):
|
2020-07-28 11:22:19 +01:00
|
|
|
|
create_service_guest_list(sample_service, email_address="some_other_email@test.com")
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
service_can_send_to_recipient(
|
2024-02-16 15:49:46 -05:00
|
|
|
|
"some_other_email@test.com", KeyType.TEAM, sample_service
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
is None
|
|
|
|
|
|
)
|
|
|
|
|
|
create_service_guest_list(sample_service, mobile_number="2028675309")
|
2024-02-16 15:49:46 -05:00
|
|
|
|
assert (
|
|
|
|
|
|
service_can_send_to_recipient(
|
|
|
|
|
|
"2028675309",
|
|
|
|
|
|
KeyType.TEAM,
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
)
|
|
|
|
|
|
is None
|
|
|
|
|
|
)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"recipient",
|
|
|
|
|
|
[
|
|
|
|
|
|
{"email_address": "some_other_email@test.com"},
|
|
|
|
|
|
{"mobile_number": "2028675300"},
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2020-07-28 11:22:19 +01:00
|
|
|
|
def test_service_can_send_to_recipient_fails_when_ignoring_guest_list(
|
2018-01-17 15:20:04 +00:00
|
|
|
|
notify_db_session,
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
recipient,
|
|
|
|
|
|
):
|
2020-07-28 11:22:19 +01:00
|
|
|
|
create_service_guest_list(sample_service, **recipient)
|
2018-01-17 15:20:04 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as exec_info:
|
|
|
|
|
|
service_can_send_to_recipient(
|
|
|
|
|
|
next(iter(recipient.values())),
|
2024-02-16 15:49:46 -05:00
|
|
|
|
KeyType.TEAM,
|
2018-01-17 15:20:04 +00:00
|
|
|
|
sample_service,
|
2020-07-28 10:19:46 +01:00
|
|
|
|
allow_guest_list_recipients=False,
|
2018-01-17 15:20:04 +00:00
|
|
|
|
)
|
|
|
|
|
|
assert exec_info.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
exec_info.value.message
|
|
|
|
|
|
== "Can’t send to this recipient using a team-only API key"
|
|
|
|
|
|
)
|
2018-01-17 15:20:04 +00:00
|
|
|
|
assert exec_info.value.fields == []
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.mark.parametrize("recipient", ["2028675300", "some_other_email@test.com"])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"key_type, error_message",
|
|
|
|
|
|
[
|
2024-02-16 15:49:46 -05:00
|
|
|
|
(KeyType.TEAM, "Can’t send to this recipient using a team-only API key"),
|
2023-08-23 10:35:43 -07:00
|
|
|
|
(
|
2024-02-16 15:49:46 -05:00
|
|
|
|
KeyType.NORMAL,
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"Can’t send to this recipient when service is in trial mode – see https://www.notifications.service.gov.uk/trial-mode", # noqa
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
) # noqa
|
2019-10-31 15:02:30 +00:00
|
|
|
|
def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(
|
|
|
|
|
|
recipient,
|
|
|
|
|
|
key_type,
|
|
|
|
|
|
error_message,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
trial_mode_service = create_service(service_name="trial mode", restricted=True)
|
2016-10-31 12:22:26 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as exec_info:
|
2023-08-23 10:35:43 -07:00
|
|
|
|
service_can_send_to_recipient(recipient, key_type, trial_mode_service)
|
2016-10-31 12:22:26 +00:00
|
|
|
|
assert exec_info.value.status_code == 400
|
|
|
|
|
|
assert exec_info.value.message == error_message
|
|
|
|
|
|
assert exec_info.value.fields == []
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
):
|
2016-10-31 12:22:26 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2024-02-16 15:49:46 -05:00
|
|
|
|
service_can_send_to_recipient("0758964221", KeyType.TEAM, sample_service)
|
2016-10-31 12:22:26 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert e.value.message == "Can’t send to this recipient using a team-only API key"
|
2016-10-31 12:22:26 +00:00
|
|
|
|
assert e.value.fields == []
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.mark.parametrize("char_count", [612, 0, 494, 200, 918])
|
|
|
|
|
|
@pytest.mark.parametrize("show_prefix", [True, False])
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL])
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_is_message_too_long_passes(
|
|
|
|
|
|
notify_db_session, show_prefix, char_count, template_type
|
|
|
|
|
|
):
|
2020-03-04 17:04:11 +00:00
|
|
|
|
service = create_service(prefix_sms=show_prefix)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
t = create_template(
|
|
|
|
|
|
service=service, content="a" * char_count, template_type=template_type
|
|
|
|
|
|
)
|
|
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
|
|
|
|
|
template_id=t.id, service_id=service.id
|
|
|
|
|
|
)
|
2020-03-04 17:04:11 +00:00
|
|
|
|
template_with_content = get_template_instance(template=template.__dict__, values={})
|
2020-11-09 15:19:00 +00:00
|
|
|
|
assert check_is_message_too_long(template_with_content) is None
|
2016-10-27 11:46:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.mark.parametrize("char_count", [919, 6000])
|
|
|
|
|
|
@pytest.mark.parametrize("show_prefix", [True, False])
|
2020-11-09 15:19:00 +00:00
|
|
|
|
def test_check_is_message_too_long_fails(notify_db_session, show_prefix, char_count):
|
2016-10-31 12:22:26 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2020-03-04 17:04:11 +00:00
|
|
|
|
service = create_service(prefix_sms=show_prefix)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
t = create_template(
|
2024-02-16 15:49:46 -05:00
|
|
|
|
service=service, content="a" * char_count, template_type=TemplateType.SMS
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
|
|
|
|
|
template_id=t.id, service_id=service.id
|
|
|
|
|
|
)
|
|
|
|
|
|
template_with_content = get_template_instance(
|
|
|
|
|
|
template=template.__dict__, values={}
|
|
|
|
|
|
)
|
2020-11-09 15:19:00 +00:00
|
|
|
|
check_is_message_too_long(template_with_content)
|
2016-10-31 12:22:26 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
expected_message = (
|
|
|
|
|
|
f"Your message is too long. "
|
|
|
|
|
|
f"Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. "
|
|
|
|
|
|
f"Your message is {char_count} characters long."
|
|
|
|
|
|
)
|
2020-11-09 15:19:00 +00:00
|
|
|
|
assert e.value.message == expected_message
|
2016-10-31 12:22:26 +00:00
|
|
|
|
assert e.value.fields == []
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-11-09 15:19:00 +00:00
|
|
|
|
def test_check_is_message_too_long_passes_for_long_email(sample_service):
|
2020-12-18 15:10:45 +00:00
|
|
|
|
email_character_count = 2_000_001
|
2023-08-23 10:35:43 -07:00
|
|
|
|
t = create_template(
|
|
|
|
|
|
service=sample_service,
|
|
|
|
|
|
content="a" * email_character_count,
|
2024-02-16 15:49:46 -05:00
|
|
|
|
template_type=TemplateType.EMAIL,
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(
|
|
|
|
|
|
template_id=t.id, service_id=t.service_id
|
|
|
|
|
|
)
|
2020-03-04 17:04:11 +00:00
|
|
|
|
template_with_content = get_template_instance(template=template.__dict__, values={})
|
2020-11-09 15:19:00 +00:00
|
|
|
|
template_with_content.values
|
|
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
|
|
|
|
|
check_is_message_too_long(template_with_content)
|
|
|
|
|
|
assert e.value.status_code == 400
|
2020-12-07 15:15:50 +00:00
|
|
|
|
expected_message = (
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"Your message is too long. "
|
|
|
|
|
|
+ "Emails cannot be longer than 2000000 bytes. "
|
|
|
|
|
|
+ "Your message is 2000001 bytes."
|
2020-12-07 15:15:50 +00:00
|
|
|
|
)
|
2020-11-09 15:19:00 +00:00
|
|
|
|
assert e.value.message == expected_message
|
|
|
|
|
|
assert e.value.fields == []
|
2020-03-04 17:04:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_notification_content_is_not_empty_passes(
|
|
|
|
|
|
notify_api, mocker, sample_service
|
|
|
|
|
|
):
|
2019-11-08 13:44:27 +00:00
|
|
|
|
template_id = create_template(sample_service, content="Content is not empty").id
|
2020-06-22 10:20:53 +01:00
|
|
|
|
template = SerialisedTemplate.from_id_and_service_id(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
template_id=template_id, service_id=sample_service.id
|
2019-11-08 13:44:27 +00:00
|
|
|
|
)
|
|
|
|
|
|
template_with_content = create_content_for_notification(template, {})
|
2019-11-19 17:05:50 +00:00
|
|
|
|
assert check_notification_content_is_not_empty(template_with_content) is None
|
2019-11-08 13:44:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"template_content,notification_values",
|
|
|
|
|
|
[("", {}), ("((placeholder))", {"placeholder": ""})],
|
|
|
|
|
|
)
|
2019-11-19 17:05:50 +00:00
|
|
|
|
def test_check_notification_content_is_not_empty_fails(
|
|
|
|
|
|
notify_api, mocker, sample_service, template_content, notification_values
|
|
|
|
|
|
):
|
|
|
|
|
|
template_id = create_template(sample_service, content=template_content).id
|
2020-06-22 10:20:53 +01:00
|
|
|
|
template = SerialisedTemplate.from_id_and_service_id(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
template_id=template_id, service_id=sample_service.id
|
|
|
|
|
|
)
|
|
|
|
|
|
template_with_content = create_content_for_notification(
|
|
|
|
|
|
template, notification_values
|
2019-11-08 13:44:27 +00:00
|
|
|
|
)
|
|
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2019-11-19 17:05:50 +00:00
|
|
|
|
check_notification_content_is_not_empty(template_with_content)
|
2019-11-08 13:44:27 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert e.value.message == "Your message is empty."
|
2019-11-08 13:44:27 +00:00
|
|
|
|
assert e.value.fields == []
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-04 17:04:11 +00:00
|
|
|
|
def test_validate_template(sample_service):
|
2024-02-16 15:49:46 -05:00
|
|
|
|
template = create_template(sample_service, template_type=TemplateType.EMAIL)
|
|
|
|
|
|
validate_template(template.id, {}, sample_service, NotificationType.EMAIL)
|
2020-03-04 17:04:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
2020-11-09 15:19:00 +00:00
|
|
|
|
@pytest.mark.parametrize("check_char_count", [True, False])
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_validate_template_calls_all_validators(
|
|
|
|
|
|
mocker, fake_uuid, sample_service, check_char_count
|
|
|
|
|
|
):
|
2024-02-16 15:49:46 -05:00
|
|
|
|
template = create_template(sample_service, template_type=TemplateType.EMAIL)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
mock_check_type = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_template_is_for_notification_type"
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_check_if_active = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_template_is_active"
|
|
|
|
|
|
)
|
2019-11-19 17:05:50 +00:00
|
|
|
|
mock_create_conent = mocker.patch(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"app.notifications.validators.create_content_for_notification",
|
|
|
|
|
|
return_value="content",
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_check_not_empty = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_notification_content_is_not_empty"
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_check_message_is_too_long = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_is_message_too_long"
|
|
|
|
|
|
)
|
|
|
|
|
|
template, template_with_content = validate_template(
|
2024-02-16 15:49:46 -05:00
|
|
|
|
template.id,
|
|
|
|
|
|
{},
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
NotificationType.EMAIL,
|
|
|
|
|
|
check_char_count=check_char_count,
|
2019-11-19 17:05:50 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
mock_check_type.assert_called_once_with(NotificationType.EMAIL, TemplateType.EMAIL)
|
2019-11-19 17:05:50 +00:00
|
|
|
|
mock_check_if_active.assert_called_once_with(template)
|
|
|
|
|
|
mock_create_conent.assert_called_once_with(template, {})
|
|
|
|
|
|
mock_check_not_empty.assert_called_once_with("content")
|
2020-11-09 15:19:00 +00:00
|
|
|
|
if check_char_count:
|
|
|
|
|
|
mock_check_message_is_too_long.assert_called_once_with("content")
|
|
|
|
|
|
else:
|
|
|
|
|
|
assert not mock_check_message_is_too_long.called
|
2019-11-19 17:05:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_validate_template_calls_all_validators_exception_message_too_long(
|
|
|
|
|
|
mocker, fake_uuid, sample_service
|
|
|
|
|
|
):
|
2024-02-16 15:49:46 -05:00
|
|
|
|
template = create_template(sample_service, template_type=TemplateType.EMAIL)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
mock_check_type = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_template_is_for_notification_type"
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_check_if_active = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_template_is_active"
|
|
|
|
|
|
)
|
2020-11-04 14:25:22 +00:00
|
|
|
|
mock_create_conent = mocker.patch(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"app.notifications.validators.create_content_for_notification",
|
|
|
|
|
|
return_value="content",
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_check_not_empty = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_notification_content_is_not_empty"
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_check_message_is_too_long = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_is_message_too_long"
|
|
|
|
|
|
)
|
|
|
|
|
|
template, template_with_content = validate_template(
|
2024-02-16 15:49:46 -05:00
|
|
|
|
template.id,
|
|
|
|
|
|
{},
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
NotificationType.EMAIL,
|
|
|
|
|
|
check_char_count=False,
|
2020-11-04 14:25:22 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-02-21 10:24:18 -05:00
|
|
|
|
mock_check_type.assert_called_once_with(NotificationType.EMAIL, TemplateType.EMAIL)
|
2020-11-04 14:25:22 +00:00
|
|
|
|
mock_check_if_active.assert_called_once_with(template)
|
|
|
|
|
|
mock_create_conent.assert_called_once_with(template, {})
|
|
|
|
|
|
mock_check_not_empty.assert_called_once_with("content")
|
|
|
|
|
|
assert not mock_check_message_is_too_long.called
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.TEAM, KeyType.NORMAL, KeyType.TEST])
|
2021-06-24 11:05:22 +01:00
|
|
|
|
def test_check_service_over_api_rate_limit_when_exceed_rate_limit_request_fails_raises_error(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
key_type, sample_service, mocker
|
|
|
|
|
|
):
|
2017-04-24 14:15:08 +01:00
|
|
|
|
with freeze_time("2016-01-01 12:00:00.000000"):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
mocker.patch("app.redis_store.exceeded_rate_limit", return_value=True)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample_service.restricted = True
|
2024-02-16 15:49:46 -05:00
|
|
|
|
api_key = create_api_key(sample_service, key_type=key_type)
|
2020-06-26 13:38:58 +01:00
|
|
|
|
serialised_service = SerialisedService.from_id(sample_service.id)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(
|
|
|
|
|
|
serialised_service.id
|
|
|
|
|
|
)[0]
|
2019-10-31 15:02:30 +00:00
|
|
|
|
|
2017-04-24 14:15:08 +01:00
|
|
|
|
with pytest.raises(RateLimitError) as e:
|
2020-06-26 13:38:58 +01:00
|
|
|
|
check_service_over_api_rate_limit(serialised_service, serialised_api_key)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
|
|
|
|
|
assert app.redis_store.exceeded_rate_limit.called_with(
|
2024-02-21 10:24:18 -05:00
|
|
|
|
f"{sample_service.id}-{api_key.key_type}",
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample_service.rate_limit,
|
2023-08-23 10:35:43 -07:00
|
|
|
|
60,
|
2017-04-24 14:15:08 +01:00
|
|
|
|
)
|
|
|
|
|
|
assert e.value.status_code == 429
|
2024-02-16 15:49:46 -05:00
|
|
|
|
assert e.value.message == (
|
|
|
|
|
|
f"Exceeded rate limit for key type {key_type.upper()} of "
|
|
|
|
|
|
f"{sample_service.rate_limit} requests per {60} seconds"
|
2017-04-24 14:15:08 +01:00
|
|
|
|
)
|
|
|
|
|
|
assert e.value.fields == []
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-24 11:05:22 +01:00
|
|
|
|
def test_check_service_over_api_rate_limit_when_rate_limit_has_not_exceeded_limit_succeeds(
|
2024-02-16 15:49:46 -05:00
|
|
|
|
sample_service,
|
|
|
|
|
|
mocker,
|
2023-08-23 10:35:43 -07:00
|
|
|
|
):
|
2017-04-24 14:15:08 +01:00
|
|
|
|
with freeze_time("2016-01-01 12:00:00.000000"):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
mocker.patch("app.redis_store.exceeded_rate_limit", return_value=False)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample_service.restricted = True
|
|
|
|
|
|
api_key = create_api_key(sample_service)
|
2020-06-26 13:38:58 +01:00
|
|
|
|
serialised_service = SerialisedService.from_id(sample_service.id)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(
|
|
|
|
|
|
serialised_service.id
|
|
|
|
|
|
)[0]
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
2020-06-26 13:38:58 +01:00
|
|
|
|
check_service_over_api_rate_limit(serialised_service, serialised_api_key)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
assert app.redis_store.exceeded_rate_limit.called_with(
|
2024-02-21 10:24:18 -05:00
|
|
|
|
f"{sample_service.id}-{api_key.key_type}", 3000, 60,
|
2017-04-24 14:15:08 +01:00
|
|
|
|
)
|
2017-04-25 09:54:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-06-24 11:05:22 +01:00
|
|
|
|
def test_check_service_over_api_rate_limit_should_do_nothing_if_limiting_is_disabled(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
sample_service, mocker
|
|
|
|
|
|
):
|
2017-04-25 09:54:09 +01:00
|
|
|
|
with freeze_time("2016-01-01 12:00:00.000000"):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
current_app.config["API_RATE_LIMIT_ENABLED"] = False
|
2017-04-25 09:54:09 +01:00
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
mocker.patch("app.redis_store.exceeded_rate_limit", return_value=False)
|
2017-04-25 09:54:09 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample_service.restricted = True
|
2020-06-26 13:38:58 +01:00
|
|
|
|
create_api_key(sample_service)
|
|
|
|
|
|
serialised_service = SerialisedService.from_id(sample_service.id)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
serialised_api_key = SerialisedAPIKeyCollection.from_service_id(
|
|
|
|
|
|
serialised_service.id
|
|
|
|
|
|
)[0]
|
2017-04-25 09:54:09 +01:00
|
|
|
|
|
2020-06-26 13:38:58 +01:00
|
|
|
|
check_service_over_api_rate_limit(serialised_service, serialised_api_key)
|
2021-06-24 11:05:22 +01:00
|
|
|
|
app.redis_store.exceeded_rate_limit.assert_not_called()
|
2021-06-21 16:03:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-06-24 11:05:22 +01:00
|
|
|
|
def test_check_rate_limiting_validates_api_rate_limit_and_daily_limit(
|
2021-06-21 16:03:24 +01:00
|
|
|
|
notify_db_session, mocker
|
|
|
|
|
|
):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
mock_rate_limit = mocker.patch(
|
|
|
|
|
|
"app.notifications.validators.check_service_over_api_rate_limit"
|
|
|
|
|
|
)
|
2021-06-21 16:03:24 +01:00
|
|
|
|
service = create_service()
|
2021-06-24 11:05:22 +01:00
|
|
|
|
api_key = create_api_key(service=service)
|
2021-06-21 16:03:24 +01:00
|
|
|
|
|
2021-06-24 11:05:22 +01:00
|
|
|
|
check_rate_limiting(service, api_key)
|
2021-06-21 16:03:24 +01:00
|
|
|
|
|
2021-06-24 11:05:22 +01:00
|
|
|
|
mock_rate_limit.assert_called_once_with(service, api_key)
|
2021-06-21 16:03:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.TEST, KeyType.NORMAL])
|
2021-06-24 11:05:22 +01:00
|
|
|
|
def test_validate_and_format_recipient_fails_when_international_number_and_service_does_not_allow_int_sms(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
key_type,
|
|
|
|
|
|
notify_db_session,
|
2017-10-19 11:06:28 +01:00
|
|
|
|
):
|
2024-01-18 10:26:40 -05:00
|
|
|
|
service = create_service(service_permissions=[ServicePermissionType.SMS])
|
2020-06-26 14:10:12 +01:00
|
|
|
|
service_model = SerialisedService.from_id(service.id)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2023-08-23 10:35:43 -07:00
|
|
|
|
validate_and_format_recipient(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"+20-12-1234-1234",
|
|
|
|
|
|
key_type,
|
|
|
|
|
|
service_model,
|
|
|
|
|
|
NotificationType.SMS,
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert e.value.message == "Cannot send to international mobile numbers"
|
2017-04-26 15:56:45 +01:00
|
|
|
|
assert e.value.fields == []
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-21 10:24:18 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.TEST, KeyType.NORMAL])
|
2021-06-24 11:05:22 +01:00
|
|
|
|
def test_validate_and_format_recipient_succeeds_with_international_numbers_if_service_does_allow_int_sms(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
key_type, sample_service_full_permissions
|
|
|
|
|
|
):
|
2020-06-26 14:10:12 +01:00
|
|
|
|
service_model = SerialisedService.from_id(sample_service_full_permissions.id)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
result = validate_and_format_recipient(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"+4407513332413", key_type, service_model, NotificationType.SMS
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
assert result == "+447513332413"
|
2017-10-04 14:34:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-06-24 11:05:22 +01:00
|
|
|
|
def test_validate_and_format_recipient_fails_when_no_recipient():
|
2018-01-11 14:25:40 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2024-01-18 10:27:31 -05:00
|
|
|
|
validate_and_format_recipient(
|
2024-02-21 10:24:18 -05:00
|
|
|
|
None, KeyType.NORMAL, "service", NotificationType.SMS,
|
2024-01-18 10:27:31 -05:00
|
|
|
|
)
|
2018-01-11 14:25:40 +00:00
|
|
|
|
assert e.value.status_code == 400
|
|
|
|
|
|
assert e.value.message == "Recipient can't be empty"
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"notification_type",
|
|
|
|
|
|
[NotificationType.SMS, NotificationType.EMAIL],
|
|
|
|
|
|
)
|
2017-10-30 13:36:49 +00:00
|
|
|
|
def test_check_service_email_reply_to_id_where_reply_to_id_is_none(notification_type):
|
|
|
|
|
|
assert check_service_email_reply_to_id(None, None, notification_type) is None
|
2017-10-05 11:33:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-11-01 11:01:20 +00:00
|
|
|
|
def test_check_service_email_reply_to_where_email_reply_to_is_found(sample_service):
|
|
|
|
|
|
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
check_service_email_reply_to_id(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
sample_service.id, reply_to_address.id, NotificationType.EMAIL
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
== "test@test.com"
|
|
|
|
|
|
)
|
2017-11-01 11:01:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_service_email_reply_to_id_where_service_id_is_not_found(
|
|
|
|
|
|
sample_service, fake_uuid
|
|
|
|
|
|
):
|
2017-10-05 13:22:00 +01:00
|
|
|
|
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
2017-10-05 11:33:20 +01:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2024-01-18 10:27:31 -05:00
|
|
|
|
check_service_email_reply_to_id(
|
|
|
|
|
|
fake_uuid, reply_to_address.id, NotificationType.EMAIL
|
|
|
|
|
|
)
|
2017-10-05 11:33:20 +01:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
e.value.message
|
2024-02-21 10:24:18 -05:00
|
|
|
|
== (
|
|
|
|
|
|
f"email_reply_to_id {reply_to_address.id} does not exist in database for i"
|
|
|
|
|
|
f"service id {fake_uuid}"
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2017-10-04 14:34:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_service_email_reply_to_id_where_reply_to_id_is_not_found(
|
|
|
|
|
|
sample_service, fake_uuid
|
|
|
|
|
|
):
|
2017-10-04 14:34:45 +01:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2024-01-18 10:27:31 -05:00
|
|
|
|
check_service_email_reply_to_id(
|
|
|
|
|
|
sample_service.id, fake_uuid, NotificationType.EMAIL
|
|
|
|
|
|
)
|
2017-10-04 14:34:45 +01:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
e.value.message
|
2024-02-21 10:24:18 -05:00
|
|
|
|
== (
|
|
|
|
|
|
f"email_reply_to_id {fake_uuid} does not exist in database for service "
|
|
|
|
|
|
f"id {sample_service.id}"
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2017-10-04 14:34:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"notification_type",
|
|
|
|
|
|
[NotificationType.SMS, NotificationType.EMAIL],
|
|
|
|
|
|
)
|
2017-11-01 11:01:20 +00:00
|
|
|
|
def test_check_service_sms_sender_id_where_sms_sender_id_is_none(notification_type):
|
2017-10-30 13:36:49 +00:00
|
|
|
|
assert check_service_sms_sender_id(None, None, notification_type) is None
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-01 11:01:20 +00:00
|
|
|
|
def test_check_service_sms_sender_id_where_sms_sender_id_is_found(sample_service):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456")
|
2024-01-18 10:27:31 -05:00
|
|
|
|
assert (
|
|
|
|
|
|
check_service_sms_sender_id(
|
|
|
|
|
|
sample_service.id,
|
|
|
|
|
|
sms_sender.id,
|
|
|
|
|
|
NotificationType.SMS,
|
|
|
|
|
|
)
|
|
|
|
|
|
== "123456"
|
|
|
|
|
|
)
|
2017-10-30 13:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_service_sms_sender_id_where_service_id_is_not_found(
|
|
|
|
|
|
sample_service, fake_uuid
|
|
|
|
|
|
):
|
|
|
|
|
|
sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456")
|
2017-10-30 13:36:49 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2024-01-18 10:26:40 -05:00
|
|
|
|
check_service_sms_sender_id(fake_uuid, sms_sender.id, NotificationType.SMS)
|
2017-10-30 13:36:49 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
e.value.message
|
2024-02-21 10:24:18 -05:00
|
|
|
|
== (
|
|
|
|
|
|
f"sms_sender_id {sms_sender.id} does not exist in database for service "
|
|
|
|
|
|
f"id {fake_uuid}"
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2017-10-30 13:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_service_sms_sender_id_where_sms_sender_is_not_found(
|
|
|
|
|
|
sample_service, fake_uuid
|
|
|
|
|
|
):
|
2017-10-30 13:36:49 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2024-01-18 10:26:40 -05:00
|
|
|
|
check_service_sms_sender_id(sample_service.id, fake_uuid, NotificationType.SMS)
|
2017-10-30 13:36:49 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
e.value.message
|
2024-02-21 10:24:18 -05:00
|
|
|
|
== (
|
|
|
|
|
|
f"sms_sender_id {fake_uuid} does not exist in database for service "
|
|
|
|
|
|
f"id {sample_service.id}"
|
2023-08-23 10:35:43 -07:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2017-12-15 16:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-16 15:49:46 -05:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"notification_type",
|
|
|
|
|
|
[NotificationType.SMS, NotificationType.EMAIL],
|
|
|
|
|
|
)
|
2017-12-15 16:51:40 +00:00
|
|
|
|
def test_check_reply_to_with_empty_reply_to(sample_service, notification_type):
|
|
|
|
|
|
assert check_reply_to(sample_service.id, None, notification_type) is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_reply_to_email_type(sample_service):
|
|
|
|
|
|
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
2024-01-18 10:26:40 -05:00
|
|
|
|
check_reply_to(sample_service.id, reply_to_address.id, NotificationType.EMAIL)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
== "test@test.com"
|
|
|
|
|
|
)
|
2017-12-15 16:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_reply_to_sms_type(sample_service):
|
2023-08-23 10:35:43 -07:00
|
|
|
|
sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456")
|
2024-01-18 10:27:31 -05:00
|
|
|
|
assert (
|
|
|
|
|
|
check_reply_to(sample_service.id, sms_sender.id, NotificationType.SMS)
|
|
|
|
|
|
== "123456"
|
|
|
|
|
|
)
|
2017-12-15 16:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_if_service_can_send_files_by_email_raises_if_no_contact_link_set(
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
):
|
2020-02-25 16:11:53 +00:00
|
|
|
|
with pytest.raises(BadRequestError) as e:
|
2020-02-26 16:04:15 +00:00
|
|
|
|
check_if_service_can_send_files_by_email(
|
|
|
|
|
|
service_contact_link=sample_service.contact_link,
|
2023-08-23 10:35:43 -07:00
|
|
|
|
service_id=sample_service.id,
|
2020-02-26 16:04:15 +00:00
|
|
|
|
)
|
2020-02-25 17:10:22 +00:00
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
message = (
|
|
|
|
|
|
f"Send files by email has not been set up - add contact details for your service at "
|
|
|
|
|
|
f"http://localhost:6012/services/{sample_service.id}/service-settings/send-files-by-email"
|
|
|
|
|
|
)
|
2020-02-25 16:11:53 +00:00
|
|
|
|
assert e.value.status_code == 400
|
2020-02-26 10:40:12 +00:00
|
|
|
|
assert e.value.message == message
|
2020-02-25 16:11:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 10:35:43 -07:00
|
|
|
|
def test_check_if_service_can_send_files_by_email_passes_if_contact_link_set(
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
):
|
|
|
|
|
|
sample_service.contact_link = "contact.me@gov.uk"
|
2020-02-26 16:04:15 +00:00
|
|
|
|
check_if_service_can_send_files_by_email(
|
2023-08-23 10:35:43 -07:00
|
|
|
|
service_contact_link=sample_service.contact_link, service_id=sample_service.id
|
2020-02-26 16:04:15 +00:00
|
|
|
|
)
|
2023-08-14 15:32:22 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_string_to_sign():
|
|
|
|
|
|
VALID_SNS_TOPICS.append("arn:aws:sns:us-west-2:009969138378:connector-svc-test")
|
|
|
|
|
|
sns_payload = {
|
|
|
|
|
|
"Type": "Notification",
|
|
|
|
|
|
"MessageId": "ccccccccc-cccc-cccc-cccc-ccccccccccccc",
|
|
|
|
|
|
"TopicArn": "arn:aws:sns:us-west-2:009969138378:connector-svc-test",
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"Message": '{"AbsoluteTime":"2021-09-08T13:28:24.656Z","Content":"help","ContentType":"text/plain","Id":"333333333-be0d-4a44-889d-d2a86fc06f0c","Type":"MESSAGE","ParticipantId":"bbbbbbbb-c562-4d95-b76c-dcbca8b4b5f7","DisplayName":"Jane","ParticipantRole":"CUSTOMER","InitialContactId":"33333333-abc5-46db-9ad5-d772559ab556","ContactId":"33333333-abc5-46db-9ad5-d772559ab556"}', # noqa
|
2023-08-14 15:32:22 -07:00
|
|
|
|
"Timestamp": "2021-09-08T13:28:24.860Z",
|
|
|
|
|
|
"SignatureVersion": "1",
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"Signature": "examplegggggg/1tEBYdiVDgJgBoJUniUFcArLFGfg5JCvpOr/v6LPCHiD7A0BWy8+ZOnGTmOjBMn80U9jSzYhKbHDbQHaNYTo9sRyQA31JtHHiIseQeMfTDpcaAXqfs8hdIXq4XZaJYqDFqosfbvh56VPh5QgmeHTltTc7eOZBUwnt/177eOTLTt2yB0ItMV3NAYuE1Tdxya1lLYZQUIMxETTVcRAZkDIu8TbRZC9a00q2RQVjXhDaU3k+tL+kk85syW/2ryjjkDYoUb+dyRGkqMy4aKA22UpfidOtdAZ/GGtXaXSKBqazZTEUuSEzt0duLtFntQiYJanU05gtDig==", # noqa
|
|
|
|
|
|
"SigningCertURL": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-11111111111111111111111111111111.pem", # noqa
|
|
|
|
|
|
"UnsubscribeURL": "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:000000000000:connector-svc-test:22222222-aaaa-bbbb-cccc-333333333333", # noqa
|
2023-08-14 15:32:22 -07:00
|
|
|
|
"MessageAttributes": {
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"InitialContactId": {
|
|
|
|
|
|
"Type": "String",
|
|
|
|
|
|
"Value": "33333333-abc5-46db-9ad5-d772559ab556",
|
|
|
|
|
|
},
|
2023-08-14 15:32:22 -07:00
|
|
|
|
"MessageVisibility": {"Type": "String", "Value": "ALL"},
|
|
|
|
|
|
"Type": {"Type": "String", "Value": "MESSAGE"},
|
|
|
|
|
|
"AccountId": {"Type": "String", "Value": "999999999999"},
|
|
|
|
|
|
"ContentType": {"Type": "String", "Value": "text/plain"},
|
2023-08-23 10:35:43 -07:00
|
|
|
|
"InstanceId": {
|
|
|
|
|
|
"Type": "String",
|
|
|
|
|
|
"Value": "dddddddd-b64e-40c5-921b-109fd92499ae",
|
|
|
|
|
|
},
|
|
|
|
|
|
"ContactId": {
|
|
|
|
|
|
"Type": "String",
|
|
|
|
|
|
"Value": "33333333-abc5-46db-9ad5-d772559ab556",
|
|
|
|
|
|
},
|
|
|
|
|
|
"ParticipantRole": {"Type": "String", "Value": "CUSTOMER"},
|
|
|
|
|
|
},
|
2023-08-14 15:32:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
str = get_string_to_sign(sns_payload)
|
2023-08-23 10:35:43 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
str
|
|
|
|
|
|
== b'Message\n{"AbsoluteTime":"2021-09-08T13:28:24.656Z","Content":"help","ContentType":"text/plain","Id":"333333333-be0d-4a44-889d-d2a86fc06f0c","Type":"MESSAGE","ParticipantId":"bbbbbbbb-c562-4d95-b76c-dcbca8b4b5f7","DisplayName":"Jane","ParticipantRole":"CUSTOMER","InitialContactId":"33333333-abc5-46db-9ad5-d772559ab556","ContactId":"33333333-abc5-46db-9ad5-d772559ab556"}\nMessageId\nccccccccc-cccc-cccc-cccc-ccccccccccccc\nTimestamp\n2021-09-08T13:28:24.860Z\nTopicArn\narn:aws:sns:us-west-2:009969138378:connector-svc-test\nType\nNotification\n' # noqa
|
|
|
|
|
|
)
|
2023-08-14 15:32:22 -07:00
|
|
|
|
|
|
|
|
|
|
# This is a test payload with no valid cert, so it should raise a ValueError
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
|
validate_sns_cert(sns_payload)
|
2023-09-12 10:15:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_service_over_total_message_limit(mocker, sample_service):
|
|
|
|
|
|
get_redis_mock = mocker.patch("app.notifications.validators.redis_store.get")
|
|
|
|
|
|
get_redis_mock.return_value = None
|
|
|
|
|
|
service_stats = check_service_over_total_message_limit(
|
2024-02-16 15:49:46 -05:00
|
|
|
|
KeyType.NORMAL,
|
|
|
|
|
|
sample_service,
|
2023-09-12 10:15:07 -07:00
|
|
|
|
)
|
|
|
|
|
|
assert service_stats == 0
|