Files
notifications-api/tests/app/v2/templates/test_get_templates.py

120 lines
4.2 KiB
Python
Raw Permalink Normal View History

2021-03-10 13:55:06 +00:00
from itertools import product
2017-03-28 10:41:25 +01:00
2021-03-10 13:55:06 +00:00
import pytest
2017-03-28 10:41:25 +01:00
from flask import json
2021-03-10 13:55:06 +00:00
from app.models import EMAIL_TYPE, TEMPLATE_TYPES
from tests import create_service_authorization_header
2017-03-28 10:41:25 +01:00
from tests.app.db import create_template
2017-03-28 13:15:39 +01:00
def test_get_all_templates_returns_200(client, sample_service):
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
templates = [
create_template(
sample_service,
template_type=tmp_type,
subject='subject_{}'.format(name) if tmp_type == EMAIL_TYPE else '',
template_name=name,
)
for name, tmp_type in product(('A', 'B', 'C'), TEMPLATE_TYPES)
]
2017-03-28 10:41:25 +01:00
auth_header = create_service_authorization_header(service_id=sample_service.id)
2017-03-28 10:41:25 +01:00
response = client.get(path='/v2/templates',
2017-03-28 10:41:25 +01:00
headers=[('Content-Type', 'application/json'), auth_header])
2017-03-28 13:15:39 +01:00
assert response.status_code == 200
2017-03-28 10:41:25 +01:00
assert response.headers['Content-type'] == 'application/json'
json_response = 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 len(json_response['templates']) == len(templates)
2017-03-28 10:41:25 +01: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
for index, template in enumerate(json_response['templates']):
assert template['id'] == str(templates[index].id)
assert template['body'] == templates[index].content
assert template['type'] == templates[index].template_type
if templates[index].template_type == EMAIL_TYPE:
assert template['subject'] == templates[index].subject
2017-03-28 10:41:25 +01:00
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
2017-03-28 13:15:39 +01:00
def test_get_all_templates_for_valid_type_returns_200(client, sample_service, tmp_type):
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
templates = [
create_template(
sample_service,
template_type=tmp_type,
template_name='Template {}'.format(i),
subject='subject_{}'.format(i) if tmp_type == EMAIL_TYPE else ''
)
for i in range(3)
]
2017-03-28 10:41:25 +01:00
auth_header = create_service_authorization_header(service_id=sample_service.id)
2017-03-28 10:41:25 +01:00
response = client.get(path='/v2/templates?type={}'.format(tmp_type),
2017-03-28 10:41:25 +01:00
headers=[('Content-Type', 'application/json'), auth_header])
2017-03-28 13:15:39 +01:00
assert response.status_code == 200
2017-03-28 10:41:25 +01:00
assert response.headers['Content-type'] == 'application/json'
json_response = 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 len(json_response['templates']) == len(templates)
2017-03-28 10:41:25 +01: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
for index, template in enumerate(json_response['templates']):
assert template['id'] == str(templates[index].id)
assert template['body'] == templates[index].content
assert template['type'] == tmp_type
if templates[index].template_type == EMAIL_TYPE:
assert template['subject'] == templates[index].subject
2017-03-28 10:41:25 +01:00
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
def test_get_correct_num_templates_for_valid_type_returns_200(client, sample_service, tmp_type):
num_templates = 3
templates = []
for _ in range(num_templates):
templates.append(create_template(sample_service, template_type=tmp_type))
for other_type in TEMPLATE_TYPES:
if other_type != tmp_type:
templates.append(create_template(sample_service, template_type=other_type))
auth_header = create_service_authorization_header(service_id=sample_service.id)
response = client.get(path='/v2/templates?type={}'.format(tmp_type),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
json_response = json.loads(response.get_data(as_text=True))
assert len(json_response['templates']) == num_templates
2017-03-28 13:15:39 +01:00
def test_get_all_templates_for_invalid_type_returns_400(client, sample_service):
auth_header = create_service_authorization_header(service_id=sample_service.id)
2017-03-28 10:41:25 +01:00
2017-03-28 13:15:39 +01:00
invalid_type = 'coconut'
2017-03-28 10:41:25 +01:00
response = client.get(path='/v2/templates?type={}'.format(invalid_type),
2017-03-28 10:41:25 +01:00
headers=[('Content-Type', 'application/json'), auth_header])
2017-03-28 13:15:39 +01:00
assert response.status_code == 400
2017-03-28 10:41:25 +01:00
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))
2017-03-28 13:15:39 +01:00
assert json_response == {
'status_code': 400,
'errors': [
{
'message': 'type coconut is not one of [sms, email, letter, broadcast]',
2017-03-28 13:15:39 +01:00
'error': 'ValidationError'
}
]
}