2016-01-13 11:04:13 +00:00
|
|
|
|
import json
|
2016-04-29 10:36:59 +01:00
|
|
|
|
import random
|
|
|
|
|
|
import string
|
2018-11-07 12:48:56 +00:00
|
|
|
|
import uuid
|
2017-06-28 16:49:43 +01:00
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
2016-06-16 13:51:20 +01:00
|
|
|
|
import pytest
|
2017-06-28 16:49:43 +01:00
|
|
|
|
from freezegun import freeze_time
|
2018-08-16 16:25:58 +01:00
|
|
|
|
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
|
|
|
|
|
|
2023-03-02 20:20:31 -05:00
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template
|
2024-01-18 10:26:40 -05:00
|
|
|
|
from app.models import ServicePermissionType, Template, TemplateHistory, TemplateType
|
2021-08-04 15:12:09 +01:00
|
|
|
|
from tests import create_admin_authorization_header
|
2023-03-02 20:20:31 -05:00
|
|
|
|
from tests.app.db import create_service, create_template, create_template_folder
|
2016-01-15 16:22:03 +00:00
|
|
|
|
|
2016-01-13 11:04:13 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"template_type, subject",
|
|
|
|
|
|
[
|
2024-01-18 10:26:40 -05:00
|
|
|
|
(TemplateType.SMS, None),
|
|
|
|
|
|
(TemplateType.EMAIL, "subject"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
|
|
|
|
|
)
|
2016-11-07 15:34:54 +00:00
|
|
|
|
def test_should_create_a_new_template_for_a_service(
|
2017-06-27 18:56:35 +01:00
|
|
|
|
client, sample_user, template_type, subject
|
2016-11-07 15:34:54 +00:00
|
|
|
|
):
|
2019-02-05 12:48:40 +00:00
|
|
|
|
service = create_service(service_permissions=[template_type])
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
|
|
|
|
|
"template_type": template_type,
|
|
|
|
|
|
"content": "template <b>content</b>",
|
|
|
|
|
|
"service": str(service.id),
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
if subject:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data.update({"subject": subject})
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["data"]["name"] == "my template"
|
|
|
|
|
|
assert json_resp["data"]["template_type"] == template_type
|
|
|
|
|
|
assert json_resp["data"]["content"] == "template <b>content</b>"
|
|
|
|
|
|
assert json_resp["data"]["service"] == str(service.id)
|
|
|
|
|
|
assert json_resp["data"]["id"]
|
|
|
|
|
|
assert json_resp["data"]["version"] == 1
|
|
|
|
|
|
assert json_resp["data"]["process_type"] == "normal"
|
|
|
|
|
|
assert json_resp["data"]["created_by"] == str(sample_user.id)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
if subject:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["data"]["subject"] == "subject"
|
2016-11-07 15:37:39 +00:00
|
|
|
|
else:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert not json_resp["data"]["subject"]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template = Template.query.get(json_resp["data"]["id"])
|
2017-01-17 15:48:51 +00:00
|
|
|
|
from app.schemas import template_schema
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
|
|
assert sorted(json_resp["data"]) == sorted(template_schema.dump(template))
|
2017-01-17 15:48:51 +00:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
2019-01-30 16:26:49 +00:00
|
|
|
|
def test_create_a_new_template_for_a_service_adds_folder_relationship(
|
2018-11-05 10:54:42 +00:00
|
|
|
|
client, sample_service
|
|
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
parent_folder = create_template_folder(service=sample_service, name="parent folder")
|
2018-11-05 10:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
|
|
|
|
|
"template_type": "sms",
|
|
|
|
|
|
"content": "template <b>content</b>",
|
|
|
|
|
|
"service": str(sample_service.id),
|
|
|
|
|
|
"created_by": str(sample_service.users[0].id),
|
|
|
|
|
|
"parent_folder_id": str(parent_folder.id),
|
2018-11-05 10:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2018-11-05 10:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2018-11-05 10:54:42 +00:00
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 201
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template = Template.query.filter(Template.name == "my template").first()
|
2018-11-05 10:54:42 +00:00
|
|
|
|
assert template.folder == parent_folder
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-07 17:07:04 +00:00
|
|
|
|
def test_create_template_should_return_400_if_folder_is_for_a_different_service(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_service
|
2018-11-05 10:54:42 +00:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service2 = create_service(service_name="second service")
|
2018-11-05 10:54:42 +00:00
|
|
|
|
parent_folder = create_template_folder(service=service2)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
|
|
|
|
|
"template_type": "sms",
|
|
|
|
|
|
"content": "template <b>content</b>",
|
|
|
|
|
|
"service": str(sample_service.id),
|
|
|
|
|
|
"created_by": str(sample_service.users[0].id),
|
|
|
|
|
|
"parent_folder_id": str(parent_folder.id),
|
2018-11-05 10:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2018-11-05 10:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2018-11-05 10:54:42 +00:00
|
|
|
|
)
|
2018-11-07 17:07:04 +00:00
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
json.loads(response.get_data(as_text=True))["message"]
|
|
|
|
|
|
== "parent_folder_id not found"
|
|
|
|
|
|
)
|
2018-11-05 10:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-07 17:07:04 +00:00
|
|
|
|
def test_create_template_should_return_400_if_folder_does_not_exist(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_service
|
2018-11-07 12:48:56 +00:00
|
|
|
|
):
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
|
|
|
|
|
"template_type": "sms",
|
|
|
|
|
|
"content": "template <b>content</b>",
|
|
|
|
|
|
"service": str(sample_service.id),
|
|
|
|
|
|
"created_by": str(sample_service.users[0].id),
|
|
|
|
|
|
"parent_folder_id": str(uuid.uuid4()),
|
2018-11-07 12:48:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2018-11-07 12:48:56 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2018-11-07 12:48:56 +00:00
|
|
|
|
)
|
2018-11-07 17:07:04 +00:00
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
json.loads(response.get_data(as_text=True))["message"]
|
|
|
|
|
|
== "parent_folder_id not found"
|
|
|
|
|
|
)
|
2018-11-07 12:48:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_raise_error_if_service_does_not_exist_on_create(
|
|
|
|
|
|
client, sample_user, fake_uuid
|
|
|
|
|
|
):
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type": TemplateType.SMS,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"content": "template content",
|
|
|
|
|
|
"service": fake_uuid,
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(fake_uuid),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 404
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == "No result found"
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"permissions, template_type, subject, expected_error",
|
|
|
|
|
|
[
|
|
|
|
|
|
(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
[ServicePermissionType.EMAIL],
|
|
|
|
|
|
TemplateType.SMS,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
None,
|
|
|
|
|
|
{"template_type": ["Creating text message templates is not allowed"]},
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
[ServicePermissionType.SMS],
|
|
|
|
|
|
TemplateType.EMAIL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"subject",
|
|
|
|
|
|
{"template_type": ["Creating email templates is not allowed"]},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2017-06-27 18:56:35 +01:00
|
|
|
|
def test_should_raise_error_on_create_if_no_permission(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_user, permissions, template_type, subject, expected_error
|
|
|
|
|
|
):
|
2017-06-27 18:56:35 +01:00
|
|
|
|
service = create_service(service_permissions=permissions)
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
|
|
|
|
|
"template_type": template_type,
|
|
|
|
|
|
"content": "template content",
|
|
|
|
|
|
"service": str(service.id),
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
2017-06-27 18:56:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
if subject:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data.update({"subject": subject})
|
2017-06-27 18:56:35 +01:00
|
|
|
|
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2017-06-27 18:56:35 +01:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2017-06-27 18:56:35 +01:00
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2017-06-28 11:54:23 +01:00
|
|
|
|
assert response.status_code == 403
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == expected_error
|
2017-06-27 18:56:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"template_type, permissions, expected_error",
|
|
|
|
|
|
[
|
|
|
|
|
|
(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
TemplateType.SMS,
|
|
|
|
|
|
[ServicePermissionType.EMAIL],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
{"template_type": ["Updating text message templates is not allowed"]},
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
TemplateType.EMAIL,
|
|
|
|
|
|
[ServicePErmissionType.SMS],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
{"template_type": ["Updating email templates is not allowed"]},
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2017-06-27 18:56:35 +01:00
|
|
|
|
def test_should_be_error_on_update_if_no_permission(
|
2019-10-28 16:31:47 +00:00
|
|
|
|
client,
|
|
|
|
|
|
sample_user,
|
|
|
|
|
|
notify_db_session,
|
|
|
|
|
|
template_type,
|
|
|
|
|
|
permissions,
|
|
|
|
|
|
expected_error,
|
|
|
|
|
|
):
|
|
|
|
|
|
service = create_service(service_permissions=permissions)
|
|
|
|
|
|
template_without_permission = create_template(service, template_type=template_type)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"content": "new template content", "created_by": str(sample_user.id)}
|
2017-06-27 18:56:35 +01:00
|
|
|
|
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2017-06-27 18:56:35 +01:00
|
|
|
|
|
|
|
|
|
|
update_response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template/{}".format(
|
|
|
|
|
|
template_without_permission.service_id, template_without_permission.id
|
|
|
|
|
|
),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2017-06-27 18:56:35 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(update_response.get_data(as_text=True))
|
2017-06-28 11:54:23 +01:00
|
|
|
|
assert update_response.status_code == 403
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == expected_error
|
2017-06-27 18:56:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
def test_should_error_if_created_by_missing(client, sample_user, sample_service):
|
|
|
|
|
|
service_id = str(sample_service.id)
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type": TemplateType.SMS,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"content": "template content",
|
|
|
|
|
|
"service": service_id,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(service_id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2016-11-07 15:37:39 +00: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["errors"][0]["error"] == "ValidationError"
|
|
|
|
|
|
assert json_resp["errors"][0]["message"] == "created_by is a required property"
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_error_if_service_does_not_exist_on_update(client, fake_uuid):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"name": "my template"}
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template/{}".format(fake_uuid, fake_uuid),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 404
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == "No result found"
|
2016-02-22 12:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-01-18 10:26:40 -05:00
|
|
|
|
@pytest.mark.parametrize("template_type", [TemplateType.EMAIL])
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_must_have_a_subject_on_an_email_template(
|
|
|
|
|
|
client, sample_user, sample_service, template_type
|
|
|
|
|
|
):
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template",
|
|
|
|
|
|
"template_type": template_type,
|
|
|
|
|
|
"content": "template content",
|
|
|
|
|
|
"service": str(sample_service.id),
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["errors"][0]["error"] == "ValidationError"
|
|
|
|
|
|
assert json_resp["errors"][0]["message"] == "subject is a required property"
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-12-24 12:24:17 +00:00
|
|
|
|
def test_update_should_update_a_template(client, sample_user):
|
2023-03-02 20:20:31 -05:00
|
|
|
|
service = create_service()
|
|
|
|
|
|
template = create_template(service, template_type="sms")
|
2020-06-29 11:16:29 +01:00
|
|
|
|
|
|
|
|
|
|
assert template.created_by == service.created_by
|
|
|
|
|
|
assert template.created_by != sample_user
|
|
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"content": "my template has new content, swell!",
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
update_response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template/{}".format(service.id, template.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert update_response.status_code == 200
|
|
|
|
|
|
update_json_resp = json.loads(update_response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert update_json_resp["data"]["content"] == (
|
|
|
|
|
|
"my template has new content, swell!"
|
2017-01-19 12:05:28 +00:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert update_json_resp["data"]["name"] == template.name
|
|
|
|
|
|
assert update_json_resp["data"]["template_type"] == template.template_type
|
|
|
|
|
|
assert update_json_resp["data"]["version"] == 2
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert update_json_resp["data"]["created_by"] == str(sample_user.id)
|
|
|
|
|
|
template_created_by_users = [
|
|
|
|
|
|
template.created_by_id for template in TemplateHistory.query.all()
|
|
|
|
|
|
]
|
2021-02-16 13:50:07 +00:00
|
|
|
|
assert len(template_created_by_users) == 2
|
|
|
|
|
|
assert service.created_by.id in template_created_by_users
|
|
|
|
|
|
assert sample_user.id in template_created_by_users
|
2020-06-29 11:16:29 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_archive_template(client, sample_template):
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": sample_template.name,
|
|
|
|
|
|
"template_type": sample_template.template_type,
|
|
|
|
|
|
"content": sample_template.content,
|
|
|
|
|
|
"archived": True,
|
|
|
|
|
|
"service": str(sample_template.service.id),
|
|
|
|
|
|
"created_by": str(sample_template.created_by.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template/{}".format(
|
|
|
|
|
|
sample_template.service.id, sample_template.id
|
|
|
|
|
|
),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=json_data,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
assert Template.query.first().archived
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-27 12:30:50 +01:00
|
|
|
|
def test_should_be_able_to_archive_template_should_remove_template_folders(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_service
|
2021-04-27 12:30:50 +01:00
|
|
|
|
):
|
|
|
|
|
|
template_folder = create_template_folder(service=sample_service)
|
|
|
|
|
|
template = create_template(service=sample_service, folder=template_folder)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"archived": True,
|
2021-04-27 12:30:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
f"/service/{sample_service.id}/template/{template.id}",
|
|
|
|
|
|
headers=[
|
|
|
|
|
|
("Content-Type", "application/json"),
|
|
|
|
|
|
create_admin_authorization_header(),
|
|
|
|
|
|
],
|
|
|
|
|
|
data=json.dumps(data),
|
2021-04-27 12:30:50 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
updated_template = Template.query.get(template.id)
|
|
|
|
|
|
assert updated_template.archived
|
|
|
|
|
|
assert not updated_template.folder
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_be_able_to_get_all_templates_for_a_service(
|
|
|
|
|
|
client, sample_user, sample_service
|
|
|
|
|
|
):
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template 1",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type": TemplateType.EMAIL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"subject": "subject 1",
|
|
|
|
|
|
"content": "template content",
|
|
|
|
|
|
"service": str(sample_service.id),
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
data_1 = json.dumps(data)
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "my template 2",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
"template_type": TemplateType.EMAIL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"subject": "subject 2",
|
|
|
|
|
|
"content": "template content",
|
|
|
|
|
|
"service": str(sample_service.id),
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
data_2 = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data_1,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data_2,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id), headers=[auth_header]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
update_json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert update_json_resp["data"][0]["name"] == "my template 1"
|
|
|
|
|
|
assert update_json_resp["data"][0]["version"] == 1
|
|
|
|
|
|
assert update_json_resp["data"][0]["created_at"]
|
|
|
|
|
|
assert update_json_resp["data"][1]["name"] == "my template 2"
|
|
|
|
|
|
assert update_json_resp["data"][1]["version"] == 1
|
|
|
|
|
|
assert update_json_resp["data"][1]["created_at"]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-11-28 17:23:50 +00:00
|
|
|
|
def test_should_get_only_templates_for_that_service(admin_request, notify_db_session):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service_1 = create_service(service_name="service_1")
|
|
|
|
|
|
service_2 = create_service(service_name="service_2")
|
2017-11-28 17:00:01 +00:00
|
|
|
|
id_1 = create_template(service_1).id
|
|
|
|
|
|
id_2 = create_template(service_1).id
|
|
|
|
|
|
id_3 = create_template(service_2).id
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
json_resp_1 = admin_request.get(
|
|
|
|
|
|
"template.get_all_templates_for_service", service_id=service_1.id
|
|
|
|
|
|
)
|
|
|
|
|
|
json_resp_2 = admin_request.get(
|
|
|
|
|
|
"template.get_all_templates_for_service", service_id=service_2.id
|
|
|
|
|
|
)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert {template["id"] for template in json_resp_1["data"]} == {
|
|
|
|
|
|
str(id_1),
|
|
|
|
|
|
str(id_2),
|
|
|
|
|
|
}
|
|
|
|
|
|
assert {template["id"] for template in json_resp_2["data"]} == {str(id_3)}
|
2016-02-22 12:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"extra_args",
|
|
|
|
|
|
(
|
|
|
|
|
|
{},
|
|
|
|
|
|
{"detailed": True},
|
|
|
|
|
|
{"detailed": "True"},
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
2020-06-16 17:10:25 +01:00
|
|
|
|
def test_should_get_return_all_fields_by_default(
|
|
|
|
|
|
admin_request,
|
|
|
|
|
|
sample_email_template,
|
|
|
|
|
|
extra_args,
|
|
|
|
|
|
):
|
|
|
|
|
|
json_response = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template.get_all_templates_for_service",
|
2020-06-16 17:10:25 +01:00
|
|
|
|
service_id=sample_email_template.service.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
**extra_args,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert json_response["data"][0].keys() == {
|
|
|
|
|
|
"archived",
|
|
|
|
|
|
"content",
|
|
|
|
|
|
"created_at",
|
|
|
|
|
|
"created_by",
|
|
|
|
|
|
"folder",
|
|
|
|
|
|
"hidden",
|
|
|
|
|
|
"id",
|
|
|
|
|
|
"name",
|
|
|
|
|
|
"process_type",
|
|
|
|
|
|
"redact_personalisation",
|
|
|
|
|
|
"reply_to",
|
|
|
|
|
|
"reply_to_text",
|
|
|
|
|
|
"service",
|
|
|
|
|
|
"subject",
|
|
|
|
|
|
"template_redacted",
|
|
|
|
|
|
"template_type",
|
|
|
|
|
|
"updated_at",
|
|
|
|
|
|
"version",
|
2020-06-16 17:10:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"extra_args",
|
|
|
|
|
|
(
|
|
|
|
|
|
{"detailed": False},
|
|
|
|
|
|
{"detailed": "False"},
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"template_type, expected_content",
|
|
|
|
|
|
(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
(TemplateType.EMAIL, None),
|
|
|
|
|
|
(TemplateType.SMS, None),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
),
|
|
|
|
|
|
)
|
2020-06-16 17:10:25 +01:00
|
|
|
|
def test_should_not_return_content_and_subject_if_requested(
|
|
|
|
|
|
admin_request,
|
2020-10-13 16:08:33 +01:00
|
|
|
|
sample_service,
|
2020-06-16 17:10:25 +01:00
|
|
|
|
extra_args,
|
2020-10-13 16:08:33 +01:00
|
|
|
|
template_type,
|
|
|
|
|
|
expected_content,
|
2020-06-16 17:10:25 +01:00
|
|
|
|
):
|
2020-10-13 16:08:33 +01:00
|
|
|
|
create_template(
|
|
|
|
|
|
sample_service,
|
|
|
|
|
|
template_type=template_type,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
content="This is a test",
|
2020-10-13 16:08:33 +01:00
|
|
|
|
)
|
2020-06-16 17:10:25 +01:00
|
|
|
|
json_response = admin_request.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template.get_all_templates_for_service",
|
2020-10-13 16:08:33 +01:00
|
|
|
|
service_id=sample_service.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
**extra_args,
|
|
|
|
|
|
)
|
|
|
|
|
|
assert json_response["data"][0].keys() == {
|
|
|
|
|
|
"content",
|
|
|
|
|
|
"folder",
|
|
|
|
|
|
"id",
|
|
|
|
|
|
"name",
|
|
|
|
|
|
"template_type",
|
2020-06-16 17:10:25 +01:00
|
|
|
|
}
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_response["data"][0]["content"] == expected_content
|
2020-06-16 17:10:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-06-16 13:51:20 +01:00
|
|
|
|
@pytest.mark.parametrize(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"subject, content, template_type",
|
|
|
|
|
|
[
|
2016-06-16 13:51:20 +01:00
|
|
|
|
(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"about your ((thing))",
|
|
|
|
|
|
"hello ((name)) we’ve received your ((thing))",
|
2024-01-18 10:26:40 -05:00
|
|
|
|
TemplateType.EMAIL,
|
2016-06-16 13:51:20 +01:00
|
|
|
|
),
|
2024-01-18 10:26:40 -05:00
|
|
|
|
(None, "hello ((name)) we’ve received your ((thing))", TemplateType.SMS),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
],
|
2016-06-17 12:57:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
def test_should_get_a_single_template(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client, sample_user, sample_service, subject, content, template_type
|
2016-06-17 12:57:43 +01:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template = create_template(
|
|
|
|
|
|
sample_service, template_type=template_type, subject=subject, content=content
|
|
|
|
|
|
)
|
2016-06-17 12:57:43 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
response = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template/{}".format(sample_service.id, template.id),
|
|
|
|
|
|
headers=[create_admin_authorization_header()],
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
2016-06-17 12:57:43 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = json.loads(response.get_data(as_text=True))["data"]
|
2016-06-17 12:57:43 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert response.status_code == 200
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert data["content"] == content
|
|
|
|
|
|
assert data["subject"] == subject
|
|
|
|
|
|
assert data["process_type"] == "normal"
|
|
|
|
|
|
assert not data["redact_personalisation"]
|
2016-06-17 12:57:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"subject, content, path, expected_subject, expected_content, expected_error",
|
|
|
|
|
|
[
|
2016-06-16 13:51:20 +01:00
|
|
|
|
(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"about your thing",
|
|
|
|
|
|
"hello user we’ve received your thing",
|
|
|
|
|
|
"/service/{}/template/{}/preview",
|
|
|
|
|
|
"about your thing",
|
|
|
|
|
|
"hello user we’ve received your thing",
|
|
|
|
|
|
None,
|
2016-06-16 13:51:20 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"about your ((thing))",
|
|
|
|
|
|
"hello ((name)) we’ve received your ((thing))",
|
|
|
|
|
|
"/service/{}/template/{}/preview?name=Amala&thing=document",
|
|
|
|
|
|
"about your document",
|
|
|
|
|
|
"hello Amala we’ve received your document",
|
|
|
|
|
|
None,
|
2016-06-16 13:51:20 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"about your ((thing))",
|
|
|
|
|
|
"hello ((name)) we’ve received your ((thing))",
|
|
|
|
|
|
"/service/{}/template/{}/preview?eman=Amala&gniht=document",
|
|
|
|
|
|
None,
|
|
|
|
|
|
None,
|
|
|
|
|
|
"Missing personalisation: thing, name",
|
2016-06-16 13:51:20 +01:00
|
|
|
|
),
|
|
|
|
|
|
(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"about your ((thing))",
|
|
|
|
|
|
"hello ((name)) we’ve received your ((thing))",
|
|
|
|
|
|
"/service/{}/template/{}/preview?name=Amala&thing=document&foo=bar",
|
|
|
|
|
|
"about your document",
|
|
|
|
|
|
"hello Amala we’ve received your document",
|
2017-03-07 16:03:10 +00:00
|
|
|
|
None,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
),
|
|
|
|
|
|
],
|
2016-06-16 13:51:20 +01:00
|
|
|
|
)
|
2016-06-17 12:57:43 +01:00
|
|
|
|
def test_should_preview_a_single_template(
|
2016-11-07 15:37:39 +00:00
|
|
|
|
client,
|
2019-10-30 10:51:07 +00:00
|
|
|
|
sample_service,
|
2016-06-16 13:51:20 +01:00
|
|
|
|
subject,
|
|
|
|
|
|
content,
|
|
|
|
|
|
path,
|
|
|
|
|
|
expected_subject,
|
|
|
|
|
|
expected_content,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
expected_error,
|
2016-06-16 13:51:20 +01:00
|
|
|
|
):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template = create_template(
|
2024-01-18 10:26:40 -05:00
|
|
|
|
sample_service,
|
|
|
|
|
|
template_type=TemplateType.EMAIL,
|
|
|
|
|
|
subject=subject,
|
|
|
|
|
|
content=content,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
response = client.get(
|
2019-10-30 10:51:07 +00:00
|
|
|
|
path.format(sample_service.id, template.id),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
headers=[create_admin_authorization_header()],
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
content = json.loads(response.get_data(as_text=True))
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
if expected_error:
|
|
|
|
|
|
assert response.status_code == 400
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert content["message"]["template"] == [expected_error]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
else:
|
|
|
|
|
|
assert response.status_code == 200
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert content["content"] == expected_content
|
|
|
|
|
|
assert content["subject"] == expected_subject
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_empty_array_if_no_templates_for_service(client, sample_service):
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id), headers=[auth_header]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert len(json_resp["data"]) == 0
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_return_404_if_no_templates_for_service_with_id(
|
|
|
|
|
|
client, sample_service, fake_uuid
|
|
|
|
|
|
):
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template/{}".format(sample_service.id, fake_uuid),
|
|
|
|
|
|
headers=[auth_header],
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert json_resp["result"] == "error"
|
|
|
|
|
|
assert json_resp["message"] == "No result found"
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-01-18 10:26:40 -05:00
|
|
|
|
@pytest.mark.parametrize("template_type", (TemplateType.SMS,))
|
2021-04-22 14:42:54 +01:00
|
|
|
|
def test_create_400_for_over_limit_content(
|
|
|
|
|
|
client,
|
|
|
|
|
|
notify_api,
|
|
|
|
|
|
sample_user,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
template_type,
|
|
|
|
|
|
):
|
|
|
|
|
|
sample_service = create_service(service_permissions=[template_type])
|
2023-08-29 14:54:30 -07:00
|
|
|
|
content = "".join(
|
|
|
|
|
|
random.choice(string.ascii_uppercase + string.digits)
|
|
|
|
|
|
for _ in range(SMS_CHAR_COUNT_LIMIT + 1)
|
|
|
|
|
|
)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "too big template",
|
|
|
|
|
|
"template_type": template_type,
|
|
|
|
|
|
"content": content,
|
|
|
|
|
|
"service": str(sample_service.id),
|
|
|
|
|
|
"created_by": str(sample_service.created_by.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template".format(sample_service.id),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=data,
|
2016-11-07 15:37:39 +00: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 ("Content has a character count greater than the limit of {}").format(
|
|
|
|
|
|
SMS_CHAR_COUNT_LIMIT
|
|
|
|
|
|
) in json_resp["message"]["content"]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_update_400_for_over_limit_content(
|
|
|
|
|
|
client, notify_api, sample_user, sample_template
|
|
|
|
|
|
):
|
|
|
|
|
|
json_data = json.dumps(
|
|
|
|
|
|
{
|
|
|
|
|
|
"content": "".join(
|
|
|
|
|
|
random.choice(string.ascii_uppercase + string.digits)
|
|
|
|
|
|
for _ in range(SMS_CHAR_COUNT_LIMIT + 1)
|
|
|
|
|
|
),
|
|
|
|
|
|
"created_by": str(sample_user.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
resp = client.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"/service/{}/template/{}".format(
|
|
|
|
|
|
sample_template.service.id, sample_template.id
|
|
|
|
|
|
),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
data=json_data,
|
2016-11-07 15:37:39 +00: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 ("Content has a character count greater than the limit of {}").format(
|
|
|
|
|
|
SMS_CHAR_COUNT_LIMIT
|
|
|
|
|
|
) in json_resp["message"]["content"]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_should_return_all_template_versions_for_service_and_template_id(
|
|
|
|
|
|
client, sample_template
|
|
|
|
|
|
):
|
2016-05-13 16:25:05 +01:00
|
|
|
|
original_content = sample_template.content
|
|
|
|
|
|
from app.dao.templates_dao import dao_update_template
|
2023-08-29 14:54:30 -07:00
|
|
|
|
|
|
|
|
|
|
sample_template.content = original_content + "1"
|
2016-05-13 16:25:05 +01:00
|
|
|
|
dao_update_template(sample_template)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sample_template.content = original_content + "2"
|
2016-05-13 16:25:05 +01:00
|
|
|
|
dao_update_template(sample_template)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
resp = client.get(
|
|
|
|
|
|
"/service/{}/template/{}/versions".format(
|
|
|
|
|
|
sample_template.service_id, sample_template.id
|
|
|
|
|
|
),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert resp.status_code == 200
|
2023-08-29 14:54:30 -07:00
|
|
|
|
resp_json = json.loads(resp.get_data(as_text=True))["data"]
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert len(resp_json) == 3
|
|
|
|
|
|
for x in resp_json:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
if x["version"] == 1:
|
|
|
|
|
|
assert x["content"] == original_content
|
|
|
|
|
|
elif x["version"] == 2:
|
|
|
|
|
|
assert x["content"] == original_content + "1"
|
2016-11-07 15:37:39 +00:00
|
|
|
|
else:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert x["content"] == original_content + "2"
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_update_does_not_create_new_version_when_there_is_no_change(
|
|
|
|
|
|
client, sample_template
|
|
|
|
|
|
):
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template_type": sample_template.template_type,
|
|
|
|
|
|
"content": sample_template.content,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
}
|
2023-08-29 14:54:30 -07:00
|
|
|
|
resp = client.post(
|
|
|
|
|
|
"/service/{}/template/{}".format(
|
|
|
|
|
|
sample_template.service_id, sample_template.id
|
|
|
|
|
|
),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
2016-06-01 10:53:03 +01:00
|
|
|
|
template = dao_get_template_by_id(sample_template.id)
|
|
|
|
|
|
assert template.version == 1
|
2017-01-17 15:48:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_set_process_type_on_template(client, sample_template):
|
2021-08-04 15:12:09 +01:00
|
|
|
|
auth_header = create_admin_authorization_header()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
data = {"process_type": "priority"}
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
|
"/service/{}/template/{}".format(
|
|
|
|
|
|
sample_template.service_id, sample_template.id
|
|
|
|
|
|
),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[("Content-Type", "application/json"), auth_header],
|
|
|
|
|
|
)
|
2017-01-17 15:48:51 +00:00
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(sample_template.id)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert template.process_type == "priority"
|
2017-06-28 16:49:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"post_data, expected_errors",
|
|
|
|
|
|
[
|
|
|
|
|
|
(
|
|
|
|
|
|
{},
|
|
|
|
|
|
[
|
|
|
|
|
|
{
|
|
|
|
|
|
"error": "ValidationError",
|
|
|
|
|
|
"message": "subject is a required property",
|
|
|
|
|
|
},
|
|
|
|
|
|
{"error": "ValidationError", "message": "name is a required property"},
|
|
|
|
|
|
{
|
|
|
|
|
|
"error": "ValidationError",
|
|
|
|
|
|
"message": "template_type is a required property",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"error": "ValidationError",
|
|
|
|
|
|
"message": "content is a required property",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"error": "ValidationError",
|
|
|
|
|
|
"message": "service is a required property",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"error": "ValidationError",
|
|
|
|
|
|
"message": "created_by is a required property",
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
|
|
|
|
|
],
|
|
|
|
|
|
)
|
2020-06-09 11:02:22 +01:00
|
|
|
|
def test_create_template_validates_against_json_schema(
|
|
|
|
|
|
admin_request,
|
|
|
|
|
|
sample_service_full_permissions,
|
|
|
|
|
|
post_data,
|
|
|
|
|
|
expected_errors,
|
|
|
|
|
|
):
|
|
|
|
|
|
response = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template.create_template",
|
2020-06-09 11:02:22 +01:00
|
|
|
|
service_id=sample_service_full_permissions.id,
|
|
|
|
|
|
_data=post_data,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_expected_status=400,
|
2020-06-09 11:02:22 +01:00
|
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert response["errors"] == expected_errors
|
2017-12-15 17:06:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-06-28 16:49:43 +01:00
|
|
|
|
def test_update_redact_template(admin_request, sample_template):
|
|
|
|
|
|
assert sample_template.redact_personalisation is False
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"redact_personalisation": True,
|
|
|
|
|
|
"created_by": str(sample_template.created_by_id),
|
2017-06-28 16:49:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dt = datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
with freeze_time(dt):
|
|
|
|
|
|
resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template.update_template",
|
2017-06-28 16:49:43 +01:00
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_data=data,
|
2017-06-28 16:49:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert resp is None
|
|
|
|
|
|
|
|
|
|
|
|
assert sample_template.redact_personalisation is True
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert (
|
|
|
|
|
|
sample_template.template_redacted.updated_by_id == sample_template.created_by_id
|
|
|
|
|
|
)
|
2017-06-28 16:49:43 +01:00
|
|
|
|
assert sample_template.template_redacted.updated_at == dt
|
|
|
|
|
|
|
|
|
|
|
|
assert sample_template.version == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_update_redact_template_ignores_other_properties(
|
|
|
|
|
|
admin_request, sample_template
|
|
|
|
|
|
):
|
2017-06-28 16:49:43 +01:00
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"name": "Foo",
|
|
|
|
|
|
"redact_personalisation": True,
|
|
|
|
|
|
"created_by": str(sample_template.created_by_id),
|
2017-06-28 16:49:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template.update_template",
|
2017-06-28 16:49:43 +01:00
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_data=data,
|
2017-06-28 16:49:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert sample_template.redact_personalisation is True
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert sample_template.name != "Foo"
|
2017-06-28 16:49:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def test_update_redact_template_does_nothing_if_already_redacted(
|
|
|
|
|
|
admin_request, sample_template
|
|
|
|
|
|
):
|
2017-06-28 16:49:43 +01:00
|
|
|
|
dt = datetime.now()
|
|
|
|
|
|
with freeze_time(dt):
|
|
|
|
|
|
dao_redact_template(sample_template, sample_template.created_by_id)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"redact_personalisation": True,
|
|
|
|
|
|
"created_by": str(sample_template.created_by_id),
|
2017-06-28 16:49:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
with freeze_time(dt + timedelta(days=1)):
|
|
|
|
|
|
resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template.update_template",
|
2017-06-28 16:49:43 +01:00
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_data=data,
|
2017-06-28 16:49:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert resp is None
|
|
|
|
|
|
|
|
|
|
|
|
assert sample_template.redact_personalisation is True
|
|
|
|
|
|
# make sure that it hasn't been updated
|
|
|
|
|
|
assert sample_template.template_redacted.updated_at == dt
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-29 12:39:02 +01:00
|
|
|
|
def test_update_redact_template_400s_if_no_created_by(admin_request, sample_template):
|
2017-06-28 16:49:43 +01:00
|
|
|
|
original_updated_time = sample_template.template_redacted.updated_at
|
2017-06-28 17:03:12 +01:00
|
|
|
|
resp = admin_request.post(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template.update_template",
|
2017-06-28 16:49:43 +01:00
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
_data={"redact_personalisation": True},
|
|
|
|
|
|
_expected_status=400,
|
2017-06-28 16:49:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
assert resp == {"result": "error", "message": {"created_by": ["Field is required"]}}
|
2017-06-28 17:03:12 +01:00
|
|
|
|
|
2017-06-28 16:49:43 +01:00
|
|
|
|
assert sample_template.redact_personalisation is False
|
|
|
|
|
|
assert sample_template.template_redacted.updated_at == original_updated_time
|