Add a ‘preview template’ endpoint

There’s a need for users of the API to be able to take advantage of
Notify’s template rendering.

For example, there’s a service that’s building a case management system.
Their users are sending emails on a case-by-case basis. Before they
send an email, it’s ressuring to double check that the right data is
being inserted, that the right template is being used, etc.

This commit:
- adds a separate endpoint for previewing a template, with
  personalisation taken from the get parameters of the request
- beefs up the tests around getting a template

Not part of this pull request:
- making this enpoint publicly accessible
This commit is contained in:
Chris Hill-Scott
2016-06-16 13:51:20 +01:00
parent f7ceaf9009
commit cf91ce57fc
2 changed files with 103 additions and 0 deletions

View File

@@ -1,8 +1,10 @@
import json
import random
import string
import pytest
from app.models import Template
from tests import create_authorization_header
from tests.app.conftest import sample_template as create_sample_template
from app.dao.templates_dao import dao_get_template_by_id
@@ -324,6 +326,82 @@ def test_should_get_only_templates_for_that_service(notify_api, sample_user, ser
assert len(json_resp_4['data']) == 1
@pytest.mark.parametrize(
"subject, content, path, expected_subject, expected_content, expected_error", [
(
'about your ((thing))',
'hello ((name)) weve received your ((thing))',
'/service/{}/template/{}',
'about your ((thing))',
'hello ((name)) weve received your ((thing))',
None
),
(
'about your thing',
'hello user weve received your thing',
'/service/{}/template/{}/preview',
'about your thing',
'hello user weve received your thing',
None
),
(
'about your ((thing))',
'hello ((name)) weve received your ((thing))',
'/service/{}/template/{}/preview?name=Amala&thing=document',
'about your document',
'hello Amala weve received your document',
None
),
(
'about your ((thing))',
'hello ((name)) weve received your ((thing))',
'/service/{}/template/{}/preview?eman=Amala&gniht=document',
None, None,
'Missing personalisation: thing, name'
),
(
'about your ((thing))',
'hello ((name)) weve received your ((thing))',
'/service/{}/template/{}/preview?name=Amala&thing=document&foo=bar',
None, None,
'Personalisation not needed for template: foo'
)
]
)
def test_should_get_a_single_template(
notify_db,
notify_api,
sample_user,
service_factory,
subject,
content,
path,
expected_subject,
expected_content,
expected_error
):
with notify_api.test_request_context(), notify_api.test_client() as client:
template = create_sample_template(
notify_db, notify_db.session, subject_line=subject, content=content, template_type='email'
)
response = client.get(
path.format(template.service.id, template.id),
headers=[create_authorization_header()]
)
content = json.loads(response.get_data(as_text=True))
if expected_error:
assert response.status_code == 400
assert content['message']['template'] == [expected_error]
else:
assert response.status_code == 200
assert content['data']['content'] == expected_content
assert content['data']['subject'] == expected_subject
def test_should_return_empty_array_if_no_templates_for_service(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client: