Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions app/notifications/process_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down
4 changes: 3 additions & 1 deletion app/v2/notifications/notification_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
Expand All @@ -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"
]
}

Expand Down
53 changes: 52 additions & 1 deletion tests/app/v2/notifications/test_get_notifications.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

import pytest
from flask import json

from app import DATETIME_FORMAT
from tests import create_authorization_header
Expand Down Expand Up @@ -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_with_placeholders_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()
}
Expand Down