mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-03 21:20:47 -04:00
@@ -14,15 +14,15 @@ from app.dao.services_dao import dao_update_service
|
||||
from app.dao.templates_dao import dao_get_all_templates_for_service, dao_update_template
|
||||
from app.errors import InvalidRequest
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
SMS_TYPE,
|
||||
ApiKey,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
NotificationType,
|
||||
Template,
|
||||
TemplateType,
|
||||
)
|
||||
from app.service.send_notification import send_one_off_notification
|
||||
from app.v2.errors import RateLimitError
|
||||
@@ -37,7 +37,7 @@ from tests.app.db import (
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template_type", [SMS_TYPE, EMAIL_TYPE])
|
||||
@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL])
|
||||
def test_create_notification_should_reject_if_missing_required_fields(
|
||||
notify_api, sample_api_key, mocker, template_type
|
||||
):
|
||||
@@ -96,7 +96,11 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"template_type, to", [(SMS_TYPE, "+447700900855"), (EMAIL_TYPE, "ok@ok.com")]
|
||||
"template_type, to",
|
||||
[
|
||||
(TemplateType.SMS, "+447700900855"),
|
||||
(TemplateType.EMAIL, "ok@ok.com"),
|
||||
]
|
||||
)
|
||||
def test_send_notification_invalid_template_id(
|
||||
notify_api, sample_template, mocker, fake_uuid, template_type, to
|
||||
@@ -281,8 +285,8 @@ def test_should_not_send_notification_for_archived_template(
|
||||
@pytest.mark.parametrize(
|
||||
"template_type, to",
|
||||
[
|
||||
(SMS_TYPE, "+447700900855"),
|
||||
(EMAIL_TYPE, "not-someone-we-trust@email-address.com"),
|
||||
(TemplateType.SMS, "+447700900855"),
|
||||
(TemplateType.EMAIL, "not-someone-we-trust@email-address.com"),
|
||||
],
|
||||
)
|
||||
def test_should_not_send_notification_if_restricted_and_not_a_service_user(
|
||||
@@ -294,7 +298,9 @@ def test_should_not_send_notification_if_restricted_and_not_a_service_user(
|
||||
"app.celery.provider_tasks.deliver_{}.apply_async".format(template_type)
|
||||
)
|
||||
template = (
|
||||
sample_template if template_type == SMS_TYPE else sample_email_template
|
||||
sample_template
|
||||
if template_type == TemplateType.SMS
|
||||
else sample_email_template
|
||||
)
|
||||
template.service.restricted = True
|
||||
dao_update_service(template.service)
|
||||
@@ -322,7 +328,7 @@ def test_should_not_send_notification_if_restricted_and_not_a_service_user(
|
||||
] == json_resp["message"]["to"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template_type", [SMS_TYPE, EMAIL_TYPE])
|
||||
@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL])
|
||||
def test_should_send_notification_if_restricted_and_a_service_user(
|
||||
notify_api, sample_template, sample_email_template, template_type, mocker
|
||||
):
|
||||
@@ -333,11 +339,11 @@ def test_should_send_notification_if_restricted_and_a_service_user(
|
||||
)
|
||||
|
||||
template = (
|
||||
sample_template if template_type == SMS_TYPE else sample_email_template
|
||||
sample_template if template_type == TemplateType.SMS else sample_email_template
|
||||
)
|
||||
to = (
|
||||
template.service.created_by.mobile_number
|
||||
if template_type == SMS_TYPE
|
||||
if template_type == TemplateType.SMS
|
||||
else template.service.created_by.email_address
|
||||
)
|
||||
template.service.restricted = True
|
||||
@@ -358,7 +364,7 @@ def test_should_send_notification_if_restricted_and_a_service_user(
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template_type", [SMS_TYPE, EMAIL_TYPE])
|
||||
@pytest.mark.parametrize("template_type", [TemplateType.SMS, TemplateType.EMAIL])
|
||||
def test_should_not_allow_template_from_another_service(
|
||||
notify_api, service_factory, sample_user, mocker, template_type
|
||||
):
|
||||
@@ -379,7 +385,7 @@ def test_should_not_allow_template_from_another_service(
|
||||
)
|
||||
to = (
|
||||
sample_user.mobile_number
|
||||
if template_type == SMS_TYPE
|
||||
if template_type == TemplateType.SMS
|
||||
else sample_user.email_address
|
||||
)
|
||||
data = {"to": to, "template": service_2_templates[0].id}
|
||||
@@ -496,8 +502,8 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
|
||||
mocker.patch("app.celery.provider_tasks.deliver_sms.apply_async")
|
||||
|
||||
service = create_service(restricted=restricted, message_limit=2)
|
||||
email_template = create_template(service, template_type=EMAIL_TYPE)
|
||||
sms_template = create_template(service, template_type=SMS_TYPE)
|
||||
email_template = create_template(service, template_type=TemplateType.EMAIL)
|
||||
sms_template = create_template(service, template_type=TemplateType.SMS)
|
||||
create_notification(template=email_template)
|
||||
|
||||
data = {"to": sample_user.mobile_number, "template": str(sms_template.id)}
|
||||
@@ -518,7 +524,7 @@ def test_should_not_return_html_in_body(notify_api, sample_service, mocker):
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch("app.celery.provider_tasks.deliver_email.apply_async")
|
||||
email_template = create_template(
|
||||
sample_service, template_type=EMAIL_TYPE, content="hello\nthere"
|
||||
sample_service, template_type=TemplateType.EMAIL, content="hello\nthere"
|
||||
)
|
||||
|
||||
data = {"to": "ok@ok.com", "template": str(email_template.id)}
|
||||
@@ -742,7 +748,10 @@ def test_should_send_sms_if_team_api_key_and_a_service_user(
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"template_type,queue_name",
|
||||
[(SMS_TYPE, "send-sms-tasks"), (EMAIL_TYPE, "send-email-tasks")],
|
||||
[
|
||||
(TemplateType.SMS, "send-sms-tasks"),
|
||||
(TemplateType.EMAIL, "send-email-tasks"),
|
||||
],
|
||||
)
|
||||
def test_should_persist_notification(
|
||||
client,
|
||||
@@ -760,10 +769,10 @@ def test_should_persist_notification(
|
||||
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
||||
)
|
||||
|
||||
template = sample_template if template_type == SMS_TYPE else sample_email_template
|
||||
template = sample_template if template_type == TemplateType.SMS else sample_email_template
|
||||
to = (
|
||||
sample_template.service.created_by.mobile_number
|
||||
if template_type == SMS_TYPE
|
||||
if template_type == TemplateType.SMS
|
||||
else sample_email_template.service.created_by.email_address
|
||||
)
|
||||
data = {"to": to, "template": template.id}
|
||||
@@ -798,7 +807,7 @@ def test_should_persist_notification(
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"template_type,queue_name",
|
||||
[(SMS_TYPE, "send-sms-tasks"), (EMAIL_TYPE, "send-email-tasks")],
|
||||
[(TemplateType.SMS, "send-sms-tasks"), (TemplateType.EMAIL, "send-email-tasks")],
|
||||
)
|
||||
def test_should_delete_notification_and_return_error_if_redis_fails(
|
||||
client,
|
||||
@@ -817,10 +826,10 @@ def test_should_delete_notification_and_return_error_if_redis_fails(
|
||||
"app.notifications.process_notifications.uuid.uuid4", return_value=fake_uuid
|
||||
)
|
||||
|
||||
template = sample_template if template_type == SMS_TYPE else sample_email_template
|
||||
template = sample_template if template_type == TemplateType.SMS else sample_email_template
|
||||
to = (
|
||||
sample_template.service.created_by.mobile_number
|
||||
if template_type == SMS_TYPE
|
||||
if template_type == TemplateType.SMS
|
||||
else sample_email_template.service.created_by.email_address
|
||||
)
|
||||
data = {"to": to, "template": template.id}
|
||||
@@ -907,7 +916,10 @@ def test_should_not_persist_notification_or_send_sms_if_simulated_number(
|
||||
@pytest.mark.parametrize("key_type", [KEY_TYPE_NORMAL, KEY_TYPE_TEAM])
|
||||
@pytest.mark.parametrize(
|
||||
"notification_type, to",
|
||||
[(SMS_TYPE, "2028675300"), (EMAIL_TYPE, "non_guest_list_recipient@mail.com")],
|
||||
[
|
||||
(TemplateType.SMS, "2028675300"),
|
||||
(TemplateType.EMAIL, "non_guest_list_recipient@mail.com"),
|
||||
],
|
||||
)
|
||||
def test_should_not_send_notification_to_non_guest_list_recipient_in_trial_mode(
|
||||
client, sample_service_guest_list, notification_type, to, key_type, mocker
|
||||
@@ -962,8 +974,8 @@ def test_should_not_send_notification_to_non_guest_list_recipient_in_trial_mode(
|
||||
@pytest.mark.parametrize(
|
||||
"notification_type, to, normalized_to",
|
||||
[
|
||||
(SMS_TYPE, "2028675300", "+12028675300"),
|
||||
(EMAIL_TYPE, "guest_list_recipient@mail.com", None),
|
||||
(NotificationType.SMS, "2028675300", "+12028675300"),
|
||||
(NotificationType.EMAIL, "guest_list_recipient@mail.com", None),
|
||||
],
|
||||
)
|
||||
def test_should_send_notification_to_guest_list_recipient(
|
||||
@@ -983,9 +995,9 @@ def test_should_send_notification_to_guest_list_recipient(
|
||||
"app.celery.provider_tasks.deliver_{}.apply_async".format(notification_type)
|
||||
)
|
||||
template = create_template(sample_service, template_type=notification_type)
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
service_guest_list = create_service_guest_list(sample_service, mobile_number=to)
|
||||
elif notification_type == EMAIL_TYPE:
|
||||
elif notification_type == NotificationType.EMAIL:
|
||||
service_guest_list = create_service_guest_list(sample_service, email_address=to)
|
||||
|
||||
assert service_guest_list.service_id == sample_service.id
|
||||
@@ -1022,8 +1034,8 @@ def test_should_send_notification_to_guest_list_recipient(
|
||||
@pytest.mark.parametrize(
|
||||
"notification_type, template_type, to",
|
||||
[
|
||||
(EMAIL_TYPE, SMS_TYPE, "notify@digital.fake.gov"),
|
||||
(SMS_TYPE, EMAIL_TYPE, "+12028675309"),
|
||||
(NotificationType.EMAIL, TemplateType.SMS, "notify@digital.fake.gov"),
|
||||
(NotificationType.SMS, TemplateType.EMAIL, "+12028675309"),
|
||||
],
|
||||
)
|
||||
def test_should_error_if_notification_type_does_not_match_template_type(
|
||||
@@ -1070,7 +1082,11 @@ def test_create_template_doesnt_raise_with_too_much_personalisation(
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"template_type, should_error", [(SMS_TYPE, True), (EMAIL_TYPE, False)]
|
||||
"template_type, should_error",
|
||||
[
|
||||
(TemplateType.SMS, True),
|
||||
(TemplateType.EMAIL, False),
|
||||
]
|
||||
)
|
||||
def test_create_template_raises_invalid_request_when_content_too_large(
|
||||
sample_service, template_type, should_error
|
||||
@@ -1338,7 +1354,7 @@ def test_post_notification_should_set_reply_to_text(
|
||||
)
|
||||
template = create_template(sample_service, template_type=notification_type)
|
||||
expected_reply_to = current_app.config["FROM_NUMBER"]
|
||||
if notification_type == EMAIL_TYPE:
|
||||
if notification_type == NotificationType.EMAIL:
|
||||
expected_reply_to = "reply_to@gov.uk"
|
||||
create_reply_to_email(
|
||||
service=sample_service, email_address=expected_reply_to, is_default=True
|
||||
|
||||
@@ -8,13 +8,13 @@ from notifications_utils.recipients import InvalidPhoneError
|
||||
from app.config import QueueNames
|
||||
from app.dao.service_guest_list_dao import dao_add_and_commit_guest_list_contacts
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
MOBILE_TYPE,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
GuestListRecipientType,
|
||||
Notification,
|
||||
NotificationType,
|
||||
ServiceGuestList,
|
||||
TemplateType,
|
||||
)
|
||||
from app.service.send_notification import send_one_off_notification
|
||||
from app.v2.errors import BadRequestError
|
||||
@@ -69,7 +69,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms(
|
||||
service = create_service()
|
||||
template = create_template(
|
||||
service=service,
|
||||
template_type=SMS_TYPE,
|
||||
template_type=TemplateType.SMS,
|
||||
content="Hello (( Name))\nYour thing is due soon",
|
||||
)
|
||||
|
||||
@@ -88,7 +88,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms(
|
||||
recipient=post_data["to"],
|
||||
service=template.service,
|
||||
personalisation={"name": "foo"},
|
||||
notification_type=SMS_TYPE,
|
||||
notification_type=NotificationType.SMS,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
created_by_id=str(service.created_by_id),
|
||||
@@ -104,7 +104,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_international_sms
|
||||
service = create_service(service_permissions=["sms", "international_sms"])
|
||||
template = create_template(
|
||||
service=service,
|
||||
template_type=SMS_TYPE,
|
||||
template_type=TemplateType.SMS,
|
||||
)
|
||||
|
||||
post_data = {
|
||||
@@ -125,7 +125,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_email(
|
||||
service = create_service()
|
||||
template = create_template(
|
||||
service=service,
|
||||
template_type=EMAIL_TYPE,
|
||||
template_type=TemplateType.EMAIL,
|
||||
subject="Test subject",
|
||||
content="Hello (( Name))\nYour thing is due soon",
|
||||
)
|
||||
@@ -145,7 +145,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_email(
|
||||
recipient=post_data["to"],
|
||||
service=template.service,
|
||||
personalisation={"name": "foo"},
|
||||
notification_type=EMAIL_TYPE,
|
||||
notification_type=NotificationType.EMAIL,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
created_by_id=str(service.created_by_id),
|
||||
@@ -203,7 +203,7 @@ def test_send_one_off_notification_raises_if_cant_send_to_recipient(
|
||||
template = create_template(service=service)
|
||||
dao_add_and_commit_guest_list_contacts(
|
||||
[
|
||||
ServiceGuestList.from_string(service.id, MOBILE_TYPE, "2028765309"),
|
||||
ServiceGuestList.from_string(service.id, GuestListRecipientType.MOBILE, "2028765309"),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -286,7 +286,7 @@ def test_send_one_off_notification_should_add_email_reply_to_text_for_notificati
|
||||
def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text(
|
||||
sample_service, celery_mock
|
||||
):
|
||||
template = create_template(service=sample_service, template_type=SMS_TYPE)
|
||||
template = create_template(service=sample_service, template_type=TemplateType.SMS)
|
||||
sms_sender = create_service_sms_sender(
|
||||
service=sample_service, sms_sender="2028675309", is_default=False
|
||||
)
|
||||
@@ -310,7 +310,7 @@ def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text(
|
||||
def test_send_one_off_sms_notification_should_use_default_service_reply_to_text(
|
||||
sample_service, celery_mock
|
||||
):
|
||||
template = create_template(service=sample_service, template_type=SMS_TYPE)
|
||||
template = create_template(service=sample_service, template_type=TemplateType.SMS)
|
||||
sample_service.service_sms_senders[0].is_default = False
|
||||
create_service_sms_sender(
|
||||
service=sample_service, sms_sender="2028675309", is_default=True
|
||||
|
||||
@@ -15,23 +15,21 @@ from app.dao.services_dao import dao_add_user_to_service, dao_remove_user_from_s
|
||||
from app.dao.templates_dao import dao_redact_template
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.models import (
|
||||
EMAIL_AUTH_TYPE,
|
||||
EMAIL_TYPE,
|
||||
INBOUND_SMS_TYPE,
|
||||
INTERNATIONAL_SMS_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
SMS_TYPE,
|
||||
AnnualBilling,
|
||||
EmailBranding,
|
||||
InboundNumber,
|
||||
Notification,
|
||||
NotificationType,
|
||||
Permission,
|
||||
Service,
|
||||
ServiceEmailReplyTo,
|
||||
ServicePermission,
|
||||
ServicePermissionType,
|
||||
ServiceSmsSender,
|
||||
TemplateType,
|
||||
User,
|
||||
)
|
||||
from tests import create_admin_authorization_header
|
||||
@@ -285,9 +283,9 @@ def test_get_service_list_has_default_permissions(admin_request, service_factory
|
||||
assert all(
|
||||
set(json["permissions"])
|
||||
== {
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
INTERNATIONAL_SMS_TYPE,
|
||||
ServicePermissionType.EMAIL,
|
||||
ServicePermissionType.SMS,
|
||||
ServicePermissionType.INTERNATIONAL_SMS,
|
||||
}
|
||||
for json in json_resp["data"]
|
||||
)
|
||||
@@ -301,9 +299,9 @@ def test_get_service_by_id_has_default_service_permissions(
|
||||
)
|
||||
|
||||
assert set(json_resp["data"]["permissions"]) == {
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
INTERNATIONAL_SMS_TYPE,
|
||||
ServicePermissionType.EMAIL,
|
||||
ServicePermissionType.SMS,
|
||||
ServicePermissionType.INTERNATIONAL_SMS,
|
||||
}
|
||||
|
||||
|
||||
@@ -769,7 +767,7 @@ def test_update_service_flags(client, sample_service):
|
||||
json_resp = resp.json
|
||||
assert resp.status_code == 200
|
||||
assert json_resp["data"]["name"] == sample_service.name
|
||||
data = {"permissions": [INTERNATIONAL_SMS_TYPE]}
|
||||
data = {"permissions": {ServicePermissionType.INTERNATIONAL_SMS}}
|
||||
|
||||
auth_header = create_admin_authorization_header()
|
||||
|
||||
@@ -780,7 +778,9 @@ def test_update_service_flags(client, sample_service):
|
||||
)
|
||||
result = resp.json
|
||||
assert resp.status_code == 200
|
||||
assert set(result["data"]["permissions"]) == set([INTERNATIONAL_SMS_TYPE])
|
||||
assert set(result["data"]["permissions"]) == {
|
||||
ServicePermissionType.INTERNATIONAL_SMS
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -854,7 +854,7 @@ def test_update_service_flags_with_service_without_default_service_permissions(
|
||||
):
|
||||
auth_header = create_admin_authorization_header()
|
||||
data = {
|
||||
"permissions": [INTERNATIONAL_SMS_TYPE],
|
||||
"permissions": {ServicePermissionType.INTERNATIONAL_SMS},
|
||||
}
|
||||
|
||||
resp = client.post(
|
||||
@@ -865,7 +865,9 @@ def test_update_service_flags_with_service_without_default_service_permissions(
|
||||
result = resp.json
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert set(result["data"]["permissions"]) == set([INTERNATIONAL_SMS_TYPE])
|
||||
assert set(result["data"]["permissions"]) == {
|
||||
ServicePermissionType.INTERNATIONAL_SMS,
|
||||
}
|
||||
|
||||
|
||||
def test_update_service_flags_will_remove_service_permissions(
|
||||
@@ -874,12 +876,18 @@ def test_update_service_flags_will_remove_service_permissions(
|
||||
auth_header = create_admin_authorization_header()
|
||||
|
||||
service = create_service(
|
||||
service_permissions=[SMS_TYPE, EMAIL_TYPE, INTERNATIONAL_SMS_TYPE]
|
||||
service_permissions={
|
||||
ServicePermissionType.SMS,
|
||||
ServicePermissionType.EMAIL,
|
||||
ServicePermissionType.INTERNATIONAL_SMS,
|
||||
}
|
||||
)
|
||||
|
||||
assert INTERNATIONAL_SMS_TYPE in [p.permission for p in service.permissions]
|
||||
assert ServicePermissionType.INTERNATIONAL_SMS in {
|
||||
p.permission for p in service.permissions
|
||||
}
|
||||
|
||||
data = {"permissions": [SMS_TYPE, EMAIL_TYPE]}
|
||||
data = {"permissions": {ServicePermissionType.SMS, ServicePermissionType.EMAIL}}
|
||||
|
||||
resp = client.post(
|
||||
"/service/{}".format(service.id),
|
||||
@@ -889,10 +897,13 @@ def test_update_service_flags_will_remove_service_permissions(
|
||||
result = resp.json
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert INTERNATIONAL_SMS_TYPE not in result["data"]["permissions"]
|
||||
assert ServicePermissionType.INTERNATIONAL_SMS not in result["data"]["permissions"]
|
||||
|
||||
permissions = ServicePermission.query.filter_by(service_id=service.id).all()
|
||||
assert set([p.permission for p in permissions]) == set([SMS_TYPE, EMAIL_TYPE])
|
||||
assert {p.permission for p in permissions} == {
|
||||
ServicePermissionType.SMS,
|
||||
ServicePermissionType.EMAIL,
|
||||
}
|
||||
|
||||
|
||||
def test_update_permissions_will_override_permission_flags(
|
||||
@@ -900,7 +911,7 @@ def test_update_permissions_will_override_permission_flags(
|
||||
):
|
||||
auth_header = create_admin_authorization_header()
|
||||
|
||||
data = {"permissions": [INTERNATIONAL_SMS_TYPE]}
|
||||
data = {"permissions": {ServicePermissionType.INTERNATIONAL_SMS}}
|
||||
|
||||
resp = client.post(
|
||||
"/service/{}".format(service_with_no_permissions.id),
|
||||
@@ -910,7 +921,9 @@ def test_update_permissions_will_override_permission_flags(
|
||||
result = resp.json
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert set(result["data"]["permissions"]) == set([INTERNATIONAL_SMS_TYPE])
|
||||
assert set(result["data"]["permissions"]) == {
|
||||
ServicePermissionType.INTERNATIONAL_SMS
|
||||
}
|
||||
|
||||
|
||||
def test_update_service_permissions_will_add_service_permissions(
|
||||
@@ -918,7 +931,7 @@ def test_update_service_permissions_will_add_service_permissions(
|
||||
):
|
||||
auth_header = create_admin_authorization_header()
|
||||
|
||||
data = {"permissions": [EMAIL_TYPE, SMS_TYPE]}
|
||||
data = {"permissions": {ServicePermissionType.EMAIL, ServicePermissionType.SMS}}
|
||||
|
||||
resp = client.post(
|
||||
"/service/{}".format(sample_service.id),
|
||||
@@ -928,17 +941,20 @@ def test_update_service_permissions_will_add_service_permissions(
|
||||
result = resp.json
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert set(result["data"]["permissions"]) == set([SMS_TYPE, EMAIL_TYPE])
|
||||
assert set(result["data"]["permissions"]) == {
|
||||
ServicePermissionType.SMS,
|
||||
ServicePermissionType.EMAIL,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"permission_to_add",
|
||||
[
|
||||
(EMAIL_TYPE),
|
||||
(SMS_TYPE),
|
||||
(INTERNATIONAL_SMS_TYPE),
|
||||
(INBOUND_SMS_TYPE),
|
||||
(EMAIL_AUTH_TYPE),
|
||||
ServicePermissionType.EMAIL,
|
||||
ServicePermissionType.SMS,
|
||||
ServicePermissionType.INTERNATIONAL_SMS,
|
||||
ServicePermissionType.INBOUND_SMS,
|
||||
ServicePermissionType.EMAIL_AUTH,
|
||||
],
|
||||
)
|
||||
def test_add_service_permission_will_add_permission(
|
||||
@@ -968,7 +984,13 @@ def test_update_permissions_with_an_invalid_permission_will_raise_error(
|
||||
auth_header = create_admin_authorization_header()
|
||||
invalid_permission = "invalid_permission"
|
||||
|
||||
data = {"permissions": [EMAIL_TYPE, SMS_TYPE, invalid_permission]}
|
||||
data = {
|
||||
"permissions": {
|
||||
ServicePermissionType.EMAIL,
|
||||
ServicePermissionType.SMS,
|
||||
invalid_permission,
|
||||
}
|
||||
}
|
||||
|
||||
resp = client.post(
|
||||
"/service/{}".format(sample_service.id),
|
||||
@@ -990,7 +1012,13 @@ def test_update_permissions_with_duplicate_permissions_will_raise_error(
|
||||
):
|
||||
auth_header = create_admin_authorization_header()
|
||||
|
||||
data = {"permissions": [EMAIL_TYPE, SMS_TYPE, SMS_TYPE]}
|
||||
data = {
|
||||
"permissions": {
|
||||
ServicePermissionType.EMAIL,
|
||||
ServicePermissionType.SMS,
|
||||
ServicePermissionType.SMS,
|
||||
}
|
||||
}
|
||||
|
||||
resp = client.post(
|
||||
"/service/{}".format(sample_service.id),
|
||||
@@ -1002,7 +1030,7 @@ def test_update_permissions_with_duplicate_permissions_will_raise_error(
|
||||
assert resp.status_code == 400
|
||||
assert result["result"] == "error"
|
||||
assert (
|
||||
"Duplicate Service Permission: ['{}']".format(SMS_TYPE)
|
||||
f"Duplicate Service Permission: ['{ServicePermissionType.SMS}']"
|
||||
in result["message"]["permissions"]
|
||||
)
|
||||
|
||||
@@ -1695,7 +1723,7 @@ def test_get_all_notifications_for_service_filters_notifications_when_using_post
|
||||
service_2 = create_service(service_name="2")
|
||||
|
||||
service_1_sms_template = create_template(service_1)
|
||||
service_1_email_template = create_template(service_1, template_type=EMAIL_TYPE)
|
||||
service_1_email_template = create_template(service_1, template_type=TemplateType.EMAIL)
|
||||
service_2_sms_template = create_template(service_2)
|
||||
|
||||
returned_notification = create_notification(
|
||||
@@ -2040,8 +2068,11 @@ def test_get_detailed_service(
|
||||
service = resp.json["data"]
|
||||
assert service["id"] == str(sample_service.id)
|
||||
assert "statistics" in service.keys()
|
||||
assert set(service["statistics"].keys()) == {SMS_TYPE, EMAIL_TYPE}
|
||||
assert service["statistics"][SMS_TYPE] == stats
|
||||
assert set(service["statistics"].keys()) == {
|
||||
NotificationType.SMS.value,
|
||||
NotificationType.EMAIL.value,
|
||||
}
|
||||
assert service["statistics"][NotificationType.SMS.value] == stats
|
||||
|
||||
|
||||
def test_get_services_with_detailed_flag(client, sample_template):
|
||||
@@ -2060,8 +2091,8 @@ def test_get_services_with_detailed_flag(client, sample_template):
|
||||
assert data[0]["name"] == "Sample service"
|
||||
assert data[0]["id"] == str(notifications[0].service_id)
|
||||
assert data[0]["statistics"] == {
|
||||
EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 3},
|
||||
NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 3},
|
||||
}
|
||||
|
||||
|
||||
@@ -2083,8 +2114,8 @@ def test_get_services_with_detailed_flag_excluding_from_test_key(
|
||||
data = resp.json["data"]
|
||||
assert len(data) == 1
|
||||
assert data[0]["statistics"] == {
|
||||
EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 2},
|
||||
NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 2},
|
||||
}
|
||||
|
||||
|
||||
@@ -2153,13 +2184,13 @@ def test_get_detailed_services_groups_by_service(notify_db_session):
|
||||
assert len(data) == 2
|
||||
assert data[0]["id"] == str(service_1.id)
|
||||
assert data[0]["statistics"] == {
|
||||
EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
SMS_TYPE: {"delivered": 1, "failed": 0, "requested": 3},
|
||||
NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.SMS.value: {"delivered": 1, "failed": 0, "requested": 3},
|
||||
}
|
||||
assert data[1]["id"] == str(service_2.id)
|
||||
assert data[1]["statistics"] == {
|
||||
EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 1},
|
||||
NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 1},
|
||||
}
|
||||
|
||||
|
||||
@@ -2182,13 +2213,13 @@ def test_get_detailed_services_includes_services_with_no_notifications(
|
||||
assert len(data) == 2
|
||||
assert data[0]["id"] == str(service_1.id)
|
||||
assert data[0]["statistics"] == {
|
||||
EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 1},
|
||||
NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 1},
|
||||
}
|
||||
assert data[1]["id"] == str(service_2.id)
|
||||
assert data[1]["statistics"] == {
|
||||
EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
}
|
||||
|
||||
|
||||
@@ -2208,8 +2239,8 @@ def test_get_detailed_services_only_includes_todays_notifications(sample_templat
|
||||
|
||||
assert len(data) == 1
|
||||
assert data[0]["statistics"] == {
|
||||
EMAIL_TYPE: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
SMS_TYPE: {"delivered": 0, "failed": 0, "requested": 3},
|
||||
NotificationType.EMAIL.value: {"delivered": 0, "failed": 0, "requested": 0},
|
||||
NotificationType.SMS.value: {"delivered": 0, "failed": 0, "requested": 3},
|
||||
}
|
||||
|
||||
|
||||
@@ -2251,12 +2282,12 @@ def test_get_detailed_services_for_date_range(
|
||||
)
|
||||
|
||||
assert len(data) == 1
|
||||
assert data[0]["statistics"][EMAIL_TYPE] == {
|
||||
assert data[0]["statistics"][NotificationType.EMAIL.value] == {
|
||||
"delivered": 0,
|
||||
"failed": 0,
|
||||
"requested": 0,
|
||||
}
|
||||
assert data[0]["statistics"][SMS_TYPE] == {
|
||||
assert data[0]["statistics"][NotificationType.SMS.value] == {
|
||||
"delivered": 2,
|
||||
"failed": 0,
|
||||
"requested": 2,
|
||||
@@ -2622,7 +2653,7 @@ def test_get_all_notifications_for_service_includes_template_redacted(
|
||||
|
||||
# TODO: check whether all hidden templates are also precompiled letters
|
||||
# def test_get_all_notifications_for_service_includes_template_hidden(admin_request, sample_service):
|
||||
# letter_template = create_template(sample_service, template_type=LETTER_TYPE)
|
||||
# letter_template = create_template(sample_service, template_type=TemplateType.LETTER)
|
||||
|
||||
# with freeze_time('2000-01-01'):
|
||||
# letter_noti = create_notification(letter_template)
|
||||
|
||||
@@ -2,12 +2,12 @@ import pytest
|
||||
from flask import current_app
|
||||
|
||||
from app.dao.services_dao import dao_add_user_to_service
|
||||
from app.models import EMAIL_TYPE, SMS_TYPE, Notification
|
||||
from app.models import Notification, NotificationType, TemplateType
|
||||
from app.service.sender import send_notification_to_service_users
|
||||
from tests.app.db import create_service, create_template, create_user
|
||||
|
||||
|
||||
@pytest.mark.parametrize("notification_type", [EMAIL_TYPE, SMS_TYPE])
|
||||
@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS])
|
||||
def test_send_notification_to_service_users_persists_notifications_correctly(
|
||||
notify_service, notification_type, sample_service, mocker
|
||||
):
|
||||
@@ -37,7 +37,7 @@ def test_send_notification_to_service_users_sends_to_queue(
|
||||
):
|
||||
send_mock = mocker.patch("app.service.sender.send_notification_to_queue")
|
||||
|
||||
template = create_template(sample_service, template_type=EMAIL_TYPE)
|
||||
template = create_template(sample_service, template_type=NotificationType.EMAIL)
|
||||
send_notification_to_service_users(
|
||||
service_id=sample_service.id, template_id=template.id
|
||||
)
|
||||
@@ -54,7 +54,7 @@ def test_send_notification_to_service_users_includes_user_fields_in_personalisat
|
||||
|
||||
user = sample_service.users[0]
|
||||
|
||||
template = create_template(sample_service, template_type=EMAIL_TYPE)
|
||||
template = create_template(sample_service, template_type=TemplateType.EMAIL)
|
||||
send_notification_to_service_users(
|
||||
service_id=sample_service.id,
|
||||
template_id=template.id,
|
||||
@@ -82,7 +82,7 @@ def test_send_notification_to_service_users_sends_to_active_users_only(
|
||||
service = create_service(user=first_active_user)
|
||||
dao_add_user_to_service(service, second_active_user)
|
||||
dao_add_user_to_service(service, pending_user)
|
||||
template = create_template(service, template_type=EMAIL_TYPE)
|
||||
template = create_template(service, template_type=TemplateType.EMAIL)
|
||||
|
||||
send_notification_to_service_users(service_id=service.id, template_id=template.id)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import json
|
||||
import uuid
|
||||
|
||||
from app.dao.service_guest_list_dao import dao_add_and_commit_guest_list_contacts
|
||||
from app.models import EMAIL_TYPE, MOBILE_TYPE, ServiceGuestList
|
||||
from app.models import GuestListRecipientType, ServiceGuestList
|
||||
from tests import create_admin_authorization_header
|
||||
|
||||
|
||||
@@ -24,11 +24,15 @@ def test_get_guest_list_separates_emails_and_phones(client, sample_service):
|
||||
dao_add_and_commit_guest_list_contacts(
|
||||
[
|
||||
ServiceGuestList.from_string(
|
||||
sample_service.id, EMAIL_TYPE, "service@example.com"
|
||||
sample_service.id,
|
||||
GuestListRecipientType.EMAIL,
|
||||
"service@example.com",
|
||||
),
|
||||
ServiceGuestList.from_string(sample_service.id, MOBILE_TYPE, "2028675309"),
|
||||
ServiceGuestList.from_string(sample_service.id, GuestListRecipientType.MOBILE, "2028675309"),
|
||||
ServiceGuestList.from_string(
|
||||
sample_service.id, MOBILE_TYPE, "+1800-555-5555"
|
||||
sample_service.id,
|
||||
GuestListRecipientType.MOBILE,
|
||||
"+1800-555-5555",
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -5,11 +5,11 @@ import pytest
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
SMS_TYPE,
|
||||
NotificationType,
|
||||
TemplateType,
|
||||
)
|
||||
from tests.app.db import (
|
||||
create_ft_notification_status,
|
||||
@@ -58,7 +58,7 @@ def test_get_template_usage_by_month_returns_two_templates(
|
||||
):
|
||||
template_one = create_template(
|
||||
sample_service,
|
||||
template_type=SMS_TYPE,
|
||||
template_type=TemplateType.SMS,
|
||||
template_name="TEST TEMPLATE",
|
||||
hidden=True,
|
||||
)
|
||||
@@ -123,8 +123,11 @@ def test_get_service_notification_statistics(
|
||||
today_only=today_only,
|
||||
)
|
||||
|
||||
assert set(resp["data"].keys()) == {SMS_TYPE, EMAIL_TYPE}
|
||||
assert resp["data"][SMS_TYPE] == stats
|
||||
assert set(resp["data"].keys()) == {
|
||||
NotificationType.SMS.value,
|
||||
NotificationType.EMAIL.value,
|
||||
}
|
||||
assert resp["data"][NotificationType.SMS.value] == stats
|
||||
|
||||
|
||||
def test_get_service_notification_statistics_with_unknown_service(admin_request):
|
||||
@@ -133,8 +136,8 @@ def test_get_service_notification_statistics_with_unknown_service(admin_request)
|
||||
)
|
||||
|
||||
assert resp["data"] == {
|
||||
SMS_TYPE: {"requested": 0, "delivered": 0, "failed": 0},
|
||||
EMAIL_TYPE: {"requested": 0, "delivered": 0, "failed": 0},
|
||||
NotificationType.SMS.value: {"requested": 0, "delivered": 0, "failed": 0},
|
||||
NotificationType.EMAIL.value: {"requested": 0, "delivered": 0, "failed": 0},
|
||||
}
|
||||
|
||||
|
||||
@@ -198,7 +201,7 @@ def test_get_monthly_notification_stats_returns_empty_stats_with_correct_dates(
|
||||
def test_get_monthly_notification_stats_returns_stats(admin_request, sample_service):
|
||||
sms_t1 = create_template(sample_service)
|
||||
sms_t2 = create_template(sample_service)
|
||||
email_template = create_template(sample_service, template_type=EMAIL_TYPE)
|
||||
email_template = create_template(sample_service, template_type=TemplateType.EMAIL)
|
||||
|
||||
create_ft_notification_status(datetime(2016, 6, 1), template=sms_t1)
|
||||
create_ft_notification_status(datetime(2016, 6, 2), template=sms_t1)
|
||||
|
||||
Reference in New Issue
Block a user