From 7a623de1718af5c919407a259175bd815bd2119a Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 15 Dec 2016 16:19:55 +0000 Subject: [PATCH 1/2] Added boby and subject to the get_notifications schema --- app/models.py | 15 ++++++ app/notifications/process_notifications.py | 3 -- app/v2/notifications/notification_schemas.py | 4 +- .../notifications/test_get_notifications.py | 53 ++++++++++++++++++- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/app/models.py b/app/models.py index e5785582fc..801bc62bee 100644 --- a/app/models.py +++ b/app/models.py @@ -608,6 +608,19 @@ def _substitute_status_seq(_statuses): return _substitute_status_seq(status_or_statuses) + @property + def content(self): + from app.utils import get_template_instance + template_object = get_template_instance(self.template.__dict__, self.personalisation) + return str(template_object) + + @property + def subject(self): + from app.utils import get_template_instance + if self.notification_type == EMAIL_TYPE: + template_object = get_template_instance(self.template.__dict__, self.personalisation) + return template_object.subject + def serialize(self): template_dict = { @@ -631,6 +644,8 @@ def serialize(self): "type": self.notification_type, "status": self.status, "template": template_dict, + "body": self.content, + "subject": self.subject, "created_at": self.created_at.strftime(DATETIME_FORMAT), "sent_at": self.sent_at.strftime(DATETIME_FORMAT) if self.sent_at else None, "completed_at": self.completed_at() diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 01a6591ac4..8ba4995d40 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -7,7 +7,6 @@ from notifications_utils.clients import redis from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE -from app.notifications.validators import check_sms_content_char_count from app.v2.errors import BadRequestError, SendNotificationToQueueError from app.utils import get_template_instance @@ -16,8 +15,6 @@ def create_content_for_notification(template, personalisation): template_object = get_template_instance(template.__dict__, personalisation) check_placeholders(template_object) - if template_object.template_type == SMS_TYPE: - check_sms_content_char_count(template_object.content_count) return template_object diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index eadcc2aa7a..3078a2fd87 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -61,6 +61,8 @@ "type": {"enum": ["sms", "letter", "email"]}, "status": {"type": "string"}, "template": template, + "body": {"type": "string"}, + "subject": {"type": ["string", "null"]}, "created_at": {"type": "string"}, "sent_at": {"type": ["string", "null"]}, "completed_at": {"type": ["string", "null"]} @@ -69,7 +71,7 @@ # technically, all keys are required since we always have all of them "id", "reference", "email_address", "phone_number", "line_1", "line_2", "line_3", "line_4", "line_5", "line_6", "postcode", - "type", "status", "template", "created_at", "sent_at", "completed_at" + "type", "status", "template", "body", "created_at", "sent_at", "completed_at" ] } diff --git a/tests/app/v2/notifications/test_get_notifications.py b/tests/app/v2/notifications/test_get_notifications.py index abb69715ea..e947a4b7f0 100644 --- a/tests/app/v2/notifications/test_get_notifications.py +++ b/tests/app/v2/notifications/test_get_notifications.py @@ -1,5 +1,6 @@ -import json + import pytest +from flask import json from app import DATETIME_FORMAT from tests import create_authorization_header @@ -53,6 +54,56 @@ def test_get_notification_by_id_returns_200( 'status': '{}'.format(sample_notification.status), 'template': expected_template_response, 'created_at': sample_notification.created_at.strftime(DATETIME_FORMAT), + 'body': sample_notification.template.content, + "subject": None, + 'sent_at': sample_notification.sent_at, + 'completed_at': sample_notification.completed_at() + } + + assert json_response == expected_response + + +def test_get_notification_by_id_returns_200( + client, notify_db, notify_db_session, sample_email_template_with_placeholders +): + sample_notification = create_sample_notification( + notify_db, notify_db_session, template=sample_email_template_with_placeholders, personalisation={"name": "Bob"} + ) + + auth_header = create_authorization_header(service_id=sample_notification.service_id) + response = client.get( + path='/v2/notifications/{}'.format(sample_notification.id), + headers=[('Content-Type', 'application/json'), auth_header]) + + assert response.status_code == 200 + assert response.headers['Content-type'] == 'application/json' + + json_response = json.loads(response.get_data(as_text=True)) + + expected_template_response = { + 'id': '{}'.format(sample_notification.serialize()['template']['id']), + 'version': sample_notification.serialize()['template']['version'], + 'uri': sample_notification.serialize()['template']['uri'] + } + + expected_response = { + 'id': '{}'.format(sample_notification.id), + 'reference': None, + 'email_address': '{}'.format(sample_notification.to), + 'phone_number': None, + 'line_1': None, + 'line_2': None, + 'line_3': None, + 'line_4': None, + 'line_5': None, + 'line_6': None, + 'postcode': None, + 'type': '{}'.format(sample_notification.notification_type), + 'status': '{}'.format(sample_notification.status), + 'template': expected_template_response, + 'created_at': sample_notification.created_at.strftime(DATETIME_FORMAT), + 'body': "Hello Bob\nThis is an email from GOV.\u200bUK", + "subject": "Bob", 'sent_at': sample_notification.sent_at, 'completed_at': sample_notification.completed_at() } From db6db2e121913e138fa0c17e215907cd9d71ebed Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 15 Dec 2016 16:30:38 +0000 Subject: [PATCH 2/2] Renamed test, it was a duplicate name --- tests/app/v2/notifications/test_get_notifications.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app/v2/notifications/test_get_notifications.py b/tests/app/v2/notifications/test_get_notifications.py index e947a4b7f0..4601954d85 100644 --- a/tests/app/v2/notifications/test_get_notifications.py +++ b/tests/app/v2/notifications/test_get_notifications.py @@ -63,7 +63,7 @@ def test_get_notification_by_id_returns_200( assert json_response == expected_response -def test_get_notification_by_id_returns_200( +def test_get_notification_by_id_with_placeholders_returns_200( client, notify_db, notify_db_session, sample_email_template_with_placeholders ): sample_notification = create_sample_notification(