mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-08 03:48:25 -04:00
Merge pull request #1264 from alphagov/template-returns-placeholders
Return placeholders when getting a template
This commit is contained in:
@@ -18,6 +18,11 @@ from notifications_utils.recipients import (
|
|||||||
InvalidEmailError
|
InvalidEmailError
|
||||||
)
|
)
|
||||||
from notifications_utils.letter_timings import get_letter_timings
|
from notifications_utils.letter_timings import get_letter_timings
|
||||||
|
from notifications_utils.template import (
|
||||||
|
PlainTextEmailTemplate,
|
||||||
|
SMSMessageTemplate,
|
||||||
|
LetterDVLATemplate,
|
||||||
|
)
|
||||||
|
|
||||||
from app.encryption import (
|
from app.encryption import (
|
||||||
hashpw,
|
hashpw,
|
||||||
@@ -525,6 +530,22 @@ class Template(db.Model):
|
|||||||
_external=True
|
_external=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _as_utils_template(self):
|
||||||
|
if self.template_type == EMAIL_TYPE:
|
||||||
|
return PlainTextEmailTemplate(
|
||||||
|
{'content': self.content, 'subject': self.subject}
|
||||||
|
)
|
||||||
|
if self.template_type == SMS_TYPE:
|
||||||
|
return SMSMessageTemplate(
|
||||||
|
{'content': self.content}
|
||||||
|
)
|
||||||
|
if self.template_type == LETTER_TYPE:
|
||||||
|
return LetterDVLATemplate(
|
||||||
|
{'content': self.content, 'subject': self.subject},
|
||||||
|
notification_reference=1,
|
||||||
|
contact_block=self.service.letter_contact_block,
|
||||||
|
)
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
serialized = {
|
serialized = {
|
||||||
"id": str(self.id),
|
"id": str(self.id),
|
||||||
@@ -536,6 +557,12 @@ class Template(db.Model):
|
|||||||
"body": self.content,
|
"body": self.content,
|
||||||
"subject": self.subject if self.template_type != SMS_TYPE else None,
|
"subject": self.subject if self.template_type != SMS_TYPE else None,
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
|
"personalisation": {
|
||||||
|
key: {
|
||||||
|
'required': True,
|
||||||
|
}
|
||||||
|
for key in self._as_utils_template().placeholders
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return serialized
|
return serialized
|
||||||
@@ -576,6 +603,9 @@ class TemplateHistory(db.Model):
|
|||||||
nullable=False,
|
nullable=False,
|
||||||
default=NORMAL)
|
default=NORMAL)
|
||||||
|
|
||||||
|
def _as_utils_template(self):
|
||||||
|
return Template._as_utils_template(self)
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
return Template.serialize(self)
|
return Template.serialize(self)
|
||||||
|
|
||||||
|
|||||||
@@ -137,7 +137,8 @@ def sample_service(
|
|||||||
limit=1000,
|
limit=1000,
|
||||||
email_from=None,
|
email_from=None,
|
||||||
permissions=[SMS_TYPE, EMAIL_TYPE],
|
permissions=[SMS_TYPE, EMAIL_TYPE],
|
||||||
research_mode=None
|
research_mode=None,
|
||||||
|
letter_contact_block='London,\nSW1A 1AA',
|
||||||
):
|
):
|
||||||
if user is None:
|
if user is None:
|
||||||
user = create_user()
|
user = create_user()
|
||||||
@@ -150,7 +151,7 @@ def sample_service(
|
|||||||
'restricted': restricted,
|
'restricted': restricted,
|
||||||
'email_from': email_from,
|
'email_from': email_from,
|
||||||
'created_by': user,
|
'created_by': user,
|
||||||
'letter_contact_block': 'London,\nSW1A 1AA'
|
'letter_contact_block': letter_contact_block,
|
||||||
}
|
}
|
||||||
service = Service.query.filter_by(name=service_name).first()
|
service = Service.query.filter_by(name=service_name).first()
|
||||||
if not service:
|
if not service:
|
||||||
@@ -181,6 +182,11 @@ def sample_service_full_permissions(notify_db, notify_db_session):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='function')
|
||||||
|
def sample_service_custom_letter_contact_block(notify_db, notify_db_session):
|
||||||
|
return sample_service(notify_db, notify_db_session, letter_contact_block='((contact block))')
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
def sample_template(
|
def sample_template(
|
||||||
notify_db,
|
notify_db,
|
||||||
|
|||||||
@@ -40,11 +40,81 @@ def test_get_template_by_id_returns_200(client, sample_service, tmp_type, expect
|
|||||||
'body': template.content,
|
'body': template.content,
|
||||||
"subject": expected_subject,
|
"subject": expected_subject,
|
||||||
'name': expected_name,
|
'name': expected_name,
|
||||||
|
'personalisation': {},
|
||||||
}
|
}
|
||||||
|
|
||||||
assert json_response == expected_response
|
assert json_response == expected_response
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("create_template_args, expected_personalisation", [
|
||||||
|
(
|
||||||
|
{
|
||||||
|
"template_type": SMS_TYPE,
|
||||||
|
"content": "Hello ((placeholder)) ((conditional??yes))",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"placeholder": {
|
||||||
|
"required": True
|
||||||
|
},
|
||||||
|
"conditional": {
|
||||||
|
"required": True
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
"template_type": EMAIL_TYPE,
|
||||||
|
"subject": "((subject))",
|
||||||
|
"content": "((content))",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"subject": {
|
||||||
|
"required": True
|
||||||
|
},
|
||||||
|
"content": {
|
||||||
|
"required": True
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
"template_type": LETTER_TYPE,
|
||||||
|
"subject": "((letterSubject))",
|
||||||
|
"content": "((letter_content))",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"letterSubject": {
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
"letter_content": {
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
"contact block": {
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
])
|
||||||
|
@pytest.mark.parametrize("version", valid_version_params)
|
||||||
|
def test_get_template_by_id_returns_placeholders(
|
||||||
|
client,
|
||||||
|
sample_service_custom_letter_contact_block,
|
||||||
|
version,
|
||||||
|
create_template_args,
|
||||||
|
expected_personalisation,
|
||||||
|
):
|
||||||
|
template = create_template(sample_service_custom_letter_contact_block, **create_template_args)
|
||||||
|
auth_header = create_authorization_header(service_id=sample_service_custom_letter_contact_block.id)
|
||||||
|
|
||||||
|
version_path = '/version/{}'.format(version) if version else ''
|
||||||
|
|
||||||
|
response = client.get(path='/v2/template/{}{}'.format(template.id, version_path),
|
||||||
|
headers=[('Content-Type', 'application/json'), auth_header])
|
||||||
|
|
||||||
|
json_response = json.loads(response.get_data(as_text=True))
|
||||||
|
assert json_response['personalisation'] == expected_personalisation
|
||||||
|
|
||||||
|
|
||||||
def test_get_template_with_non_existent_template_id_returns_404(client, fake_uuid, sample_service):
|
def test_get_template_with_non_existent_template_id_returns_404(client, fake_uuid, sample_service):
|
||||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user