From f185dbecbefeb074ceb0406885fbc9b1c3838b35 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 7 Feb 2019 11:40:02 +0000 Subject: [PATCH 1/2] Return rendered HTML when previewing a template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If you’re trying to show what a Notify email will look like in your caseworking system all the API gives you at the moment is raw markdown (with the placeholders replaced). This isn’t that useful if your caseworkers have no idea what markdown is. If we also give teams the HTML then they can embed this in their systems, and the people using those systems will be able to see how headings, bulleted lists, etc. look. --- app/utils.py | 14 +++++- app/v2/template/template_schemas.py | 2 + tests/app/v2/template/test_post_template.py | 53 ++++++++++++++++++--- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/app/utils.py b/app/utils.py index d8916341f..fcd33b54b 100644 --- a/app/utils.py +++ b/app/utils.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta import pytz from flask import url_for from sqlalchemy import func -from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate +from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate, get_html_email_body from notifications_utils.timezones import convert_utc_to_bst local_timezone = pytz.timezone("Europe/London") @@ -35,6 +35,18 @@ def get_template_instance(template, values): }[template['template_type']](template, values) +def get_html_email_body_from_template(template_instance): + from app.models import EMAIL_TYPE + + if template_instance.template_type != EMAIL_TYPE: + return None + + return get_html_email_body( + template_instance.content, + template_instance.values, + ) + + def get_london_midnight_in_utc(date): """ This function converts date to midnight as BST (British Standard Time) to UTC, diff --git a/app/v2/template/template_schemas.py b/app/v2/template/template_schemas.py index ebd0b8342..38892fb99 100644 --- a/app/v2/template/template_schemas.py +++ b/app/v2/template/template_schemas.py @@ -1,5 +1,6 @@ from app.models import SMS_TYPE, TEMPLATE_TYPES from app.schema_validation.definitions import uuid, personalisation +from app.utils import get_html_email_body_from_template get_template_by_id_request = { @@ -79,6 +80,7 @@ def create_post_template_preview_response(template, template_object): "type": template.template_type, "version": template.version, "body": str(template_object), + "html": get_html_email_body_from_template(template_object), "subject": subject, "postage": template.postage } diff --git a/tests/app/v2/template/test_post_template.py b/tests/app/v2/template/test_post_template.py index 158d9b258..faa839da9 100644 --- a/tests/app/v2/template/test_post_template.py +++ b/tests/app/v2/template/test_post_template.py @@ -2,7 +2,7 @@ import pytest from flask import json -from app.models import SMS_TYPE, TEMPLATE_TYPES +from app.models import EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES from tests import create_authorization_header from tests.app.db import create_template @@ -16,36 +16,68 @@ valid_post = [ "Some content", None, "Some subject", - "Some content" + "Some content", + ( + '

' + 'Some content' + '

' + ), ), ( "Some subject", "Dear ((Name)), Hello. Yours Truly, The Government.", valid_personalisation, "Some subject", - "Dear Jo, Hello. Yours Truly, The Government." + "Dear Jo, Hello. Yours Truly, The Government.", + ( + '

' + 'Dear Jo, Hello. Yours Truly, The Government.' + '

' + ), ), ( "Message for ((Name))", "Dear ((Name)), Hello. Yours Truly, The Government.", valid_personalisation, "Message for Jo", - "Dear Jo, Hello. Yours Truly, The Government." + "Dear Jo, Hello. Yours Truly, The Government.", + ( + '

' + 'Dear Jo, Hello. Yours Truly, The Government.' + '

' + ), ), ( "Message for ((Name))", "Some content", valid_personalisation, "Message for Jo", - "Some content" + "Some content", + ( + '

' + 'Some content' + '

' + ), ), ] @pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES) -@pytest.mark.parametrize("subject,content,post_data,expected_subject,expected_content", valid_post) +@pytest.mark.parametrize( + "subject,content,post_data,expected_subject,expected_content,expected_html", + valid_post +) def test_valid_post_template_returns_200( - client, sample_service, tmp_type, subject, content, post_data, expected_subject, expected_content): + client, + sample_service, + tmp_type, + subject, + content, + post_data, + expected_subject, + expected_content, + expected_html, +): template = create_template( sample_service, template_type=tmp_type, @@ -64,8 +96,15 @@ def test_valid_post_template_returns_200( resp_json = json.loads(response.get_data(as_text=True)) assert resp_json['id'] == str(template.id) + if tmp_type != SMS_TYPE: assert expected_subject in resp_json['subject'] + + if tmp_type == EMAIL_TYPE: + assert resp_json['html'] == expected_html + else: + assert resp_json['html'] is None + assert expected_content in resp_json['body'] From aab36157a5c071d37e0eefcabd63e50f233879e6 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 8 Feb 2019 11:33:36 +0000 Subject: [PATCH 2/2] Test for HTML response in the schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because we test for the other properties in the schema. Also sets `additionalProperties` to `False` so we’re forced to update the schemas if we make similar changes in the future. This means removing `created_by` from the test data because it’s not returned by the real response. --- app/v2/template/template_schemas.py | 6 ++++-- tests/app/v2/template/test_template_schemas.py | 7 +++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/v2/template/template_schemas.py b/app/v2/template/template_schemas.py index 38892fb99..a9c9afece 100644 --- a/app/v2/template/template_schemas.py +++ b/app/v2/template/template_schemas.py @@ -66,9 +66,11 @@ post_template_preview_response = { "version": {"type": "integer"}, "body": {"type": "string"}, "subject": {"type": ["string", "null"]}, - "postage": {"type": "string"} + "postage": {"type": "string"}, + "html": {"type": ["string", "null"]}, }, - "required": ["id", "type", "version", "body"] + "required": ["id", "type", "version", "body"], + "additionalProperties": False, } diff --git a/tests/app/v2/template/test_template_schemas.py b/tests/app/v2/template/test_template_schemas.py index f5a89b922..de84ad661 100644 --- a/tests/app/v2/template/test_template_schemas.py +++ b/tests/app/v2/template/test_template_schemas.py @@ -71,17 +71,16 @@ valid_json_post_response = { 'id': str(uuid.uuid4()), 'type': 'email', 'version': 1, - 'created_by': 'someone@test.com', - 'body': 'some body' + 'body': 'some body', } valid_json_post_response_with_optionals = { 'id': str(uuid.uuid4()), 'type': 'email', 'version': 1, - 'created_by': 'someone@test.com', 'body': "some body", - 'subject': 'some subject' + 'subject': 'some subject', + 'html': '

some body

', }