2017-06-28 10:26:25 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from freezegun import freeze_time
|
2018-12-17 10:37:19 +00:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2016-02-22 12:55:18 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2017-06-28 10:26:25 +01:00
|
|
|
import pytest
|
|
|
|
|
|
2016-01-13 12:14:21 +00:00
|
|
|
from app.dao.templates_dao import (
|
2016-02-22 09:46:16 +00:00
|
|
|
dao_create_template,
|
|
|
|
|
dao_get_template_by_id_and_service_id,
|
|
|
|
|
dao_get_all_templates_for_service,
|
2016-05-13 16:25:05 +01:00
|
|
|
dao_update_template,
|
2017-02-13 18:47:29 +00:00
|
|
|
dao_get_template_versions,
|
2018-01-10 12:40:14 +00:00
|
|
|
dao_redact_template, dao_update_template_reply_to
|
|
|
|
|
)
|
2018-03-07 15:42:59 +00:00
|
|
|
from app.models import (
|
|
|
|
|
Template,
|
2018-11-08 11:16:59 +00:00
|
|
|
TemplateFolder,
|
2018-03-07 15:42:59 +00:00
|
|
|
TemplateHistory,
|
2018-04-12 10:46:22 +01:00
|
|
|
TemplateRedacted
|
2018-03-07 15:42:59 +00:00
|
|
|
)
|
2017-06-28 10:26:25 +01:00
|
|
|
|
2017-11-21 14:53:06 +00:00
|
|
|
from tests.app.db import create_template, create_letter_contact
|
2016-01-13 12:14:21 +00:00
|
|
|
|
|
|
|
|
|
2016-11-07 15:34:54 +00:00
|
|
|
@pytest.mark.parametrize('template_type, subject', [
|
|
|
|
|
('sms', None),
|
|
|
|
|
('email', 'subject'),
|
2016-11-07 15:38:04 +00:00
|
|
|
('letter', 'subject'),
|
2016-11-07 15:34:54 +00:00
|
|
|
])
|
|
|
|
|
def test_create_template(sample_service, sample_user, template_type, subject):
|
2016-01-13 12:14:21 +00:00
|
|
|
data = {
|
2016-02-22 09:46:16 +00:00
|
|
|
'name': 'Sample Template',
|
2016-11-07 15:34:54 +00:00
|
|
|
'template_type': template_type,
|
2016-01-13 12:14:21 +00:00
|
|
|
'content': "Template content",
|
2016-04-25 10:38:37 +01:00
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user
|
2016-02-22 09:46:16 +00:00
|
|
|
}
|
2019-02-11 18:20:34 +00:00
|
|
|
if template_type == 'letter':
|
|
|
|
|
data['postage'] = 'second'
|
2016-11-07 15:34:54 +00:00
|
|
|
if subject:
|
|
|
|
|
data.update({'subject': subject})
|
2016-01-13 12:14:21 +00:00
|
|
|
template = Template(**data)
|
2016-02-22 09:46:16 +00:00
|
|
|
dao_create_template(template)
|
|
|
|
|
|
2016-01-13 12:14:21 +00:00
|
|
|
assert Template.query.count() == 1
|
2016-02-22 09:46:16 +00:00
|
|
|
assert len(dao_get_all_templates_for_service(sample_service.id)) == 1
|
|
|
|
|
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template'
|
2017-01-17 12:08:24 +00:00
|
|
|
assert dao_get_all_templates_for_service(sample_service.id)[0].process_type == 'normal'
|
2016-01-13 12:14:21 +00:00
|
|
|
|
|
|
|
|
|
2017-06-28 10:26:25 +01:00
|
|
|
def test_create_template_creates_redact_entry(sample_service):
|
|
|
|
|
assert TemplateRedacted.query.count() == 0
|
|
|
|
|
|
|
|
|
|
template = create_template(sample_service)
|
|
|
|
|
|
|
|
|
|
redacted = TemplateRedacted.query.one()
|
|
|
|
|
assert redacted.template_id == template.id
|
|
|
|
|
assert redacted.redact_personalisation is False
|
|
|
|
|
assert redacted.updated_by_id == sample_service.created_by_id
|
|
|
|
|
|
|
|
|
|
|
2017-11-21 14:53:06 +00:00
|
|
|
def test_create_template_with_reply_to(sample_service, sample_user):
|
|
|
|
|
letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA')
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "letter",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user,
|
|
|
|
|
'reply_to': letter_contact.id,
|
2019-02-11 18:20:34 +00:00
|
|
|
'postage': 'second'
|
2017-11-21 14:53:06 +00:00
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
|
|
|
|
|
assert dao_get_all_templates_for_service(sample_service.id)[0].reply_to == letter_contact.id
|
|
|
|
|
|
|
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
def test_update_template(sample_service, sample_user):
|
2016-02-22 09:46:16 +00:00
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "sms",
|
|
|
|
|
'content': "Template content",
|
2016-04-25 10:38:37 +01:00
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user
|
2016-02-22 09:46:16 +00:00
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
created = dao_get_all_templates_for_service(sample_service.id)[0]
|
|
|
|
|
assert created.name == 'Sample Template'
|
|
|
|
|
|
|
|
|
|
created.name = 'new name'
|
|
|
|
|
dao_update_template(created)
|
|
|
|
|
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'new name'
|
|
|
|
|
|
|
|
|
|
|
2018-11-08 11:16:59 +00:00
|
|
|
def test_update_template_in_a_folder_to_archived(sample_service, sample_user):
|
|
|
|
|
template_data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "sms",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user
|
|
|
|
|
}
|
|
|
|
|
template = Template(**template_data)
|
|
|
|
|
|
|
|
|
|
template_folder_data = {
|
|
|
|
|
'name': 'My Folder',
|
|
|
|
|
'service_id': sample_service.id,
|
|
|
|
|
}
|
|
|
|
|
template_folder = TemplateFolder(**template_folder_data)
|
|
|
|
|
|
|
|
|
|
template.folder = template_folder
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
|
|
|
|
|
template.archived = True
|
|
|
|
|
dao_update_template(template)
|
|
|
|
|
|
|
|
|
|
template_folder = TemplateFolder.query.one()
|
|
|
|
|
archived_template = Template.query.one()
|
|
|
|
|
|
|
|
|
|
assert template_folder
|
|
|
|
|
assert not archived_template.folder
|
|
|
|
|
|
|
|
|
|
|
2018-01-10 12:40:14 +00:00
|
|
|
def test_dao_update_template_reply_to_none_to_some(sample_service, sample_user):
|
2017-11-21 14:53:06 +00:00
|
|
|
letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA')
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "letter",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user,
|
2019-02-11 18:20:34 +00:00
|
|
|
'postage': 'second'
|
2017-11-21 14:53:06 +00:00
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
2018-01-10 12:40:14 +00:00
|
|
|
created = Template.query.get(template.id)
|
2017-11-21 14:53:06 +00:00
|
|
|
assert created.reply_to is None
|
2018-01-10 12:40:14 +00:00
|
|
|
assert created.service_letter_contact_id is None
|
2017-11-21 14:53:06 +00:00
|
|
|
|
2018-01-10 12:40:14 +00:00
|
|
|
dao_update_template_reply_to(template_id=template.id,
|
|
|
|
|
reply_to=letter_contact.id)
|
|
|
|
|
|
|
|
|
|
updated = Template.query.get(template.id)
|
|
|
|
|
assert updated.reply_to == letter_contact.id
|
|
|
|
|
assert updated.version == 2
|
|
|
|
|
assert updated.updated_at
|
2017-11-21 14:53:06 +00:00
|
|
|
|
|
|
|
|
template_history = TemplateHistory.query.filter_by(id=created.id, version=2).one()
|
2018-01-10 12:40:14 +00:00
|
|
|
assert template_history.service_letter_contact_id == letter_contact.id
|
|
|
|
|
assert template_history.updated_at == updated.updated_at
|
2017-11-21 14:53:06 +00:00
|
|
|
|
|
|
|
|
|
2018-12-17 10:03:54 +00:00
|
|
|
def test_dao_update_template_reply_to_some_to_some(sample_service, sample_user):
|
2018-01-09 16:41:58 +00:00
|
|
|
letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA')
|
2018-01-10 12:40:14 +00:00
|
|
|
letter_contact_2 = create_letter_contact(sample_service, 'London, N1 1DE')
|
2018-01-09 16:41:58 +00:00
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "letter",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user,
|
2019-02-11 18:20:34 +00:00
|
|
|
'service_letter_contact_id': letter_contact.id,
|
|
|
|
|
'postage': 'second',
|
2018-01-09 16:41:58 +00:00
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
2018-01-10 12:40:14 +00:00
|
|
|
created = Template.query.get(template.id)
|
|
|
|
|
dao_update_template_reply_to(template_id=created.id, reply_to=letter_contact_2.id)
|
|
|
|
|
updated = Template.query.get(template.id)
|
|
|
|
|
assert updated.reply_to == letter_contact_2.id
|
|
|
|
|
assert updated.version == 2
|
|
|
|
|
assert updated.updated_at
|
2018-01-09 16:41:58 +00:00
|
|
|
|
2018-01-10 12:40:14 +00:00
|
|
|
updated_history = TemplateHistory.query.filter_by(id=created.id, version=2).one()
|
|
|
|
|
assert updated_history.service_letter_contact_id == letter_contact_2.id
|
|
|
|
|
assert updated_history.updated_at == updated_history.updated_at
|
2018-01-09 16:41:58 +00:00
|
|
|
|
2018-01-10 12:40:14 +00:00
|
|
|
|
2018-01-10 13:32:54 +00:00
|
|
|
def test_dao_update_template_reply_to_some_to_none(sample_service, sample_user):
|
2018-01-10 12:40:14 +00:00
|
|
|
letter_contact = create_letter_contact(sample_service, 'Edinburgh, ED1 1AA')
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "letter",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user,
|
2019-02-11 18:20:34 +00:00
|
|
|
'service_letter_contact_id': letter_contact.id,
|
|
|
|
|
'postage': 'second'
|
2018-01-10 12:40:14 +00:00
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
created = Template.query.get(template.id)
|
|
|
|
|
dao_update_template_reply_to(template_id=created.id, reply_to=None)
|
|
|
|
|
updated = Template.query.get(template.id)
|
|
|
|
|
assert updated.reply_to is None
|
|
|
|
|
assert updated.version == 2
|
|
|
|
|
assert updated.updated_at
|
|
|
|
|
|
|
|
|
|
history = TemplateHistory.query.filter_by(id=created.id, version=2).one()
|
|
|
|
|
assert history.service_letter_contact_id is None
|
|
|
|
|
assert history.updated_at == updated.updated_at
|
2018-01-09 16:41:58 +00:00
|
|
|
|
|
|
|
|
|
2017-06-28 10:26:25 +01:00
|
|
|
def test_redact_template(sample_template):
|
|
|
|
|
redacted = TemplateRedacted.query.one()
|
|
|
|
|
assert redacted.template_id == sample_template.id
|
|
|
|
|
assert redacted.redact_personalisation is False
|
|
|
|
|
|
|
|
|
|
time = datetime.now()
|
|
|
|
|
with freeze_time(time):
|
|
|
|
|
dao_redact_template(sample_template, sample_template.created_by_id)
|
|
|
|
|
|
|
|
|
|
assert redacted.redact_personalisation is True
|
|
|
|
|
assert redacted.updated_at == time
|
|
|
|
|
assert redacted.updated_by_id == sample_template.created_by_id
|
|
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_all_templates_for_service(service_factory):
|
2016-03-31 17:46:18 +01:00
|
|
|
service_1 = service_factory.get('service 1', email_from='service.1')
|
|
|
|
|
service_2 = service_factory.get('service 2', email_from='service.2')
|
2016-02-22 09:46:16 +00:00
|
|
|
|
2016-01-13 12:14:21 +00:00
|
|
|
assert Template.query.count() == 2
|
2016-02-22 09:46:16 +00:00
|
|
|
assert len(dao_get_all_templates_for_service(service_1.id)) == 1
|
|
|
|
|
assert len(dao_get_all_templates_for_service(service_2.id)) == 1
|
|
|
|
|
|
2019-06-11 16:45:35 +01:00
|
|
|
create_template(
|
|
|
|
|
service=service_1,
|
2016-04-25 10:38:37 +01:00
|
|
|
template_name='Sample Template 1',
|
2016-02-22 09:46:16 +00:00
|
|
|
template_type="sms",
|
|
|
|
|
content="Template content",
|
|
|
|
|
)
|
2019-06-11 16:45:35 +01:00
|
|
|
create_template(
|
|
|
|
|
service=service_1,
|
2016-04-25 10:38:37 +01:00
|
|
|
template_name='Sample Template 2',
|
2016-02-22 09:46:16 +00:00
|
|
|
template_type="sms",
|
|
|
|
|
content="Template content",
|
|
|
|
|
)
|
2019-06-11 16:45:35 +01:00
|
|
|
create_template(
|
|
|
|
|
service=service_2,
|
2016-04-25 10:38:37 +01:00
|
|
|
template_name='Sample Template 3',
|
2016-02-22 09:46:16 +00:00
|
|
|
template_type="sms",
|
|
|
|
|
content="Template content",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert Template.query.count() == 5
|
|
|
|
|
assert len(dao_get_all_templates_for_service(service_1.id)) == 3
|
|
|
|
|
assert len(dao_get_all_templates_for_service(service_2.id)) == 2
|
|
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_all_templates_for_service_is_alphabetised(sample_service):
|
2019-06-11 16:45:35 +01:00
|
|
|
create_template(
|
2016-04-25 10:38:37 +01:00
|
|
|
template_name='Sample Template 1',
|
2016-02-22 09:46:16 +00:00
|
|
|
template_type="sms",
|
|
|
|
|
content="Template content",
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
2019-06-11 16:45:35 +01:00
|
|
|
template_2 = create_template(
|
2016-04-25 10:38:37 +01:00
|
|
|
template_name='Sample Template 2',
|
2016-02-22 09:46:16 +00:00
|
|
|
template_type="sms",
|
|
|
|
|
content="Template content",
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
2019-06-11 16:45:35 +01:00
|
|
|
create_template(
|
2016-04-25 10:38:37 +01:00
|
|
|
template_name='Sample Template 3',
|
2016-02-22 09:46:16 +00:00
|
|
|
template_type="sms",
|
|
|
|
|
content="Template content",
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
2016-01-13 12:14:21 +00:00
|
|
|
|
2016-02-22 09:46:16 +00:00
|
|
|
assert Template.query.count() == 3
|
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 dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template 1'
|
2016-02-22 09:46:16 +00:00
|
|
|
assert dao_get_all_templates_for_service(sample_service.id)[1].name == 'Sample Template 2'
|
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 dao_get_all_templates_for_service(sample_service.id)[2].name == 'Sample Template 3'
|
2016-01-13 12:14:21 +00:00
|
|
|
|
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
|
|
|
template_2.name = 'AAAAA Sample Template 2'
|
2016-04-11 17:39:49 +01:00
|
|
|
dao_update_template(template_2)
|
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 dao_get_all_templates_for_service(sample_service.id)[0].name == 'AAAAA Sample Template 2'
|
|
|
|
|
assert dao_get_all_templates_for_service(sample_service.id)[1].name == 'Sample Template 1'
|
2016-04-11 17:39:49 +01:00
|
|
|
|
2016-02-22 09:46:16 +00:00
|
|
|
|
|
|
|
|
def test_get_all_returns_empty_list_if_no_templates(sample_service):
|
2016-01-13 12:14:21 +00:00
|
|
|
assert Template.query.count() == 0
|
2016-02-22 09:46:16 +00:00
|
|
|
assert len(dao_get_all_templates_for_service(sample_service.id)) == 0
|
2016-01-13 12:14:21 +00:00
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_all_templates_ignores_archived_templates(sample_service):
|
2019-06-11 16:45:35 +01:00
|
|
|
normal_template = create_template(
|
2016-05-23 14:00:28 +01:00
|
|
|
template_name='Normal Template',
|
|
|
|
|
service=sample_service,
|
|
|
|
|
archived=False
|
|
|
|
|
)
|
2019-06-11 16:45:35 +01:00
|
|
|
archived_template = create_template(
|
2016-05-23 14:00:28 +01:00
|
|
|
template_name='Archived Template',
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
|
|
|
|
# sample_template fixture uses dao, which forces archived = False at creation.
|
|
|
|
|
archived_template.archived = True
|
|
|
|
|
dao_update_template(archived_template)
|
|
|
|
|
|
|
|
|
|
templates = dao_get_all_templates_for_service(sample_service.id)
|
|
|
|
|
|
|
|
|
|
assert len(templates) == 1
|
|
|
|
|
assert templates[0] == normal_template
|
|
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_all_templates_ignores_hidden_templates(sample_service):
|
2019-06-11 16:45:35 +01:00
|
|
|
normal_template = create_template(
|
2018-02-26 13:18:51 +00:00
|
|
|
template_name='Normal Template',
|
|
|
|
|
service=sample_service,
|
|
|
|
|
archived=False
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-11 16:45:35 +01:00
|
|
|
create_template(
|
2018-02-26 13:18:51 +00:00
|
|
|
template_name='Hidden Template',
|
|
|
|
|
hidden=True,
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
templates = dao_get_all_templates_for_service(sample_service.id)
|
|
|
|
|
|
|
|
|
|
assert len(templates) == 1
|
|
|
|
|
assert templates[0] == normal_template
|
|
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_template_by_id_and_service(sample_service):
|
2019-06-11 16:45:35 +01:00
|
|
|
sample_template = create_template(
|
2016-02-22 09:46:16 +00:00
|
|
|
template_name='Test Template',
|
|
|
|
|
service=sample_service)
|
2017-06-29 18:16:03 +01:00
|
|
|
template = dao_get_template_by_id_and_service_id(
|
2016-02-22 09:46:16 +00:00
|
|
|
template_id=sample_template.id,
|
2017-06-29 18:16:03 +01:00
|
|
|
service_id=sample_service.id)
|
|
|
|
|
assert template.id == sample_template.id
|
|
|
|
|
assert template.name == 'Test Template'
|
|
|
|
|
assert template.version == sample_template.version
|
|
|
|
|
assert not template.redact_personalisation
|
2016-02-22 09:46:16 +00:00
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_template_by_id_and_service_returns_none_for_hidden_templates(sample_service):
|
2019-06-11 16:45:35 +01:00
|
|
|
sample_template = create_template(
|
2018-02-26 13:18:51 +00:00
|
|
|
template_name='Test Template',
|
|
|
|
|
hidden=True,
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(NoResultFound):
|
|
|
|
|
dao_get_template_by_id_and_service_id(
|
|
|
|
|
template_id=sample_template.id,
|
|
|
|
|
service_id=sample_service.id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_template_version_returns_none_for_hidden_templates(sample_service):
|
2019-06-11 16:45:35 +01:00
|
|
|
sample_template = create_template(
|
2018-02-26 13:18:51 +00:00
|
|
|
template_name='Test Template',
|
|
|
|
|
hidden=True,
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(NoResultFound):
|
|
|
|
|
dao_get_template_by_id_and_service_id(
|
|
|
|
|
sample_template.id,
|
|
|
|
|
sample_service.id,
|
|
|
|
|
'1'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2016-04-08 13:34:46 +01:00
|
|
|
def test_get_template_by_id_and_service_returns_none_if_no_template(sample_service, fake_uuid):
|
2016-03-11 12:39:55 +00:00
|
|
|
with pytest.raises(NoResultFound) as e:
|
2016-04-08 13:34:46 +01:00
|
|
|
dao_get_template_by_id_and_service_id(template_id=fake_uuid, service_id=sample_service.id)
|
2016-03-11 12:39:55 +00:00
|
|
|
assert 'No row was found for one' in str(e.value)
|
2016-04-25 10:38:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_template_creates_a_history_record_with_current_data(sample_service, sample_user):
|
|
|
|
|
assert Template.query.count() == 0
|
2016-08-02 16:23:14 +01:00
|
|
|
assert TemplateHistory.query.count() == 0
|
2016-04-25 10:38:37 +01:00
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "email",
|
|
|
|
|
'subject': "subject",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user
|
|
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
|
|
|
|
|
assert Template.query.count() == 1
|
|
|
|
|
|
|
|
|
|
template_from_db = Template.query.first()
|
2016-08-02 16:23:14 +01:00
|
|
|
template_history = TemplateHistory.query.first()
|
2016-04-25 10:38:37 +01:00
|
|
|
|
|
|
|
|
assert template_from_db.id == template_history.id
|
|
|
|
|
assert template_from_db.name == template_history.name
|
|
|
|
|
assert template_from_db.version == 1
|
|
|
|
|
assert template_from_db.version == template_history.version
|
|
|
|
|
assert sample_user.id == template_history.created_by_id
|
|
|
|
|
assert template_from_db.created_by.id == template_history.created_by_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_template_creates_a_history_record_with_current_data(sample_service, sample_user):
|
|
|
|
|
assert Template.query.count() == 0
|
2016-08-02 16:23:14 +01:00
|
|
|
assert TemplateHistory.query.count() == 0
|
2016-04-25 10:38:37 +01:00
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "email",
|
|
|
|
|
'subject': "subject",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user
|
|
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
|
|
|
|
|
created = dao_get_all_templates_for_service(sample_service.id)[0]
|
|
|
|
|
assert created.name == 'Sample Template'
|
|
|
|
|
assert Template.query.count() == 1
|
|
|
|
|
assert Template.query.first().version == 1
|
2016-08-02 16:23:14 +01:00
|
|
|
assert TemplateHistory.query.count() == 1
|
2016-04-25 10:38:37 +01:00
|
|
|
|
|
|
|
|
created.name = 'new name'
|
|
|
|
|
dao_update_template(created)
|
|
|
|
|
|
|
|
|
|
assert Template.query.count() == 1
|
2016-08-02 16:23:14 +01:00
|
|
|
assert TemplateHistory.query.count() == 2
|
2016-04-25 10:38:37 +01:00
|
|
|
|
|
|
|
|
template_from_db = Template.query.first()
|
|
|
|
|
|
|
|
|
|
assert template_from_db.version == 2
|
|
|
|
|
|
2016-08-02 16:23:14 +01:00
|
|
|
assert TemplateHistory.query.filter_by(name='Sample Template').one().version == 1
|
|
|
|
|
assert TemplateHistory.query.filter_by(name='new name').one().version == 2
|
2016-05-06 15:42:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_template_history_version(sample_user, sample_service, sample_template):
|
|
|
|
|
old_content = sample_template.content
|
|
|
|
|
sample_template.content = "New content"
|
|
|
|
|
dao_update_template(sample_template)
|
|
|
|
|
old_template = dao_get_template_by_id_and_service_id(
|
|
|
|
|
sample_template.id,
|
|
|
|
|
sample_service.id,
|
|
|
|
|
'1'
|
|
|
|
|
)
|
|
|
|
|
assert old_template.content == old_content
|
2016-05-13 16:25:05 +01:00
|
|
|
|
|
|
|
|
|
2017-06-29 18:16:03 +01:00
|
|
|
def test_can_get_template_then_redacted_returns_right_values(sample_template):
|
|
|
|
|
template = dao_get_template_by_id_and_service_id(template_id=sample_template.id,
|
|
|
|
|
service_id=sample_template.service_id)
|
|
|
|
|
assert not template.redact_personalisation
|
|
|
|
|
dao_redact_template(template=template, user_id=sample_template.created_by_id)
|
|
|
|
|
assert template.redact_personalisation
|
|
|
|
|
|
|
|
|
|
|
2016-05-13 16:25:05 +01:00
|
|
|
def test_get_template_versions(sample_template):
|
|
|
|
|
original_content = sample_template.content
|
|
|
|
|
sample_template.content = 'new version'
|
|
|
|
|
dao_update_template(sample_template)
|
|
|
|
|
versions = dao_get_template_versions(service_id=sample_template.service_id, template_id=sample_template.id)
|
2016-08-08 16:57:39 +01:00
|
|
|
assert len(versions) == 2
|
|
|
|
|
versions = sorted(versions, key=lambda x: x.version)
|
|
|
|
|
assert versions[0].content == original_content
|
|
|
|
|
assert versions[1].content == 'new version'
|
|
|
|
|
|
|
|
|
|
assert versions[0].created_at == versions[1].created_at
|
|
|
|
|
|
|
|
|
|
assert versions[0].updated_at is None
|
|
|
|
|
assert versions[1].updated_at is not None
|
|
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
from app.schemas import template_history_schema
|
2016-05-13 16:25:05 +01:00
|
|
|
v = template_history_schema.load(versions, many=True)
|
2016-08-08 16:57:39 +01:00
|
|
|
assert len(v) == 2
|
2017-02-13 18:47:29 +00:00
|
|
|
|
|
|
|
|
|
2019-06-25 16:16:50 +01:00
|
|
|
def test_get_template_versions_is_empty_for_hidden_templates(sample_service):
|
2019-06-11 16:45:35 +01:00
|
|
|
sample_template = create_template(
|
2018-02-26 13:18:51 +00:00
|
|
|
template_name='Test Template',
|
|
|
|
|
hidden=True,
|
|
|
|
|
service=sample_service
|
|
|
|
|
)
|
|
|
|
|
versions = dao_get_template_versions(service_id=sample_template.service_id, template_id=sample_template.id)
|
|
|
|
|
assert len(versions) == 0
|
|
|
|
|
|
|
|
|
|
|
2018-12-17 10:37:19 +00:00
|
|
|
@pytest.mark.parametrize("template_type,postage", [('letter', 'third'), ('sms', 'second')])
|
|
|
|
|
def test_template_postage_constraint_on_create(sample_service, sample_user, template_type, postage):
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': template_type,
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user,
|
|
|
|
|
'postage': postage
|
|
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
with pytest.raises(expected_exception=SQLAlchemyError):
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_template_postage_constraint_on_update(sample_service, sample_user):
|
|
|
|
|
data = {
|
|
|
|
|
'name': 'Sample Template',
|
|
|
|
|
'template_type': "letter",
|
|
|
|
|
'content': "Template content",
|
|
|
|
|
'service': sample_service,
|
|
|
|
|
'created_by': sample_user,
|
|
|
|
|
'postage': 'second'
|
|
|
|
|
}
|
|
|
|
|
template = Template(**data)
|
|
|
|
|
dao_create_template(template)
|
|
|
|
|
created = dao_get_all_templates_for_service(sample_service.id)[0]
|
|
|
|
|
assert created.name == 'Sample Template'
|
|
|
|
|
|
|
|
|
|
created.postage = 'third'
|
|
|
|
|
with pytest.raises(expected_exception=SQLAlchemyError):
|
|
|
|
|
dao_update_template(created)
|