mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Merge pull request #2344 from alphagov/html-response
Return rendered HTML when previewing a template
This commit is contained in:
14
app/utils.py
14
app/utils.py
@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
|||||||
import pytz
|
import pytz
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
from sqlalchemy import func
|
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
|
from notifications_utils.timezones import convert_utc_to_bst
|
||||||
|
|
||||||
local_timezone = pytz.timezone("Europe/London")
|
local_timezone = pytz.timezone("Europe/London")
|
||||||
@@ -35,6 +35,18 @@ def get_template_instance(template, values):
|
|||||||
}[template['template_type']](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):
|
def get_london_midnight_in_utc(date):
|
||||||
"""
|
"""
|
||||||
This function converts date to midnight as BST (British Standard Time) to UTC,
|
This function converts date to midnight as BST (British Standard Time) to UTC,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from app.models import SMS_TYPE, TEMPLATE_TYPES
|
from app.models import SMS_TYPE, TEMPLATE_TYPES
|
||||||
from app.schema_validation.definitions import uuid, personalisation
|
from app.schema_validation.definitions import uuid, personalisation
|
||||||
|
from app.utils import get_html_email_body_from_template
|
||||||
|
|
||||||
|
|
||||||
get_template_by_id_request = {
|
get_template_by_id_request = {
|
||||||
@@ -65,9 +66,11 @@ post_template_preview_response = {
|
|||||||
"version": {"type": "integer"},
|
"version": {"type": "integer"},
|
||||||
"body": {"type": "string"},
|
"body": {"type": "string"},
|
||||||
"subject": {"type": ["string", "null"]},
|
"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,
|
"type": template.template_type,
|
||||||
"version": template.version,
|
"version": template.version,
|
||||||
"body": str(template_object),
|
"body": str(template_object),
|
||||||
|
"html": get_html_email_body_from_template(template_object),
|
||||||
"subject": subject,
|
"subject": subject,
|
||||||
"postage": template.postage
|
"postage": template.postage
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import pytest
|
|||||||
|
|
||||||
from flask import json
|
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 import create_authorization_header
|
||||||
from tests.app.db import create_template
|
from tests.app.db import create_template
|
||||||
|
|
||||||
@@ -16,36 +16,68 @@ valid_post = [
|
|||||||
"Some content",
|
"Some content",
|
||||||
None,
|
None,
|
||||||
"Some subject",
|
"Some subject",
|
||||||
"Some content"
|
"Some content",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Some content'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"Some subject",
|
"Some subject",
|
||||||
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
||||||
valid_personalisation,
|
valid_personalisation,
|
||||||
"Some subject",
|
"Some subject",
|
||||||
"Dear Jo, Hello. Yours Truly, The Government."
|
"Dear Jo, Hello. Yours Truly, The Government.",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Dear Jo, Hello. Yours Truly, The Government.'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"Message for ((Name))",
|
"Message for ((Name))",
|
||||||
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
"Dear ((Name)), Hello. Yours Truly, The Government.",
|
||||||
valid_personalisation,
|
valid_personalisation,
|
||||||
"Message for Jo",
|
"Message for Jo",
|
||||||
"Dear Jo, Hello. Yours Truly, The Government."
|
"Dear Jo, Hello. Yours Truly, The Government.",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Dear Jo, Hello. Yours Truly, The Government.'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"Message for ((Name))",
|
"Message for ((Name))",
|
||||||
"Some content",
|
"Some content",
|
||||||
valid_personalisation,
|
valid_personalisation,
|
||||||
"Message for Jo",
|
"Message for Jo",
|
||||||
"Some content"
|
"Some content",
|
||||||
|
(
|
||||||
|
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
|
||||||
|
'Some content'
|
||||||
|
'</p>'
|
||||||
|
),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
@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(
|
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(
|
template = create_template(
|
||||||
sample_service,
|
sample_service,
|
||||||
template_type=tmp_type,
|
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))
|
resp_json = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
assert resp_json['id'] == str(template.id)
|
assert resp_json['id'] == str(template.id)
|
||||||
|
|
||||||
if tmp_type != SMS_TYPE:
|
if tmp_type != SMS_TYPE:
|
||||||
assert expected_subject in resp_json['subject']
|
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']
|
assert expected_content in resp_json['body']
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -71,17 +71,16 @@ valid_json_post_response = {
|
|||||||
'id': str(uuid.uuid4()),
|
'id': str(uuid.uuid4()),
|
||||||
'type': 'email',
|
'type': 'email',
|
||||||
'version': 1,
|
'version': 1,
|
||||||
'created_by': 'someone@test.com',
|
'body': 'some body',
|
||||||
'body': 'some body'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
valid_json_post_response_with_optionals = {
|
valid_json_post_response_with_optionals = {
|
||||||
'id': str(uuid.uuid4()),
|
'id': str(uuid.uuid4()),
|
||||||
'type': 'email',
|
'type': 'email',
|
||||||
'version': 1,
|
'version': 1,
|
||||||
'created_by': 'someone@test.com',
|
|
||||||
'body': "some body",
|
'body': "some body",
|
||||||
'subject': 'some subject'
|
'subject': 'some subject',
|
||||||
|
'html': '<p>some body</p>',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user