2016-01-13 11:04:13 +00:00
|
|
|
|
import json
|
2016-04-29 10:36:59 +01:00
|
|
|
|
import random
|
|
|
|
|
|
import string
|
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-01-09 16:41:58 +00:00
|
|
|
|
from app.models import Template, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, TemplateHistory
|
2017-06-28 16:49:43 +01:00
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template
|
|
|
|
|
|
|
2016-01-15 16:22:03 +00:00
|
|
|
|
from tests import create_authorization_header
|
2017-06-27 18:56:35 +01:00
|
|
|
|
from tests.app.conftest import (
|
|
|
|
|
|
sample_template as create_sample_template,
|
2017-06-28 11:54:23 +01:00
|
|
|
|
sample_template_without_email_permission,
|
|
|
|
|
|
sample_template_without_letter_permission,
|
2017-06-27 18:56:35 +01:00
|
|
|
|
sample_template_without_sms_permission,
|
|
|
|
|
|
)
|
2017-11-28 17:00:01 +00:00
|
|
|
|
from tests.app.db import create_service, create_letter_contact, create_template
|
2016-01-15 16:22:03 +00:00
|
|
|
|
|
2016-01-13 11:04:13 +00:00
|
|
|
|
|
2016-11-07 15:34:54 +00:00
|
|
|
|
@pytest.mark.parametrize('template_type, subject', [
|
2017-06-27 18:56:35 +01:00
|
|
|
|
(SMS_TYPE, None),
|
|
|
|
|
|
(EMAIL_TYPE, 'subject'),
|
|
|
|
|
|
(LETTER_TYPE, 'subject'),
|
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
|
|
|
|
):
|
2017-06-27 18:56:35 +01:00
|
|
|
|
service = create_service(service_permissions=[template_type])
|
2016-11-07 15:37:39 +00:00
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template',
|
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
|
'content': 'template <b>content</b>',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
'service': str(service.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
if subject:
|
|
|
|
|
|
data.update({'subject': subject})
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
2017-06-27 18:56:35 +01:00
|
|
|
|
'/service/{}/template'.format(service.id),
|
2016-11-07 15:37:39 +00:00
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 201
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert json_resp['data']['name'] == 'my template'
|
|
|
|
|
|
assert json_resp['data']['template_type'] == template_type
|
2017-01-19 12:05:28 +00:00
|
|
|
|
assert json_resp['data']['content'] == 'template <b>content</b>'
|
2017-06-27 18:56:35 +01:00
|
|
|
|
assert json_resp['data']['service'] == str(service.id)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert json_resp['data']['id']
|
|
|
|
|
|
assert json_resp['data']['version'] == 1
|
2017-01-17 15:48:51 +00:00
|
|
|
|
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:
|
|
|
|
|
|
assert json_resp['data']['subject'] == 'subject'
|
|
|
|
|
|
else:
|
|
|
|
|
|
assert not json_resp['data']['subject']
|
|
|
|
|
|
|
2017-01-17 15:48:51 +00:00
|
|
|
|
template = Template.query.get(json_resp['data']['id'])
|
|
|
|
|
|
from app.schemas import template_schema
|
|
|
|
|
|
assert sorted(json_resp['data']) == sorted(template_schema.dump(template).data)
|
|
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
2017-06-27 18:56:35 +01: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 = {
|
|
|
|
|
|
'name': 'my template',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
'template_type': SMS_TYPE,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
'content': 'template content',
|
|
|
|
|
|
'service': fake_uuid,
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
'/service/{}/template'.format(fake_uuid),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
|
assert json_resp['message'] == 'No result found'
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-27 18:56:35 +01:00
|
|
|
|
@pytest.mark.parametrize('permissions, template_type, subject, expected_error', [
|
2017-06-30 15:00:44 +01:00
|
|
|
|
([EMAIL_TYPE], SMS_TYPE, None, {'template_type': ['Creating text message templates is not allowed']}),
|
|
|
|
|
|
([SMS_TYPE], EMAIL_TYPE, 'subject', {'template_type': ['Creating email templates is not allowed']}),
|
|
|
|
|
|
([SMS_TYPE], LETTER_TYPE, 'subject', {'template_type': ['Creating letter templates is not allowed']}),
|
2017-06-27 18:56:35 +01:00
|
|
|
|
])
|
|
|
|
|
|
def test_should_raise_error_on_create_if_no_permission(
|
|
|
|
|
|
client, sample_user, permissions, template_type, subject, expected_error):
|
|
|
|
|
|
service = create_service(service_permissions=permissions)
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template',
|
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
|
'content': 'template content',
|
|
|
|
|
|
'service': str(service.id),
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
if subject:
|
|
|
|
|
|
data.update({'subject': subject})
|
|
|
|
|
|
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
'/service/{}/template'.format(service.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
2017-06-28 11:54:23 +01:00
|
|
|
|
assert response.status_code == 403
|
2017-06-27 18:56:35 +01:00
|
|
|
|
assert json_resp['result'] == 'error'
|
2017-06-28 11:54:23 +01:00
|
|
|
|
assert json_resp['message'] == expected_error
|
2017-06-27 18:56:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('template_factory, expected_error', [
|
2017-06-30 15:00:44 +01:00
|
|
|
|
(sample_template_without_sms_permission, {'template_type': ['Updating text message templates is not allowed']}),
|
|
|
|
|
|
(sample_template_without_email_permission, {'template_type': ['Updating email templates is not allowed']}),
|
|
|
|
|
|
(sample_template_without_letter_permission, {'template_type': ['Updating letter templates is not allowed']})
|
2017-06-27 18:56:35 +01:00
|
|
|
|
])
|
|
|
|
|
|
def test_should_be_error_on_update_if_no_permission(
|
|
|
|
|
|
client, sample_user, template_factory, expected_error, notify_db, notify_db_session):
|
|
|
|
|
|
template_without_permission = template_factory(notify_db, notify_db_session)
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'content': 'new template content',
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
update_response = client.post(
|
|
|
|
|
|
'/service/{}/template/{}'.format(
|
|
|
|
|
|
template_without_permission.service_id, template_without_permission.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2017-06-27 18:56:35 +01:00
|
|
|
|
assert json_resp['result'] == 'error'
|
2017-06-28 11:54:23 +01:00
|
|
|
|
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 = {
|
|
|
|
|
|
'name': 'my template',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
'template_type': SMS_TYPE,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
'content': 'template content',
|
|
|
|
|
|
'service': service_id
|
|
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
'/service/{}/template'.format(service_id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_error_if_service_does_not_exist_on_update(client, fake_uuid):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template'
|
|
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
'/service/{}/template/{}'.format(fake_uuid, fake_uuid),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
|
assert json_resp['message'] == 'No result found'
|
2016-02-22 12:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-06-27 18:56:35 +01:00
|
|
|
|
@pytest.mark.parametrize('template_type', [EMAIL_TYPE, LETTER_TYPE])
|
2016-11-07 15:37:39 +00:00
|
|
|
|
def test_must_have_a_subject_on_an_email_or_letter_template(client, sample_user, sample_service, template_type):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template',
|
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
|
'content': 'template content',
|
|
|
|
|
|
'service': str(sample_service.id),
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
'/service/{}/template'.format(sample_service.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
|
assert json_resp['message'] == {'subject': ['Invalid template subject']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_should_update_a_template(client, sample_user, sample_template):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'content': 'my template has new content <script type="text/javascript">alert("foo")</script>',
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
update_response = client.post(
|
|
|
|
|
|
'/service/{}/template/{}'.format(sample_template.service_id, sample_template.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert update_response.status_code == 200
|
|
|
|
|
|
update_json_resp = json.loads(update_response.get_data(as_text=True))
|
2017-01-19 12:05:28 +00:00
|
|
|
|
assert update_json_resp['data']['content'] == (
|
|
|
|
|
|
'my template has new content <script type="text/javascript">alert("foo")</script>'
|
|
|
|
|
|
)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert update_json_resp['data']['name'] == sample_template.name
|
|
|
|
|
|
assert update_json_resp['data']['template_type'] == sample_template.template_type
|
|
|
|
|
|
assert update_json_resp['data']['version'] == 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_archive_template(client, sample_template):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
|
'/service/{}/template/{}'.format(sample_template.service.id, sample_template.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=json_data
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
assert Template.query.first().archived
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_get_all_templates_for_a_service(client, sample_user, sample_service):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template 1',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
'template_type': EMAIL_TYPE,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
'subject': 'subject 1',
|
|
|
|
|
|
'content': 'template content',
|
|
|
|
|
|
'service': str(sample_service.id),
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
data_1 = json.dumps(data)
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template 2',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
'template_type': EMAIL_TYPE,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
'subject': 'subject 2',
|
|
|
|
|
|
'content': 'template content',
|
|
|
|
|
|
'service': str(sample_service.id),
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
data_2 = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
client.post(
|
|
|
|
|
|
'/service/{}/template'.format(sample_service.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data_1
|
|
|
|
|
|
)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
client.post(
|
|
|
|
|
|
'/service/{}/template'.format(sample_service.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data_2
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
|
|
|
|
|
'/service/{}/template'.format(sample_service.id),
|
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
update_json_resp = json.loads(response.get_data(as_text=True))
|
List templates in alphabetical order
Currently templates are ordered by the newest created first. This made
sense when, after creating a new template, you were landed on the page
that listed all the templates. In other words, you needed to see
confirmation of the thing that you’ve just done.
Now (since https://github.com/alphagov/notifications-admin/pull/1195)
you get landed on the page for just that template. So you always see
the template you’ve just created, no matter how the list of templates is
ordered. So we are free to change the order of the templates.
Ordering by time created is not great, because it gives users no control
over which templates appear first. For example, our research reported
this from one team:
> One frustration they have is that when you add a new template it
> automatically goes to the top of the list. To get round this, whenever
> they add a new template they delete all of the existing ones and start
> again because they want to keep their templates in numerical order.
> They'd like to be able to control the order of templates in the list.
We _could_ give people some sort of drag-and-drop template ordering
thing. But this feels like overkill. I think that alphabetical order is
better because:
- it’s easily discoverable – anyone who wants to know how a list is
ordered can quickly tell just by looking at it
- it’s universal – everyone knows how alphabetical ordering works
- it’s familiar – this is how people documents on their computer are
ordered; there’s no new UI to learn
- it’s what users are doing already – from the same service as above:
> They number their templates 1,2a, 2b, 3a etc
So this commit changes the ordering from newest created first to
alphabetical.
Previous changes to template order and navigation:
- https://github.com/alphagov/notifications-admin/pull/1163
- https://github.com/alphagov/notifications-admin/pull/1195
- https://github.com/alphagov/notifications-admin/pull/1330
Implementation notes
---
I refactored some of the tests here. I still don’t think they’re great
tests, but they’re a little more Pythonic now at least.
I also added a sort by template type, so that the order is deterministic
when you have, for example, an email template and a text message
template with the same name. If you have two text message templates with
the same name you’re on your own.
2017-11-23 11:37:35 +00:00
|
|
|
|
assert update_json_resp['data'][0]['name'] == 'my template 1'
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert update_json_resp['data'][0]['version'] == 1
|
|
|
|
|
|
assert update_json_resp['data'][0]['created_at']
|
List templates in alphabetical order
Currently templates are ordered by the newest created first. This made
sense when, after creating a new template, you were landed on the page
that listed all the templates. In other words, you needed to see
confirmation of the thing that you’ve just done.
Now (since https://github.com/alphagov/notifications-admin/pull/1195)
you get landed on the page for just that template. So you always see
the template you’ve just created, no matter how the list of templates is
ordered. So we are free to change the order of the templates.
Ordering by time created is not great, because it gives users no control
over which templates appear first. For example, our research reported
this from one team:
> One frustration they have is that when you add a new template it
> automatically goes to the top of the list. To get round this, whenever
> they add a new template they delete all of the existing ones and start
> again because they want to keep their templates in numerical order.
> They'd like to be able to control the order of templates in the list.
We _could_ give people some sort of drag-and-drop template ordering
thing. But this feels like overkill. I think that alphabetical order is
better because:
- it’s easily discoverable – anyone who wants to know how a list is
ordered can quickly tell just by looking at it
- it’s universal – everyone knows how alphabetical ordering works
- it’s familiar – this is how people documents on their computer are
ordered; there’s no new UI to learn
- it’s what users are doing already – from the same service as above:
> They number their templates 1,2a, 2b, 3a etc
So this commit changes the ordering from newest created first to
alphabetical.
Previous changes to template order and navigation:
- https://github.com/alphagov/notifications-admin/pull/1163
- https://github.com/alphagov/notifications-admin/pull/1195
- https://github.com/alphagov/notifications-admin/pull/1330
Implementation notes
---
I refactored some of the tests here. I still don’t think they’re great
tests, but they’re a little more Pythonic now at least.
I also added a sort by template type, so that the order is deterministic
when you have, for example, an email template and a text message
template with the same name. If you have two text message templates with
the same name you’re on your own.
2017-11-23 11:37:35 +00:00
|
|
|
|
assert update_json_resp['data'][1]['name'] == 'my template 2'
|
2016-11-07 15:37:39 +00:00
|
|
|
|
assert update_json_resp['data'][1]['version'] == 1
|
|
|
|
|
|
assert update_json_resp['data'][1]['created_at']
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-28 17:23:50 +00:00
|
|
|
|
def test_should_get_only_templates_for_that_service(admin_request, notify_db_session):
|
2017-11-28 17:00:01 +00:00
|
|
|
|
service_1 = create_service(service_name='service_1')
|
|
|
|
|
|
service_2 = create_service(service_name='service_2')
|
|
|
|
|
|
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
|
|
|
|
|
2017-11-28 17:00:01 +00: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
|
|
|
|
|
2017-11-28 17:00:01 +00: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
|
|
|
|
|
|
|
|
|
|
|
2016-06-16 13:51:20 +01:00
|
|
|
|
@pytest.mark.parametrize(
|
2016-06-17 12:57:43 +01:00
|
|
|
|
"subject, content, template_type", [
|
2016-06-16 13:51:20 +01:00
|
|
|
|
(
|
|
|
|
|
|
'about your ((thing))',
|
|
|
|
|
|
'hello ((name)) we’ve received your ((thing))',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
EMAIL_TYPE
|
2016-06-16 13:51:20 +01:00
|
|
|
|
),
|
2016-06-17 12:57:43 +01:00
|
|
|
|
(
|
|
|
|
|
|
None,
|
|
|
|
|
|
'hello ((name)) we’ve received your ((thing))',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
SMS_TYPE
|
2016-11-07 15:44:13 +00:00
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'about your ((thing))',
|
|
|
|
|
|
'hello ((name)) we’ve received your ((thing))',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
LETTER_TYPE
|
2016-06-17 12:57:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_should_get_a_single_template(
|
|
|
|
|
|
notify_db,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
client,
|
2016-06-17 12:57:43 +01:00
|
|
|
|
sample_user,
|
|
|
|
|
|
service_factory,
|
|
|
|
|
|
subject,
|
|
|
|
|
|
content,
|
|
|
|
|
|
template_type
|
|
|
|
|
|
):
|
|
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
template = create_sample_template(
|
|
|
|
|
|
notify_db, notify_db.session, subject_line=subject, content=content, template_type=template_type
|
|
|
|
|
|
)
|
2016-06-17 12:57:43 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
response = client.get(
|
|
|
|
|
|
'/service/{}/template/{}'.format(template.service.id, template.id),
|
|
|
|
|
|
headers=[create_authorization_header()]
|
|
|
|
|
|
)
|
2016-06-17 12:57:43 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00: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
|
|
|
|
|
|
assert data['content'] == content
|
|
|
|
|
|
assert data['subject'] == subject
|
2017-01-17 15:48:51 +00:00
|
|
|
|
assert data['process_type'] == 'normal'
|
2017-06-29 18:16:03 +01:00
|
|
|
|
assert not data['redact_personalisation']
|
2016-06-17 12:57:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
"subject, content, path, expected_subject, expected_content, expected_error", [
|
2016-06-16 13:51:20 +01: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
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'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
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'about your ((thing))',
|
|
|
|
|
|
'hello ((name)) we’ve received your ((thing))',
|
|
|
|
|
|
'/service/{}/template/{}/preview?eman=Amala&gniht=document',
|
|
|
|
|
|
None, None,
|
|
|
|
|
|
'Missing personalisation: thing, name'
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
'about your ((thing))',
|
|
|
|
|
|
'hello ((name)) we’ve received your ((thing))',
|
|
|
|
|
|
'/service/{}/template/{}/preview?name=Amala&thing=document&foo=bar',
|
2017-03-07 16:03:10 +00:00
|
|
|
|
'about your document',
|
|
|
|
|
|
'hello Amala we’ve received your document',
|
|
|
|
|
|
None,
|
2016-06-16 13:51:20 +01:00
|
|
|
|
)
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
2016-06-17 12:57:43 +01:00
|
|
|
|
def test_should_preview_a_single_template(
|
2016-06-16 13:51:20 +01:00
|
|
|
|
notify_db,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
client,
|
2016-06-16 13:51:20 +01:00
|
|
|
|
sample_user,
|
|
|
|
|
|
service_factory,
|
|
|
|
|
|
subject,
|
|
|
|
|
|
content,
|
|
|
|
|
|
path,
|
|
|
|
|
|
expected_subject,
|
|
|
|
|
|
expected_content,
|
|
|
|
|
|
expected_error
|
|
|
|
|
|
):
|
|
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
template = create_sample_template(
|
2017-06-27 18:56:35 +01:00
|
|
|
|
notify_db, notify_db.session, subject_line=subject, content=content, template_type=EMAIL_TYPE
|
2016-11-07 15:37:39 +00:00
|
|
|
|
)
|
2016-06-16 13:51:20 +01:00
|
|
|
|
|
2016-11-07 15:37:39 +00:00
|
|
|
|
response = client.get(
|
|
|
|
|
|
path.format(template.service.id, template.id),
|
|
|
|
|
|
headers=[create_authorization_header()]
|
|
|
|
|
|
)
|
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
|
|
|
|
|
|
assert content['message']['template'] == [expected_error]
|
|
|
|
|
|
else:
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
assert content['content'] == expected_content
|
|
|
|
|
|
assert content['subject'] == expected_subject
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_empty_array_if_no_templates_for_service(client, sample_service):
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
|
|
|
|
|
'/service/{}/template'.format(sample_service.id),
|
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert len(json_resp['data']) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_404_if_no_templates_for_service_with_id(client, sample_service, fake_uuid):
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
|
|
|
|
|
'/service/{}/template/{}'.format(sample_service.id, fake_uuid),
|
|
|
|
|
|
headers=[auth_header]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert json_resp['result'] == 'error'
|
|
|
|
|
|
assert json_resp['message'] == 'No result found'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_400_for_over_limit_content(client, notify_api, sample_user, sample_service, fake_uuid):
|
|
|
|
|
|
|
|
|
|
|
|
limit = notify_api.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
|
content = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(limit + 1))
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'too big template',
|
2017-06-27 18:56:35 +01:00
|
|
|
|
'template_type': SMS_TYPE,
|
2016-11-07 15:37:39 +00:00
|
|
|
|
'content': content,
|
|
|
|
|
|
'service': str(sample_service.id),
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
}
|
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
|
|
'/service/{}/template'.format(sample_service.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
assert (
|
|
|
|
|
|
'Content has a character count greater than the limit of {}'
|
|
|
|
|
|
).format(limit) in json_resp['message']['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_400_for_over_limit_content(client, notify_api, sample_user, sample_template):
|
|
|
|
|
|
|
|
|
|
|
|
limit = notify_api.config.get('SMS_CHAR_COUNT_LIMIT')
|
|
|
|
|
|
json_data = json.dumps({
|
|
|
|
|
|
'content': ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(limit + 1)),
|
|
|
|
|
|
'created_by': str(sample_user.id)
|
|
|
|
|
|
})
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
resp = client.post(
|
|
|
|
|
|
'/service/{}/template/{}'.format(sample_template.service.id, sample_template.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header],
|
|
|
|
|
|
data=json_data
|
|
|
|
|
|
)
|
|
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
|
assert (
|
|
|
|
|
|
'Content has a character count greater than the limit of {}'
|
|
|
|
|
|
).format(limit) in json_resp['message']['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
sample_template.content = original_content + '1'
|
|
|
|
|
|
dao_update_template(sample_template)
|
|
|
|
|
|
sample_template.content = original_content + '2'
|
|
|
|
|
|
dao_update_template(sample_template)
|
2016-11-07 15:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
resp = client.get('/service/{}/template/{}/versions'.format(sample_template.service_id, sample_template.id),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
resp_json = json.loads(resp.get_data(as_text=True))['data']
|
|
|
|
|
|
assert len(resp_json) == 3
|
|
|
|
|
|
for x in resp_json:
|
|
|
|
|
|
if x['version'] == 1:
|
|
|
|
|
|
assert x['content'] == original_content
|
|
|
|
|
|
elif x['version'] == 2:
|
|
|
|
|
|
assert x['content'] == original_content + '1'
|
|
|
|
|
|
else:
|
|
|
|
|
|
assert x['content'] == original_content + '2'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_does_not_create_new_version_when_there_is_no_change(client, sample_template):
|
|
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'template_type': sample_template.template_type,
|
|
|
|
|
|
'content': sample_template.content,
|
|
|
|
|
|
}
|
|
|
|
|
|
resp = client.post('/service/{}/template/{}'.format(sample_template.service_id, sample_template.id),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
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):
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
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])
|
|
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(sample_template.id)
|
|
|
|
|
|
assert template.process_type == 'priority'
|
2017-06-28 16:49:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-11-23 17:02:15 +00:00
|
|
|
|
def test_create_a_template_with_reply_to(admin_request, sample_user):
|
|
|
|
|
|
service = create_service(service_permissions=['letter'])
|
|
|
|
|
|
letter_contact = create_letter_contact(service, "Edinburgh, ED1 1AA")
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template',
|
|
|
|
|
|
'subject': 'subject',
|
|
|
|
|
|
'template_type': 'letter',
|
|
|
|
|
|
'content': 'template <b>content</b>',
|
|
|
|
|
|
'service': str(service.id),
|
|
|
|
|
|
'created_by': str(sample_user.id),
|
|
|
|
|
|
'reply_to': str(letter_contact.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.post('template.create_template', service_id=service.id, _data=data, _expected_status=201)
|
|
|
|
|
|
|
|
|
|
|
|
assert json_resp['data']['template_type'] == 'letter'
|
2018-01-04 15:56:58 +00:00
|
|
|
|
assert json_resp['data']['reply_to'] == str(letter_contact.id)
|
|
|
|
|
|
assert json_resp['data']['reply_to_text'] == letter_contact.contact_block
|
2017-11-23 17:02:15 +00:00
|
|
|
|
|
|
|
|
|
|
template = Template.query.get(json_resp['data']['id'])
|
|
|
|
|
|
from app.schemas import template_schema
|
|
|
|
|
|
assert sorted(json_resp['data']) == sorted(template_schema.dump(template).data)
|
2018-01-09 16:41:58 +00:00
|
|
|
|
th = TemplateHistory.query.filter_by(id=template.id, version=1).one()
|
|
|
|
|
|
assert th.service_letter_contact_id == letter_contact.id
|
2017-11-23 17:02:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-15 17:06:11 +00:00
|
|
|
|
def test_create_a_template_with_foreign_service_reply_to(admin_request, sample_user):
|
|
|
|
|
|
service = create_service(service_permissions=['letter'])
|
|
|
|
|
|
service2 = create_service(service_name='test service', email_from='test@example.com',
|
|
|
|
|
|
service_permissions=['letter'])
|
|
|
|
|
|
letter_contact = create_letter_contact(service2, "Edinburgh, ED1 1AA")
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'my template',
|
|
|
|
|
|
'subject': 'subject',
|
|
|
|
|
|
'template_type': 'letter',
|
|
|
|
|
|
'content': 'template <b>content</b>',
|
|
|
|
|
|
'service': str(service.id),
|
|
|
|
|
|
'created_by': str(sample_user.id),
|
|
|
|
|
|
'reply_to': str(letter_contact.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json_resp = admin_request.post('template.create_template', service_id=service.id, _data=data, _expected_status=400)
|
|
|
|
|
|
|
|
|
|
|
|
assert json_resp['message'] == "letter_contact_id {} does not exist in database for service id {}".format(
|
|
|
|
|
|
str(letter_contact.id), str(service.id)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-08 15:54:58 +00:00
|
|
|
|
@pytest.mark.parametrize('template_default, service_default',
|
|
|
|
|
|
[('template address', 'service address'),
|
|
|
|
|
|
(None, 'service address'),
|
|
|
|
|
|
('template address', None),
|
2018-01-08 16:54:19 +00:00
|
|
|
|
(None, None)
|
2018-01-08 15:54:58 +00:00
|
|
|
|
])
|
|
|
|
|
|
def test_get_template_reply_to(client, sample_service, template_default, service_default):
|
2017-11-22 14:19:49 +00:00
|
|
|
|
auth_header = create_authorization_header()
|
2018-01-08 15:54:58 +00:00
|
|
|
|
if service_default:
|
|
|
|
|
|
create_letter_contact(
|
|
|
|
|
|
service=sample_service, contact_block=service_default, is_default=True
|
|
|
|
|
|
)
|
|
|
|
|
|
if template_default:
|
|
|
|
|
|
template_default_contact = create_letter_contact(
|
|
|
|
|
|
service=sample_service, contact_block=template_default, is_default=False
|
|
|
|
|
|
)
|
|
|
|
|
|
reply_to_id = str(template_default_contact.id) if template_default else None
|
|
|
|
|
|
template = create_template(service=sample_service, template_type='letter', reply_to=reply_to_id)
|
2017-11-22 14:19:49 +00:00
|
|
|
|
|
2018-01-08 15:54:58 +00:00
|
|
|
|
resp = client.get('/service/{}/template/{}'.format(template.service_id, template.id),
|
2017-11-22 14:19:49 +00:00
|
|
|
|
headers=[auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200, resp.get_data(as_text=True)
|
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
|
|
assert 'service_letter_contact_id' not in json_resp['data']
|
2018-01-08 15:54:58 +00:00
|
|
|
|
assert json_resp['data']['reply_to'] == reply_to_id
|
|
|
|
|
|
assert json_resp['data']['reply_to_text'] == template_default
|
2017-11-22 14:19:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_template_reply_to(client, sample_letter_template):
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
letter_contact = create_letter_contact(sample_letter_template.service, "Edinburgh, ED1 1AA")
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'reply_to': str(letter_contact.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resp = client.post('/service/{}/template/{}'.format(sample_letter_template.service_id, sample_letter_template.id),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200, resp.get_data(as_text=True)
|
|
|
|
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(sample_letter_template.id)
|
2018-01-09 16:41:58 +00:00
|
|
|
|
assert template.service_letter_contact_id == letter_contact.id
|
|
|
|
|
|
th = TemplateHistory.query.filter_by(id=sample_letter_template.id, version=2).one()
|
|
|
|
|
|
assert th.service_letter_contact_id == letter_contact.id
|
2017-11-22 14:19:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-01-10 12:40:14 +00:00
|
|
|
|
def test_update_template_reply_to_set_to_blank(client, notify_db_session):
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
service = create_service(service_permissions=['letter'])
|
|
|
|
|
|
letter_contact = create_letter_contact(service, "Edinburgh, ED1 1AA")
|
|
|
|
|
|
template = create_template(service=service, template_type='letter', reply_to=letter_contact.id)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'reply_to': None,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resp = client.post('/service/{}/template/{}'.format(template.service_id, template.id),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200, resp.get_data(as_text=True)
|
|
|
|
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(template.id)
|
|
|
|
|
|
assert template.service_letter_contact_id is None
|
|
|
|
|
|
th = TemplateHistory.query.filter_by(id=template.id, version=2).one()
|
|
|
|
|
|
assert th.service_letter_contact_id is None
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-15 17:06:11 +00:00
|
|
|
|
def test_update_template_with_foreign_service_reply_to(client, sample_letter_template):
|
|
|
|
|
|
auth_header = create_authorization_header()
|
|
|
|
|
|
|
|
|
|
|
|
service2 = create_service(service_name='test service', email_from='test@example.com',
|
|
|
|
|
|
service_permissions=['letter'])
|
|
|
|
|
|
letter_contact = create_letter_contact(service2, "Edinburgh, ED1 1AA")
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'reply_to': str(letter_contact.id),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resp = client.post('/service/{}/template/{}'.format(sample_letter_template.service_id, sample_letter_template.id),
|
|
|
|
|
|
data=json.dumps(data),
|
|
|
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 400, resp.get_data(as_text=True)
|
|
|
|
|
|
json_resp = json.loads(resp.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
|
|
assert json_resp['message'] == "letter_contact_id {} does not exist in database for service id {}".format(
|
|
|
|
|
|
str(letter_contact.id), str(sample_letter_template.service_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
|
|
'redact_personalisation': True,
|
2017-06-29 12:39:02 +01:00
|
|
|
|
'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(
|
|
|
|
|
|
'template.update_template',
|
|
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
|
|
|
|
|
_data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert resp is None
|
|
|
|
|
|
|
|
|
|
|
|
assert sample_template.redact_personalisation is True
|
|
|
|
|
|
assert sample_template.template_redacted.updated_by_id == sample_template.created_by_id
|
|
|
|
|
|
assert sample_template.template_redacted.updated_at == dt
|
|
|
|
|
|
|
|
|
|
|
|
assert sample_template.version == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_redact_template_ignores_other_properties(admin_request, sample_template):
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'name': 'Foo',
|
|
|
|
|
|
'redact_personalisation': True,
|
2017-06-29 12:39:02 +01:00
|
|
|
|
'created_by': str(sample_template.created_by_id)
|
2017-06-28 16:49:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
admin_request.post(
|
|
|
|
|
|
'template.update_template',
|
|
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
|
|
|
|
|
_data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert sample_template.redact_personalisation is True
|
|
|
|
|
|
assert sample_template.name != 'Foo'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_redact_template_does_nothing_if_already_redacted(admin_request, sample_template):
|
|
|
|
|
|
dt = datetime.now()
|
|
|
|
|
|
with freeze_time(dt):
|
|
|
|
|
|
dao_redact_template(sample_template, sample_template.created_by_id)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'redact_personalisation': True,
|
2017-06-29 12:39:02 +01:00
|
|
|
|
'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(
|
|
|
|
|
|
'template.update_template',
|
|
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
|
|
|
|
|
_data=data
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
2017-06-28 16:49:43 +01:00
|
|
|
|
'template.update_template',
|
|
|
|
|
|
service_id=sample_template.service_id,
|
|
|
|
|
|
template_id=sample_template.id,
|
2017-06-28 17:03:12 +01:00
|
|
|
|
_data={'redact_personalisation': True},
|
|
|
|
|
|
_expected_status=400
|
2017-06-28 16:49:43 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2017-06-28 17:03:12 +01:00
|
|
|
|
assert resp == {
|
|
|
|
|
|
'result': 'error',
|
2017-06-29 12:39:02 +01:00
|
|
|
|
'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
|