2016-06-15 12:27:57 +01:00
|
|
|
|
import random
|
|
|
|
|
|
import string
|
|
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
|
import pytest
|
|
|
|
|
|
from flask import current_app, json
|
2016-06-15 12:27:57 +01:00
|
|
|
|
from freezegun import freeze_time
|
2016-06-30 17:32:49 +01:00
|
|
|
|
from notifications_python_client.authentication import create_jwt_token
|
2018-08-16 16:25:58 +01:00
|
|
|
|
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
import app
|
2016-09-07 13:47:22 +01:00
|
|
|
|
from app.dao import notifications_dao
|
2016-06-30 17:32:49 +01:00
|
|
|
|
from app.dao.api_key_dao import save_model_api_key
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.dao.services_dao import dao_update_service
|
2023-08-25 08:10:33 -07:00
|
|
|
|
from app.dao.templates_dao import dao_get_all_templates_for_service, dao_update_template
|
2024-01-16 15:12:57 -05:00
|
|
|
|
from app.enums import KeyType, NotificationType, TemplateType
|
2017-12-04 14:13:43 +00:00
|
|
|
|
from app.errors import InvalidRequest
|
2024-01-16 15:12:57 -05:00
|
|
|
|
from app.models import ApiKey, Notification, NotificationHistory, Template
|
2020-07-29 14:52:18 +01:00
|
|
|
|
from app.service.send_notification import send_one_off_notification
|
2020-03-17 15:41:30 +00:00
|
|
|
|
from app.v2.errors import RateLimitError
|
2021-08-04 15:12:09 +01:00
|
|
|
|
from tests import create_service_authorization_header
|
2019-10-31 15:02:30 +00:00
|
|
|
|
from tests.app.db import (
|
|
|
|
|
|
create_api_key,
|
|
|
|
|
|
create_notification,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
create_reply_to_email,
|
2019-10-31 15:02:30 +00:00
|
|
|
|
create_service,
|
2020-07-28 11:22:19 +01:00
|
|
|
|
create_service_guest_list,
|
2019-10-31 15:02:30 +00:00
|
|
|
|
create_template,
|
2019-10-28 16:31:47 +00:00
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
2024-01-18 10:26:40 -05:00
|
|
|
|
@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL])
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_create_notification_should_reject_if_missing_required_fields(
|
|
|
|
|
|
notify_api, sample_api_key, mocker, template_type
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{template_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data = {}
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_api_key.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
path=f"/notifications/{template_type}",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert "Missing data for required field." in json_resp["message"]["to"][0]
|
|
|
|
|
|
assert (
|
|
|
|
|
|
"Missing data for required field."
|
|
|
|
|
|
in json_resp["message"]["template"][0]
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
|
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "invalid", "template": sample_template.id}
|
|
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert len(json_resp["message"].keys()) == 1
|
|
|
|
|
|
assert (
|
|
|
|
|
|
"Invalid phone number: The string supplied did not seem to be a phone number."
|
|
|
|
|
|
in json_resp["message"]["to"]
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type, to",
|
|
|
|
|
|
[
|
|
|
|
|
|
(TemplateType.SMS, "+447700900855"),
|
|
|
|
|
|
(TemplateType.EMAIL, "ok@ok.com"),
|
2024-01-18 10:27:31 -05:00
|
|
|
|
],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
def test_send_notification_invalid_template_id(
|
|
|
|
|
|
notify_api, sample_template, mocker, fake_uuid, template_type, to
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{template_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to, "template": fake_uuid}
|
|
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
path=f"/notifications/{template_type}",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2020-03-04 17:04:11 +00:00
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
test_string = "Template not found"
|
|
|
|
|
|
assert test_string in json_resp["message"]
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_send_notification_with_placeholders_replaced(
|
|
|
|
|
|
notify_api, sample_email_template_with_placeholders, mocker
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "ok@ok.com",
|
|
|
|
|
|
"template": str(sample_email_template_with_placeholders.id),
|
|
|
|
|
|
"personalisation": {"name": "Jo"},
|
2016-06-15 12:27:57 +01:00
|
|
|
|
}
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_email_template_with_placeholders.service.id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response_data = json.loads(response.data)["data"]
|
|
|
|
|
|
notification_id = response_data["notification"]["id"]
|
|
|
|
|
|
data.update(
|
|
|
|
|
|
{"template_version": sample_email_template_with_placeholders.version}
|
2016-06-15 12:27:57 +01:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
|
|
mocked.assert_called_once_with([notification_id], queue="send-email-tasks")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 201
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert response_data["body"] == "Hello Jo\nThis is an email from GOV.UK"
|
|
|
|
|
|
assert response_data["subject"] == "Jo"
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"personalisation, expected_body, expected_subject",
|
|
|
|
|
|
[
|
2017-04-05 09:28:17 +01:00
|
|
|
|
(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
["Jo", "John", "Josephine"],
|
|
|
|
|
|
(
|
|
|
|
|
|
"Hello \n\n"
|
|
|
|
|
|
"* Jo\n"
|
|
|
|
|
|
"* John\n"
|
|
|
|
|
|
"* Josephine\n"
|
|
|
|
|
|
"This is an email from GOV.UK"
|
|
|
|
|
|
),
|
|
|
|
|
|
"Jo, John and Josephine",
|
2017-04-05 09:28:17 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
6,
|
|
|
|
|
|
("Hello 6\n" "This is an email from GOV.UK"),
|
|
|
|
|
|
"6",
|
2017-04-05 09:28:17 +01:00
|
|
|
|
),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2017-04-05 09:28:17 +01:00
|
|
|
|
def test_send_notification_with_placeholders_replaced_with_unusual_types(
|
2017-04-05 09:18:58 +01:00
|
|
|
|
client,
|
|
|
|
|
|
sample_email_template_with_placeholders,
|
2017-04-05 09:28:17 +01:00
|
|
|
|
mocker,
|
|
|
|
|
|
personalisation,
|
|
|
|
|
|
expected_body,
|
|
|
|
|
|
expected_subject,
|
2017-04-05 09:18:58 +01:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2017-04-05 09:18:58 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2017-04-05 09:18:58 +01:00
|
|
|
|
data=json.dumps(
|
|
|
|
|
|
{
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "ok@ok.com",
|
|
|
|
|
|
"template": str(sample_email_template_with_placeholders.id),
|
|
|
|
|
|
"personalisation": {"name": personalisation},
|
2017-04-05 09:18:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
),
|
|
|
|
|
|
headers=[
|
2023-08-29 14:54:30 -07:00
|
|
|
|
("Content-Type", "application/json"),
|
|
|
|
|
|
create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_email_template_with_placeholders.service.id
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
2017-04-05 09:18:58 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response_data = json.loads(response.data)["data"]
|
|
|
|
|
|
assert response_data["body"] == expected_body
|
|
|
|
|
|
assert response_data["subject"] == expected_subject
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"personalisation, expected_body, expected_subject",
|
|
|
|
|
|
[
|
|
|
|
|
|
(
|
|
|
|
|
|
None,
|
|
|
|
|
|
("we consider None equivalent to missing personalisation"),
|
|
|
|
|
|
"",
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2023-05-25 10:50:01 -07:00
|
|
|
|
def test_send_notification_with_placeholders_replaced_with_unusual_types_no_personalization(
|
|
|
|
|
|
client,
|
|
|
|
|
|
sample_email_template_with_placeholders,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
personalisation,
|
|
|
|
|
|
expected_body,
|
|
|
|
|
|
expected_subject,
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2023-05-25 10:50:01 -07:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2023-05-25 10:50:01 -07:00
|
|
|
|
data=json.dumps(
|
|
|
|
|
|
{
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "ok@ok.com",
|
|
|
|
|
|
"template": str(sample_email_template_with_placeholders.id),
|
|
|
|
|
|
"personalisation": {"name": personalisation},
|
2023-05-25 10:50:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
),
|
|
|
|
|
|
headers=[
|
2023-08-29 14:54:30 -07:00
|
|
|
|
("Content-Type", "application/json"),
|
|
|
|
|
|
create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_email_template_with_placeholders.service.id
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
2023-05-25 10:50:01 -07:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_not_send_notification_for_archived_template(
|
|
|
|
|
|
notify_api, sample_template
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
|
sample_template.archived = True
|
|
|
|
|
|
dao_update_template(sample_template)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
json_data = json.dumps(
|
|
|
|
|
|
{"to": "+447700900855", "template": sample_template.id}
|
|
|
|
|
|
)
|
|
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json_data,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert "Template has been deleted" in json_resp["message"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"template_type, to",
|
|
|
|
|
|
[
|
2024-01-18 10:26:40 -05:00
|
|
|
|
(TemplateType.SMS, "+447700900855"),
|
|
|
|
|
|
(TemplateType.EMAIL, "not-someone-we-trust@email-address.com"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_should_not_send_notification_if_restricted_and_not_a_service_user(
|
|
|
|
|
|
notify_api, sample_template, sample_email_template, mocker, template_type, to
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{template_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
template = (
|
2024-01-18 10:26:40 -05:00
|
|
|
|
sample_template
|
|
|
|
|
|
if template_type == TemplateType.SMS
|
|
|
|
|
|
else sample_email_template
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template.service.restricted = True
|
|
|
|
|
|
dao_update_service(template.service)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to, "template": template.id}
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
path=f"/notifications/{template_type}",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert [
|
|
|
|
|
|
(
|
|
|
|
|
|
"Can’t send to this recipient when service is in trial mode "
|
|
|
|
|
|
"– see https://www.notifications.service.gov.uk/trial-mode"
|
|
|
|
|
|
)
|
|
|
|
|
|
] == json_resp["message"]["to"]
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-01-18 10:26:40 -05:00
|
|
|
|
@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL])
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_send_notification_if_restricted_and_a_service_user(
|
|
|
|
|
|
notify_api, sample_template, sample_email_template, template_type, mocker
|
|
|
|
|
|
):
|
2016-06-30 13:21:35 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{template_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template = (
|
2024-01-18 10:27:31 -05:00
|
|
|
|
sample_template
|
|
|
|
|
|
if template_type == TemplateType.SMS
|
|
|
|
|
|
else sample_email_template
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
to = (
|
|
|
|
|
|
template.service.created_by.mobile_number
|
2024-01-18 10:26:40 -05:00
|
|
|
|
if template_type == TemplateType.SMS
|
2016-10-03 10:57:10 +01:00
|
|
|
|
else template.service.created_by.email_address
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template.service.restricted = True
|
|
|
|
|
|
dao_update_service(template.service)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to, "template": template.id}
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=template.service_id
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
path=f"/notifications/{template_type}",
|
2016-06-30 13:21:35 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2016-10-03 14:26:36 +01:00
|
|
|
|
assert mocked.called == 1
|
2016-06-30 13:21:35 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-01-18 10:26:40 -05:00
|
|
|
|
@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL])
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_not_allow_template_from_another_service(
|
|
|
|
|
|
notify_api, service_factory, sample_user, mocker, template_type
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{template_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
service_1 = service_factory.get(
|
|
|
|
|
|
"service 1", user=sample_user, email_from="service.1"
|
|
|
|
|
|
)
|
|
|
|
|
|
service_2 = service_factory.get(
|
|
|
|
|
|
"service 2", user=sample_user, email_from="service.2"
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service_2_templates = dao_get_all_templates_for_service(
|
|
|
|
|
|
service_id=service_2.id
|
|
|
|
|
|
)
|
|
|
|
|
|
to = (
|
|
|
|
|
|
sample_user.mobile_number
|
2024-01-18 10:26:40 -05:00
|
|
|
|
if template_type == TemplateType.SMS
|
2023-08-29 14:54:30 -07:00
|
|
|
|
else sample_user.email_address
|
|
|
|
|
|
)
|
|
|
|
|
|
data = {"to": to, "template": service_2_templates[0].id}
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(service_id=service_1.id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
path=f"/notifications/{template_type}",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.assert_not_called()
|
2020-03-04 17:04:11 +00:00
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
test_string = "Template not found"
|
|
|
|
|
|
assert test_string in json_resp["message"]
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
|
|
|
|
|
def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker):
|
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "202 867 5309", "template": str(sample_template.id)}
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response_data = json.loads(response.data)["data"]
|
|
|
|
|
|
notification_id = response_data["notification"]["id"]
|
2016-09-07 13:47:22 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked.assert_called_once_with([notification_id], queue="send-sms-tasks")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert notification_id
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert "subject" not in response_data
|
|
|
|
|
|
assert response_data["body"] == sample_template.content
|
|
|
|
|
|
assert response_data["template_version"] == sample_template.version
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_reject_email_notification_with_bad_email(
|
|
|
|
|
|
notify_api, sample_email_template, mocker
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
to_address = "bad-email"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to_address, "template": str(sample_email_template.service_id)}
|
|
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_email_template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
data = json.loads(response.get_data(as_text=True))
|
2016-10-03 14:26:36 +01:00
|
|
|
|
mocked.apply_async.assert_not_called()
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert data["result"] == "error"
|
|
|
|
|
|
assert data["message"]["to"][0] == "Not a valid email address"
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-03-30 10:46:23 +01:00
|
|
|
|
@freeze_time("2016-01-01 11:09:00.061258")
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_allow_valid_email_notification(
|
|
|
|
|
|
notify_api, sample_email_template, mocker
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "ok@ok.com", "template": str(sample_email_template.id)}
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_email_template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
assert response.status_code == 201
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response_data = json.loads(response.get_data(as_text=True))["data"]
|
|
|
|
|
|
notification_id = response_data["notification"]["id"]
|
2017-03-30 10:46:23 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
[notification_id], queue="send-email-tasks"
|
2016-06-15 12:27:57 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert notification_id
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert response_data["subject"] == "Email Subject"
|
|
|
|
|
|
assert response_data["body"] == sample_email_template.content
|
|
|
|
|
|
assert response_data["template_version"] == sample_email_template.version
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize("restricted", [True, False])
|
2016-06-15 12:27:57 +01:00
|
|
|
|
@freeze_time("2016-01-01 12:00:00.061258")
|
2016-09-06 14:21:36 +01:00
|
|
|
|
def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
notify_api, sample_user, mocker, restricted
|
|
|
|
|
|
):
|
2016-06-15 12:27:57 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
service = create_service(restricted=restricted, message_limit=2)
|
2024-01-18 10:26:40 -05:00
|
|
|
|
email_template = create_template(service, template_type=TemplateType.EMAIL)
|
|
|
|
|
|
sms_template = create_template(service, template_type=TemplateType.SMS)
|
2019-10-31 15:02:30 +00:00
|
|
|
|
create_notification(template=email_template)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": sample_user.mobile_number, "template": str(sms_template.id)}
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(service_id=service.id)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2016-06-15 12:27:57 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
def test_should_not_return_html_in_body(notify_api, sample_service, mocker):
|
2016-06-15 12:11:48 +01:00
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
|
with notify_api.test_client() as client:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
|
|
|
|
|
email_template = create_template(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
sample_service, template_type=TemplateType.EMAIL, content="hello\nthere"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "ok@ok.com", "template": str(email_template.id)}
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=email_template.service_id
|
|
|
|
|
|
)
|
2016-06-15 12:11:48 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2016-06-15 12:11:48 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
json.loads(response.get_data(as_text=True))["data"]["body"]
|
|
|
|
|
|
== "hello\nthere"
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_not_send_email_if_team_api_key_and_not_a_service_user(
|
|
|
|
|
|
client, sample_email_template, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2021-12-16 14:26:17 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "not-someone-we-trust@email-address.com",
|
|
|
|
|
|
"template": str(sample_email_template.id),
|
2021-12-16 14:26:17 +00:00
|
|
|
|
}
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
auth_header = create_service_authorization_header(
|
2024-01-16 15:12:57 -05:00
|
|
|
|
service_id=sample_email_template.service_id, key_type=KeyType.TEAM
|
2021-12-16 14:26:17 +00:00
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2021-12-16 14:26:17 +00:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_not_called()
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert ["Can’t send to this recipient using a team-only API key"] == json_resp[
|
|
|
|
|
|
"message"
|
|
|
|
|
|
]["to"]
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(
|
|
|
|
|
|
client, sample_template, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "2028675300",
|
|
|
|
|
|
"template": str(sample_template.id),
|
2021-12-16 14:26:17 +00:00
|
|
|
|
}
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
2024-01-16 15:12:57 -05:00
|
|
|
|
service_id=sample_template.service_id, key_type=KeyType.TEAM
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2021-12-16 14:26:17 +00:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_not_called()
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2021-12-16 14:26:17 +00:00
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert ["Can’t send to this recipient using a team-only API key"] == json_resp[
|
|
|
|
|
|
"message"
|
|
|
|
|
|
]["to"]
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_send_email_if_team_api_key_and_a_service_user(
|
|
|
|
|
|
client, sample_email_template, fake_uuid, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": sample_email_template.service.created_by.email_address,
|
|
|
|
|
|
"template": sample_email_template.id,
|
2017-07-27 17:07:14 +01:00
|
|
|
|
}
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(
|
2024-01-16 15:12:57 -05:00
|
|
|
|
service_id=sample_email_template.service_id, key_type=KeyType.TEAM
|
2021-08-04 15:12:09 +01:00
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2017-07-27 17:07:14 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-03-30 10:46:23 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
[fake_uuid], queue="send-email-tasks"
|
2017-07-27 17:07:14 +01:00
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 201
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize("restricted", [True, False])
|
|
|
|
|
|
@pytest.mark.parametrize("limit", [0, 1])
|
2016-09-28 15:48:12 +01:00
|
|
|
|
def test_should_send_sms_to_anyone_with_test_key(
|
2017-07-27 17:07:14 +01:00
|
|
|
|
client, sample_template, mocker, restricted, limit, fake_uuid
|
2016-09-28 15:48:12 +01:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
|
|
|
|
|
)
|
2016-09-28 15:48:12 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "2028675300", "template": sample_template.id}
|
2017-07-27 17:07:14 +01:00
|
|
|
|
sample_template.service.restricted = restricted
|
|
|
|
|
|
sample_template.service.message_limit = limit
|
|
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=sample_template.service,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
name="test_key",
|
2017-07-27 17:07:14 +01:00
|
|
|
|
created_by=sample_template.created_by,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
key_type=KeyType.TEST,
|
2017-07-27 17:07:14 +01:00
|
|
|
|
)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_jwt_token(
|
|
|
|
|
|
secret=api_key.secret, client_id=str(api_key.service_id)
|
|
|
|
|
|
)
|
2016-09-28 15:48:12 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2017-07-27 17:07:14 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
2024-02-20 14:40:40 -05:00
|
|
|
|
("Authorization", f"Bearer {auth_header}"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
2017-07-27 17:07:14 +01:00
|
|
|
|
)
|
|
|
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
[fake_uuid], queue="send-sms-tasks"
|
2017-07-27 17:07:14 +01:00
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 201
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize("restricted", [True, False])
|
|
|
|
|
|
@pytest.mark.parametrize("limit", [0, 1])
|
2016-08-30 11:00:22 +01:00
|
|
|
|
def test_should_send_email_to_anyone_with_test_key(
|
2017-07-27 17:07:14 +01:00
|
|
|
|
client, sample_email_template, mocker, restricted, limit, fake_uuid
|
2016-08-30 11:00:22 +01:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
|
|
|
|
|
)
|
2016-08-30 10:47:27 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "anyone123@example.com", "template": sample_email_template.id}
|
2017-07-27 17:07:14 +01:00
|
|
|
|
sample_email_template.service.restricted = restricted
|
|
|
|
|
|
sample_email_template.service.message_limit = limit
|
|
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=sample_email_template.service,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
name="test_key",
|
2017-07-27 17:07:14 +01:00
|
|
|
|
created_by=sample_email_template.created_by,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
key_type=KeyType.TEST,
|
2017-07-27 17:07:14 +01:00
|
|
|
|
)
|
|
|
|
|
|
save_model_api_key(api_key)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_jwt_token(
|
|
|
|
|
|
secret=api_key.secret, client_id=str(api_key.service_id)
|
|
|
|
|
|
)
|
2016-08-30 10:47:27 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2017-07-27 17:07:14 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
2024-02-20 14:40:40 -05:00
|
|
|
|
("Authorization", f"Bearer {auth_header}"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
2017-07-27 17:07:14 +01:00
|
|
|
|
)
|
2017-03-30 10:46:23 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
app.celery.provider_tasks.deliver_email.apply_async.assert_called_once_with(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
[fake_uuid], queue="send-email-tasks"
|
2017-07-27 17:07:14 +01:00
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 201
|
2016-08-30 10:47:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_send_sms_if_team_api_key_and_a_service_user(
|
|
|
|
|
|
client, sample_template, fake_uuid, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": sample_template.service.created_by.mobile_number,
|
|
|
|
|
|
"template": sample_template.id,
|
2017-07-27 17:07:14 +01:00
|
|
|
|
}
|
2023-08-29 14:54:30 -07:00
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=sample_template.service,
|
|
|
|
|
|
name="team_key",
|
|
|
|
|
|
created_by=sample_template.created_by,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
key_type=KeyType.TEAM,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2017-07-27 17:07:14 +01:00
|
|
|
|
save_model_api_key(api_key)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_jwt_token(
|
|
|
|
|
|
secret=api_key.secret, client_id=str(api_key.service_id)
|
|
|
|
|
|
)
|
2016-06-30 13:21:35 +01:00
|
|
|
|
|
2017-07-27 17:07:14 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2017-07-27 17:07:14 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
2024-02-20 14:45:53 -05:00
|
|
|
|
("Authorization", f"Bearer {auth_header}"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-06-30 17:32:49 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
app.celery.provider_tasks.deliver_sms.apply_async.assert_called_once_with(
|
|
|
|
|
|
[fake_uuid], queue="send-sms-tasks"
|
|
|
|
|
|
)
|
2017-07-27 17:07:14 +01:00
|
|
|
|
assert response.status_code == 201
|
2016-09-07 13:47:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"template_type,queue_name",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
[
|
|
|
|
|
|
(TemplateType.SMS, "send-sms-tasks"),
|
|
|
|
|
|
(TemplateType.EMAIL, "send-email-tasks"),
|
|
|
|
|
|
],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2017-07-20 16:17:04 +01:00
|
|
|
|
def test_should_persist_notification(
|
|
|
|
|
|
client,
|
|
|
|
|
|
sample_template,
|
|
|
|
|
|
sample_email_template,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
template_type,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
queue_name,
|
2017-07-20 16:17:04 +01:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{template_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
|
|
|
|
|
)
|
2017-07-27 17:07:14 +01:00
|
|
|
|
|
2024-01-18 10:27:31 -05:00
|
|
|
|
template = (
|
|
|
|
|
|
sample_template if template_type == TemplateType.SMS else sample_email_template
|
|
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
to = (
|
|
|
|
|
|
sample_template.service.created_by.mobile_number
|
2024-01-18 10:26:40 -05:00
|
|
|
|
if template_type == TemplateType.SMS
|
2017-07-20 16:17:04 +01:00
|
|
|
|
else sample_email_template.service.created_by.email_address
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
data = {"to": to, "template": template.id}
|
2017-07-20 16:17:04 +01:00
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=template.service,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
name="team_key",
|
2017-07-20 16:17:04 +01:00
|
|
|
|
created_by=template.created_by,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
key_type=KeyType.TEAM,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2017-07-20 16:17:04 +01:00
|
|
|
|
save_model_api_key(api_key)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_jwt_token(
|
|
|
|
|
|
secret=api_key.secret, client_id=str(api_key.service_id)
|
|
|
|
|
|
)
|
2016-09-07 13:47:22 +01:00
|
|
|
|
|
2017-07-20 16:17:04 +01:00
|
|
|
|
response = client.post(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
path=f"/notifications/{template_type}",
|
2017-07-20 16:17:04 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
2024-02-20 14:45:53 -05:00
|
|
|
|
("Authorization", f"Bearer {auth_header}"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-09-07 13:47:22 +01:00
|
|
|
|
|
2017-07-20 16:17:04 +01:00
|
|
|
|
mocked.assert_called_once_with([fake_uuid], queue=queue_name)
|
|
|
|
|
|
assert response.status_code == 201
|
2016-09-07 13:47:22 +01:00
|
|
|
|
|
2017-07-20 16:17:04 +01:00
|
|
|
|
notification = notifications_dao.get_notification_by_id(fake_uuid)
|
2024-01-16 11:21:24 -08:00
|
|
|
|
assert notification.to == "1"
|
2017-07-20 16:17:04 +01:00
|
|
|
|
assert notification.template_id == template.id
|
|
|
|
|
|
assert notification.notification_type == template_type
|
2016-09-08 16:00:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"template_type,queue_name",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
[(TemplateType.SMS, "send-sms-tasks"), (TemplateType.EMAIL, "send-email-tasks")],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2022-09-28 16:27:37 -04:00
|
|
|
|
def test_should_delete_notification_and_return_error_if_redis_fails(
|
2017-07-20 16:17:04 +01:00
|
|
|
|
client,
|
|
|
|
|
|
sample_email_template,
|
|
|
|
|
|
sample_template,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
template_type,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
queue_name,
|
2017-07-20 16:17:04 +01:00
|
|
|
|
):
|
2017-02-13 18:47:29 +00:00
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{template_type}.apply_async",
|
2023-08-29 14:54:30 -07:00
|
|
|
|
side_effect=Exception("failed to talk to redis"),
|
|
|
|
|
|
)
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
2017-02-13 18:47:29 +00:00
|
|
|
|
)
|
2017-07-27 17:07:14 +01:00
|
|
|
|
|
2024-01-18 10:27:31 -05:00
|
|
|
|
template = (
|
|
|
|
|
|
sample_template if template_type == TemplateType.SMS else sample_email_template
|
|
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
to = (
|
|
|
|
|
|
sample_template.service.created_by.mobile_number
|
2024-01-18 10:26:40 -05:00
|
|
|
|
if template_type == TemplateType.SMS
|
2017-02-13 18:47:29 +00:00
|
|
|
|
else sample_email_template.service.created_by.email_address
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
data = {"to": to, "template": template.id}
|
2017-02-13 18:47:29 +00:00
|
|
|
|
api_key = ApiKey(
|
|
|
|
|
|
service=template.service,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
name="team_key",
|
2017-02-13 18:47:29 +00:00
|
|
|
|
created_by=template.created_by,
|
2024-01-16 15:12:57 -05:00
|
|
|
|
key_type=KeyType.TEAM,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2017-02-13 18:47:29 +00:00
|
|
|
|
save_model_api_key(api_key)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_jwt_token(
|
|
|
|
|
|
secret=api_key.secret, client_id=str(api_key.service_id)
|
|
|
|
|
|
)
|
2016-09-08 16:00:18 +01:00
|
|
|
|
|
2023-07-21 11:24:22 -07:00
|
|
|
|
with pytest.raises(expected_exception=Exception) as e:
|
2017-06-19 14:58:38 +01:00
|
|
|
|
client.post(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
path=f"/notifications/{template_type}",
|
2017-06-19 14:58:38 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
2024-02-20 14:40:40 -05:00
|
|
|
|
("Authorization", f"Bearer {auth_header}"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
2017-06-19 14:58:38 +01:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert str(e.value) == "failed to talk to redis"
|
2016-09-08 16:00:18 +01:00
|
|
|
|
|
2017-07-20 16:17:04 +01:00
|
|
|
|
mocked.assert_called_once_with([fake_uuid], queue=queue_name)
|
2017-02-13 18:47:29 +00:00
|
|
|
|
assert not notifications_dao.get_notification_by_id(fake_uuid)
|
|
|
|
|
|
assert not NotificationHistory.query.get(fake_uuid)
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"to_email",
|
|
|
|
|
|
[
|
|
|
|
|
|
"simulate-delivered@notifications.service.gov.uk",
|
|
|
|
|
|
"simulate-delivered-2@notifications.service.gov.uk",
|
|
|
|
|
|
"simulate-delivered-3@notifications.service.gov.uk",
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-09-14 11:12:57 +01:00
|
|
|
|
def test_should_not_persist_notification_or_send_email_if_simulated_email(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, to_email, sample_email_template, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
apply_async = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to_email, "template": sample_email_template.id}
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_email_template.service_id
|
|
|
|
|
|
)
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2016-09-13 17:00:28 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
2016-09-14 10:25:09 +01:00
|
|
|
|
assert response.status_code == 201
|
2016-09-13 17:00:28 +01:00
|
|
|
|
apply_async.assert_not_called()
|
2016-09-14 13:07:57 +01:00
|
|
|
|
assert Notification.query.count() == 0
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-02 09:32:24 -08:00
|
|
|
|
@pytest.mark.parametrize("to_sms", ["+14254147755", "+14254147167"])
|
2016-09-14 11:12:57 +01:00
|
|
|
|
def test_should_not_persist_notification_or_send_sms_if_simulated_number(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, to_sms, sample_template, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
apply_async = mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to_sms, "template": sample_template.id}
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template.service_id
|
|
|
|
|
|
)
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2016-09-13 17:00:28 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-09-13 17:00:28 +01:00
|
|
|
|
|
2016-09-14 10:25:09 +01:00
|
|
|
|
assert response.status_code == 201
|
2016-09-13 17:00:28 +01:00
|
|
|
|
apply_async.assert_not_called()
|
2016-09-14 13:07:57 +01:00
|
|
|
|
assert Notification.query.count() == 0
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
2024-01-16 15:12:57 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.NORMAL, KeyType.TEAM])
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"notification_type, to",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
[
|
|
|
|
|
|
(TemplateType.SMS, "2028675300"),
|
|
|
|
|
|
(TemplateType.EMAIL, "non_guest_list_recipient@mail.com"),
|
|
|
|
|
|
],
|
2017-03-30 10:46:23 +01:00
|
|
|
|
)
|
2020-07-28 11:22:19 +01:00
|
|
|
|
def test_should_not_send_notification_to_non_guest_list_recipient_in_trial_mode(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_service_guest_list, notification_type, to, key_type, mocker
|
2016-10-10 10:27:57 +01:00
|
|
|
|
):
|
2020-07-28 11:22:19 +01:00
|
|
|
|
service = sample_service_guest_list.service
|
2019-10-31 15:02:30 +00:00
|
|
|
|
service.restricted = True
|
|
|
|
|
|
service.message_limit = 2
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
apply_async = mocker.patch(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{notification_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2019-10-31 15:02:30 +00:00
|
|
|
|
template = create_template(service, template_type=notification_type)
|
2020-07-28 11:22:19 +01:00
|
|
|
|
assert sample_service_guest_list.service_id == service.id
|
2020-07-28 11:23:30 +01:00
|
|
|
|
assert to not in [member.recipient for member in service.guest_list]
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
create_notification(template=template)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to, "template": str(template.id)}
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
api_key = create_api_key(service, key_type=key_type)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_jwt_token(
|
|
|
|
|
|
secret=api_key.secret, client_id=str(api_key.service_id)
|
|
|
|
|
|
)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:41:21 -05:00
|
|
|
|
path=f"/notifications/{notification_type}",
|
2016-09-27 13:48:51 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
2024-02-20 14:40:40 -05:00
|
|
|
|
("Authorization", f"Bearer {auth_header}"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
expected_response_message = (
|
2023-08-29 14:54:30 -07:00
|
|
|
|
(
|
|
|
|
|
|
"Can’t send to this recipient when service is in trial mode "
|
|
|
|
|
|
"– see https://www.notifications.service.gov.uk/trial-mode"
|
|
|
|
|
|
)
|
2024-01-16 15:12:57 -05:00
|
|
|
|
if key_type == KeyType.NORMAL
|
2023-08-29 14:54:30 -07:00
|
|
|
|
else ("Can’t send to this recipient using a team-only API key")
|
|
|
|
|
|
)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert expected_response_message in json_resp["message"]["to"]
|
2016-09-27 13:48:51 +01:00
|
|
|
|
apply_async.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize("service_restricted", [True, False])
|
2024-01-16 15:12:57 -05:00
|
|
|
|
@pytest.mark.parametrize("key_type", [KeyType.NORMAL, KeyType.TEAM])
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"notification_type, to, normalized_to",
|
|
|
|
|
|
[
|
2024-01-18 10:26:40 -05:00
|
|
|
|
(NotificationType.SMS, "2028675300", "+12028675300"),
|
|
|
|
|
|
(NotificationType.EMAIL, "guest_list_recipient@mail.com", None),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
2017-03-30 10:46:23 +01:00
|
|
|
|
)
|
2020-07-28 11:22:19 +01:00
|
|
|
|
def test_should_send_notification_to_guest_list_recipient(
|
2017-03-30 10:46:23 +01:00
|
|
|
|
client,
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample_service,
|
2017-03-30 10:46:23 +01:00
|
|
|
|
notification_type,
|
|
|
|
|
|
to,
|
2023-01-06 10:02:23 -05:00
|
|
|
|
normalized_to,
|
2017-03-30 10:46:23 +01:00
|
|
|
|
key_type,
|
|
|
|
|
|
service_restricted,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker,
|
2016-10-07 15:38:36 +01:00
|
|
|
|
):
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample_service.message_limit = 2
|
|
|
|
|
|
sample_service.restricted = service_restricted
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
apply_async = mocker.patch(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{notification_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2019-10-31 15:02:30 +00:00
|
|
|
|
template = create_template(sample_service, template_type=notification_type)
|
2024-01-18 10:26:40 -05:00
|
|
|
|
if notification_type == NotificationType.SMS:
|
2020-07-28 11:22:19 +01:00
|
|
|
|
service_guest_list = create_service_guest_list(sample_service, mobile_number=to)
|
2024-01-18 10:26:40 -05:00
|
|
|
|
elif notification_type == NotificationType.EMAIL:
|
2020-07-28 11:22:19 +01:00
|
|
|
|
service_guest_list = create_service_guest_list(sample_service, email_address=to)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2020-07-28 11:22:19 +01:00
|
|
|
|
assert service_guest_list.service_id == sample_service.id
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert (normalized_to or to) in [
|
|
|
|
|
|
member.recipient for member in sample_service.guest_list
|
|
|
|
|
|
]
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
create_notification(template=template)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to, "template": str(template.id)}
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample_key = create_api_key(sample_service, key_type=key_type)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_jwt_token(
|
|
|
|
|
|
secret=sample_key.secret, client_id=str(sample_key.service_id)
|
|
|
|
|
|
)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
path=f"/notifications/{notification_type}",
|
2016-09-27 13:48:51 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
2024-02-20 14:40:40 -05:00
|
|
|
|
("Authorization", f"Bearer {auth_header}"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-09-27 13:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 201
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["data"]["notification"]["id"]
|
|
|
|
|
|
assert json_resp["data"]["body"] == template.content
|
|
|
|
|
|
assert json_resp["data"]["template_version"] == template.version
|
2016-10-03 14:26:36 +01:00
|
|
|
|
assert apply_async.called
|
2016-09-27 16:54:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-09-23 15:46:48 +01:00
|
|
|
|
@pytest.mark.parametrize(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"notification_type, template_type, to",
|
|
|
|
|
|
[
|
2024-01-18 10:26:40 -05:00
|
|
|
|
(NotificationType.EMAIL, TemplateType.SMS, "notify@digital.fake.gov"),
|
|
|
|
|
|
(NotificationType.SMS, TemplateType.EMAIL, "+12028675309"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-09-23 15:46:48 +01:00
|
|
|
|
def test_should_error_if_notification_type_does_not_match_template_type(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_service, template_type, notification_type, to
|
2016-09-23 15:46:48 +01:00
|
|
|
|
):
|
2019-10-31 15:02:30 +00:00
|
|
|
|
template = create_template(sample_service, template_type=template_type)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": to, "template": template.id}
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(service_id=template.service_id)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response = client.post(
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"/notifications/{notification_type}",
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-09-23 15:46:48 +01:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert (
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"{template_type} template is not suitable for {notification_type} notification"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
in json_resp["message"]
|
|
|
|
|
|
)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_template_raises_invalid_request_exception_with_missing_personalisation(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sample_template_with_placeholders,
|
|
|
|
|
|
):
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template = Template.query.get(sample_template_with_placeholders.id)
|
|
|
|
|
|
from app.notifications.rest import create_template_object_for_notification
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
with pytest.raises(InvalidRequest) as e:
|
|
|
|
|
|
create_template_object_for_notification(template, {})
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert {"template": ["Missing personalisation: Name"]} == e.value.message
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-03-07 16:03:10 +00:00
|
|
|
|
def test_create_template_doesnt_raise_with_too_much_personalisation(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sample_template_with_placeholders,
|
2016-10-03 10:57:10 +01:00
|
|
|
|
):
|
|
|
|
|
|
from app.notifications.rest import create_template_object_for_notification
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template = Template.query.get(sample_template_with_placeholders.id)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
create_template_object_for_notification(template, {"name": "Jo", "extra": "stuff"})
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type, should_error",
|
|
|
|
|
|
[
|
|
|
|
|
|
(TemplateType.SMS, True),
|
|
|
|
|
|
(TemplateType.EMAIL, False),
|
2024-01-18 10:27:31 -05:00
|
|
|
|
],
|
2016-10-03 10:57:10 +01:00
|
|
|
|
)
|
|
|
|
|
|
def test_create_template_raises_invalid_request_when_content_too_large(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sample_service, template_type, should_error
|
2016-10-03 10:57:10 +01:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sample = create_template(
|
|
|
|
|
|
sample_service, template_type=template_type, content="((long_text))"
|
|
|
|
|
|
)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
template = Template.query.get(sample.id)
|
|
|
|
|
|
from app.notifications.rest import create_template_object_for_notification
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
try:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
create_template_object_for_notification(
|
|
|
|
|
|
template,
|
|
|
|
|
|
{
|
|
|
|
|
|
"long_text": "".join(
|
|
|
|
|
|
random.choice(string.ascii_uppercase + string.digits)
|
|
|
|
|
|
for _ in range(SMS_CHAR_COUNT_LIMIT + 1)
|
|
|
|
|
|
)
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
if should_error:
|
|
|
|
|
|
pytest.fail("expected an InvalidRequest")
|
|
|
|
|
|
except InvalidRequest as e:
|
|
|
|
|
|
if not should_error:
|
|
|
|
|
|
pytest.fail("do not expect an InvalidRequest")
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert e.message == {
|
|
|
|
|
|
"content": [
|
2024-02-20 14:45:53 -05:00
|
|
|
|
f"Content has a character count greater than the limit of {SMS_CHAR_COUNT_LIMIT}"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
]
|
|
|
|
|
|
}
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
"notification_type,send_to",
|
|
|
|
|
|
[
|
|
|
|
|
|
(NotificationType.SMS, "2028675309"),
|
|
|
|
|
|
(
|
|
|
|
|
|
NotificationType.EMAIL,
|
|
|
|
|
|
"sample@email.com",
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2019-10-31 15:02:30 +00:00
|
|
|
|
def test_send_notification_uses_priority_queue_when_template_is_marked_as_priority(
|
|
|
|
|
|
client,
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
notification_type,
|
|
|
|
|
|
send_to,
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sample = create_template(
|
|
|
|
|
|
sample_service, template_type=notification_type, process_type="priority"
|
|
|
|
|
|
)
|
|
|
|
|
|
mocked = mocker.patch(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
f"app.celery.provider_tasks.deliver_{notification_type}.apply_async"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": send_to, "template": str(sample.id)}
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(service_id=sample.service_id)
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
path=f"/notifications/{notification_type}",
|
2017-01-17 13:16:26 +00:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response_data = json.loads(response.data)["data"]
|
|
|
|
|
|
notification_id = response_data["notification"]["id"]
|
2017-01-17 13:16:26 +00:00
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 201
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked.assert_called_once_with([notification_id], queue="priority-tasks")
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
"notification_type, send_to",
|
|
|
|
|
|
[
|
|
|
|
|
|
(NotificationType.SMS, "2028675309"),
|
|
|
|
|
|
(
|
|
|
|
|
|
NotificationType.EMAIL,
|
|
|
|
|
|
"sample@email.com",
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
2017-04-24 14:15:08 +01:00
|
|
|
|
)
|
|
|
|
|
|
def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_service, mocker, notification_type, send_to
|
2017-04-24 14:15:08 +01:00
|
|
|
|
):
|
2019-10-31 15:02:30 +00:00
|
|
|
|
sample = create_template(sample_service, template_type=notification_type)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
persist_mock = mocker.patch("app.notifications.rest.persist_notification")
|
|
|
|
|
|
deliver_mock = mocker.patch("app.notifications.rest.send_notification_to_queue")
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
|
|
|
|
|
mocker.patch(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"app.notifications.rest.check_rate_limiting",
|
|
|
|
|
|
side_effect=RateLimitError("LIMIT", "INTERVAL", "TYPE"),
|
|
|
|
|
|
)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": send_to, "template": str(sample.id)}
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(service_id=sample.service_id)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
path=f"/notifications/{notification_type}",
|
2017-04-24 14:15:08 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
message = json.loads(response.data)["message"]
|
|
|
|
|
|
result = json.loads(response.data)["result"]
|
2017-04-24 14:15:08 +01:00
|
|
|
|
assert response.status_code == 429
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert result == "error"
|
2024-02-20 14:40:40 -05:00
|
|
|
|
assert message == (
|
|
|
|
|
|
"Exceeded rate limit for key type TYPE of LIMIT "
|
|
|
|
|
|
"requests per INTERVAL seconds"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2017-04-24 14:15:08 +01:00
|
|
|
|
|
|
|
|
|
|
assert not persist_mock.called
|
|
|
|
|
|
assert not deliver_mock.called
|
2017-05-02 10:56:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_allow_store_original_number_on_sms_notification(
|
|
|
|
|
|
client, sample_template, mocker
|
|
|
|
|
|
):
|
|
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "(202) 867-5309", "template": str(sample_template.id)}
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template.service_id
|
|
|
|
|
|
)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-06-16 16:30:03 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2017-06-16 16:30:03 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
response_data = json.loads(response.data)["data"]
|
|
|
|
|
|
notification_id = response_data["notification"]["id"]
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked.assert_called_once_with([notification_id], queue="send-sms-tasks")
|
2017-06-16 16:30:03 +01:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
assert notification_id
|
|
|
|
|
|
notifications = Notification.query.all()
|
|
|
|
|
|
assert len(notifications) == 1
|
2024-01-16 11:21:24 -08:00
|
|
|
|
assert "1" == notifications[0].to
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 17:55:02 +01:00
|
|
|
|
def test_should_not_allow_sending_to_international_number_without_international_permission(
|
|
|
|
|
|
client, sample_template, mocker
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "+(44) 7700-900 855", "template": str(sample_template.id)}
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template.service_id
|
|
|
|
|
|
)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-06-16 16:30:03 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2017-06-16 16:30:03 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-06-16 16:30:03 +01:00
|
|
|
|
assert not mocked.called
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
error_json = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert error_json["result"] == "error"
|
|
|
|
|
|
assert error_json["message"] == "Cannot send to international mobile numbers"
|
2020-06-16 17:55:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_allow_sending_to_international_number_with_international_permission(
|
|
|
|
|
|
client, sample_service_full_permissions, mocker
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2019-10-31 15:02:30 +00:00
|
|
|
|
template = create_template(sample_service_full_permissions)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": "+(44) 7700-900 855", "template": str(template.id)}
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_service_full_permissions.id
|
|
|
|
|
|
)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-06-16 16:30:03 +01:00
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2017-06-16 16:30:03 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2017-06-16 16:30:03 +01:00
|
|
|
|
assert response.status_code == 201
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
2019-10-28 16:31:47 +00:00
|
|
|
|
def test_should_not_allow_sms_notifications_if_service_permission_not_set(
|
|
|
|
|
|
client,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
sample_template_without_sms_permission,
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "+12028675309",
|
|
|
|
|
|
"template": str(sample_template_without_sms_permission.id),
|
2019-10-28 16:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template_without_sms_permission.service_id
|
|
|
|
|
|
)
|
2019-10-28 16:31:47 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/sms",
|
2019-10-28 16:31:47 +00:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2019-10-28 16:31:47 +00:00
|
|
|
|
|
|
|
|
|
|
assert not mocked.called
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
error_json = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert error_json["result"] == "error"
|
|
|
|
|
|
assert error_json["message"]["service"][0] == "Cannot send text messages"
|
2019-10-28 16:31:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_not_allow_email_notifications_if_service_permission_not_set(
|
|
|
|
|
|
client,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
sample_template_without_email_permission,
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
mocked = mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
2019-10-28 16:31:47 +00:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "notify@digital.fake.gov",
|
|
|
|
|
|
"template": str(sample_template_without_email_permission.id),
|
2017-06-26 14:18:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
auth_header = create_service_authorization_header(
|
|
|
|
|
|
service_id=sample_template_without_email_permission.service_id
|
|
|
|
|
|
)
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
path="/notifications/email",
|
2017-06-26 14:18:43 +01:00
|
|
|
|
data=json.dumps(data),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-06-26 14:18:43 +01:00
|
|
|
|
|
|
|
|
|
|
assert not mocked.called
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
error_json = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert error_json["result"] == "error"
|
|
|
|
|
|
assert error_json["message"]["service"][0] == "Cannot send emails"
|
2017-08-23 14:56:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"notification_type, err_msg",
|
2023-08-29 14:54:30 -07:00
|
|
|
|
[("apple", "apple notification type is not supported")],
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_should_throw_exception_if_notification_type_is_invalid(
|
|
|
|
|
|
client, sample_service, notification_type, err_msg
|
|
|
|
|
|
):
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_service_authorization_header(service_id=sample_service.id)
|
2017-08-23 14:56:03 +01:00
|
|
|
|
response = client.post(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
path=f"/notifications/{notification_type}",
|
2017-08-23 14:56:03 +01:00
|
|
|
|
data={},
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-08-23 14:56:03 +01:00
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
assert json.loads(response.get_data(as_text=True))["message"] == err_msg
|
2017-11-27 12:30:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
"notification_type, recipient",
|
|
|
|
|
|
[
|
|
|
|
|
|
(NotificationType.SMS, "2028675309"),
|
|
|
|
|
|
(
|
|
|
|
|
|
NotificationType.EMAIL,
|
|
|
|
|
|
"test@gov.uk",
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
|
|
|
|
|
def test_post_notification_should_set_reply_to_text(
|
|
|
|
|
|
client, sample_service, mocker, notification_type, recipient
|
|
|
|
|
|
):
|
2024-02-20 14:40:40 -05:00
|
|
|
|
mocker.patch(f"app.celery.provider_tasks.deliver_{notification_type}.apply_async")
|
2019-10-31 15:02:30 +00:00
|
|
|
|
template = create_template(sample_service, template_type=notification_type)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
expected_reply_to = current_app.config["FROM_NUMBER"]
|
2024-01-18 10:26:40 -05:00
|
|
|
|
if notification_type == NotificationType.EMAIL:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
expected_reply_to = "reply_to@gov.uk"
|
|
|
|
|
|
create_reply_to_email(
|
|
|
|
|
|
service=sample_service, email_address=expected_reply_to, is_default=True
|
|
|
|
|
|
)
|
2017-11-27 12:30:50 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"to": recipient, "template": str(template.id)}
|
|
|
|
|
|
response = client.post(
|
2024-02-20 14:40:40 -05:00
|
|
|
|
f"/notifications/{notification_type}",
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
|
|
|
|
|
create_service_authorization_header(service_id=sample_service.id),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2017-11-27 12:30:50 +00:00
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
notifications = Notification.query.all()
|
|
|
|
|
|
assert len(notifications) == 1
|
|
|
|
|
|
assert notifications[0].reply_to_text == expected_reply_to
|
2020-07-29 14:52:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-03-02 20:20:31 -05:00
|
|
|
|
@pytest.mark.skip(reason="Rewrite without letters?")
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize("reference_paceholder,", [None, "ref2"])
|
2021-03-19 16:42:55 +00:00
|
|
|
|
def test_send_notification_should_set_client_reference_from_placeholder(
|
|
|
|
|
|
sample_letter_template, mocker, reference_paceholder
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
deliver_mock = mocker.patch(
|
|
|
|
|
|
"app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async"
|
|
|
|
|
|
)
|
2021-03-19 16:42:55 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template_id": sample_letter_template.id,
|
|
|
|
|
|
"personalisation": {
|
|
|
|
|
|
"address_line_1": "Jane",
|
|
|
|
|
|
"address_line_2": "Moss Lane",
|
|
|
|
|
|
"address_line_3": "SW1A 1AA",
|
2021-03-19 16:42:55 +00:00
|
|
|
|
},
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"to": "Jane",
|
|
|
|
|
|
"created_by": sample_letter_template.service.created_by_id,
|
2021-03-19 16:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
if reference_paceholder:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data["personalisation"]["reference"] = reference_paceholder
|
2021-03-19 16:42:55 +00:00
|
|
|
|
|
|
|
|
|
|
notification_id = send_one_off_notification(sample_letter_template.service_id, data)
|
|
|
|
|
|
assert deliver_mock.called
|
2023-08-29 14:54:30 -07:00
|
|
|
|
notification = Notification.query.get(notification_id["id"])
|
2021-03-19 16:42:55 +00:00
|
|
|
|
assert notification.client_reference == reference_paceholder
|