mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 10:28:55 -04:00
Added boby and subject to the get_notifications schema
This commit is contained in:
@@ -608,6 +608,19 @@ class Notification(db.Model):
|
|||||||
|
|
||||||
return _substitute_status_seq(status_or_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):
|
def serialize(self):
|
||||||
|
|
||||||
template_dict = {
|
template_dict = {
|
||||||
@@ -631,6 +644,8 @@ class Notification(db.Model):
|
|||||||
"type": self.notification_type,
|
"type": self.notification_type,
|
||||||
"status": self.status,
|
"status": self.status,
|
||||||
"template": template_dict,
|
"template": template_dict,
|
||||||
|
"body": self.content,
|
||||||
|
"subject": self.subject,
|
||||||
"created_at": self.created_at.strftime(DATETIME_FORMAT),
|
"created_at": self.created_at.strftime(DATETIME_FORMAT),
|
||||||
"sent_at": self.sent_at.strftime(DATETIME_FORMAT) if self.sent_at else None,
|
"sent_at": self.sent_at.strftime(DATETIME_FORMAT) if self.sent_at else None,
|
||||||
"completed_at": self.completed_at()
|
"completed_at": self.completed_at()
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from app.celery import provider_tasks
|
|||||||
from notifications_utils.clients import redis
|
from notifications_utils.clients import redis
|
||||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
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.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.v2.errors import BadRequestError, SendNotificationToQueueError
|
||||||
from app.utils import get_template_instance
|
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)
|
template_object = get_template_instance(template.__dict__, personalisation)
|
||||||
check_placeholders(template_object)
|
check_placeholders(template_object)
|
||||||
|
|
||||||
if template_object.template_type == SMS_TYPE:
|
|
||||||
check_sms_content_char_count(template_object.content_count)
|
|
||||||
return template_object
|
return template_object
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ get_notification_response = {
|
|||||||
"type": {"enum": ["sms", "letter", "email"]},
|
"type": {"enum": ["sms", "letter", "email"]},
|
||||||
"status": {"type": "string"},
|
"status": {"type": "string"},
|
||||||
"template": template,
|
"template": template,
|
||||||
|
"body": {"type": "string"},
|
||||||
|
"subject": {"type": ["string", "null"]},
|
||||||
"created_at": {"type": "string"},
|
"created_at": {"type": "string"},
|
||||||
"sent_at": {"type": ["string", "null"]},
|
"sent_at": {"type": ["string", "null"]},
|
||||||
"completed_at": {"type": ["string", "null"]}
|
"completed_at": {"type": ["string", "null"]}
|
||||||
@@ -69,7 +71,7 @@ get_notification_response = {
|
|||||||
# technically, all keys are required since we always have all of them
|
# technically, all keys are required since we always have all of them
|
||||||
"id", "reference", "email_address", "phone_number",
|
"id", "reference", "email_address", "phone_number",
|
||||||
"line_1", "line_2", "line_3", "line_4", "line_5", "line_6", "postcode",
|
"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"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import json
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from flask import json
|
||||||
|
|
||||||
from app import DATETIME_FORMAT
|
from app import DATETIME_FORMAT
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
@@ -53,6 +54,56 @@ def test_get_notification_by_id_returns_200(
|
|||||||
'status': '{}'.format(sample_notification.status),
|
'status': '{}'.format(sample_notification.status),
|
||||||
'template': expected_template_response,
|
'template': expected_template_response,
|
||||||
'created_at': sample_notification.created_at.strftime(DATETIME_FORMAT),
|
'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,
|
'sent_at': sample_notification.sent_at,
|
||||||
'completed_at': sample_notification.completed_at()
|
'completed_at': sample_notification.completed_at()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user