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..a9c9afece 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 = { @@ -65,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, } @@ -79,6 +82,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'] 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

', }