From c92138d5ab4790ba76bed6d7385873b0492532b2 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 21 Jun 2016 16:45:13 +0100 Subject: [PATCH] return replaced subject back from get notifications API --- app/schemas.py | 9 ++++++--- tests/app/notifications/test_rest.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/schemas.py b/app/schemas.py index 9ee71cf49..23e99fb5a 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -248,7 +248,7 @@ class SmsAdminNotificationSchema(SmsNotificationSchema): class NotificationStatusSchema(BaseSchema): - template = fields.Nested(TemplateSchema, only=["id", "name", "template_type", "content"], dump_only=True) + template = fields.Nested(TemplateSchema, only=["id", "name", "template_type", "content", "subject"], dump_only=True) job = fields.Nested(JobSchema, only=["id", "original_file_name"], dump_only=True) personalisation = fields.Dict(required=False) @@ -267,10 +267,13 @@ class NotificationStatusSchema(BaseSchema): def handle_template_merge(self, in_data): if in_data.get('personalisation'): from notifications_utils.template import Template - merged = Template(in_data['template'], in_data['personalisation']).replaced - in_data['body'] = merged + template = Template(in_data['template'], in_data['personalisation']) + in_data['body'] = template.replaced + if in_data['template']['template_type'] == 'email': + in_data['subject'] = template.replaced_subject in_data.pop('personalisation', None) in_data['template'].pop('content', None) + in_data['template'].pop('subject', None) return in_data diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 277762f27..76157ccad 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -1248,6 +1248,30 @@ def test_get_notification_by_id_returns_merged_template_content(notify_db, notification = json.loads(response.get_data(as_text=True))['data']['notification'] assert response.status_code == 200 assert notification['body'] == 'Hello world' + assert 'subject' not in notification + + +def test_get_notification_by_id_returns_merged_template_content_for_email( + notify_db, + notify_db_session, + notify_api, + sample_email_template_with_placeholders +): + sample_notification = create_sample_notification(notify_db, + notify_db_session, + template=sample_email_template_with_placeholders, + personalisation={"name": "world"}) + with notify_api.test_request_context(), notify_api.test_client() as client: + auth_header = create_authorization_header(service_id=sample_notification.service_id) + + response = client.get( + '/notifications/{}'.format(sample_notification.id), + headers=[auth_header]) + + notification = json.loads(response.get_data(as_text=True))['data']['notification'] + assert response.status_code == 200 + assert notification['body'] == 'Hello world' + assert notification['subject'] == 'world' def test_get_notifications_for_service_returns_merged_template_content(notify_api,