mirror of
https://github.com/GSA/notifications-api.git
synced 2026-03-25 12:40:11 -04:00
This commit changes the code in post notification endpoint to handle a serialised template (ie a `dict`) rather than a database object. This is the first step towards being able to cache the template and not hit the database on every request. There should be no functional changes here, it’s just refactoring. There are some changes to the tests where the signature of functions has changed. Importing of the template schema has to be done at a function level, otherwise Marshmallow gets weird.
91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
from app.models import LETTER_TYPE
|
|
from app.models import Notification
|
|
from app.models import NOTIFICATION_CREATED
|
|
from app.notifications.process_letter_notifications import create_letter_notification
|
|
from app.notifications.process_notifications import create_content_for_notification
|
|
from app.notifications.validators import get_template_dict
|
|
|
|
|
|
def test_create_letter_notification_creates_notification(sample_letter_template, sample_api_key):
|
|
data = {
|
|
'personalisation': {
|
|
'address_line_1': 'The Queen',
|
|
'address_line_2': 'Buckingham Palace',
|
|
'postcode': 'SW1 1AA',
|
|
}
|
|
}
|
|
|
|
template = create_content_for_notification(get_template_dict(
|
|
sample_letter_template.id, sample_letter_template.service_id
|
|
), {})
|
|
|
|
notification = create_letter_notification(
|
|
data,
|
|
template,
|
|
sample_letter_template.service,
|
|
sample_api_key,
|
|
NOTIFICATION_CREATED,
|
|
)
|
|
|
|
assert notification == Notification.query.one()
|
|
assert notification.job is None
|
|
assert notification.status == NOTIFICATION_CREATED
|
|
assert notification.template_id == sample_letter_template.id
|
|
assert notification.template_version == sample_letter_template.version
|
|
assert notification.api_key == sample_api_key
|
|
assert notification.notification_type == LETTER_TYPE
|
|
assert notification.key_type == sample_api_key.key_type
|
|
assert notification.reference is not None
|
|
assert notification.client_reference is None
|
|
assert notification.postage == 'second'
|
|
|
|
|
|
def test_create_letter_notification_sets_reference(sample_letter_template, sample_api_key):
|
|
data = {
|
|
'personalisation': {
|
|
'address_line_1': 'The Queen',
|
|
'address_line_2': 'Buckingham Palace',
|
|
'postcode': 'SW1 1AA',
|
|
},
|
|
'reference': 'foo'
|
|
}
|
|
|
|
template = create_content_for_notification(get_template_dict(
|
|
sample_letter_template.id, sample_letter_template.service_id
|
|
), {})
|
|
|
|
notification = create_letter_notification(
|
|
data,
|
|
template,
|
|
sample_letter_template.service,
|
|
sample_api_key,
|
|
NOTIFICATION_CREATED,
|
|
)
|
|
|
|
assert notification.client_reference == 'foo'
|
|
|
|
|
|
def test_create_letter_notification_sets_billable_units(sample_letter_template, sample_api_key):
|
|
data = {
|
|
'personalisation': {
|
|
'address_line_1': 'The Queen',
|
|
'address_line_2': 'Buckingham Palace',
|
|
'postcode': 'SW1 1AA',
|
|
},
|
|
}
|
|
|
|
template = create_content_for_notification(get_template_dict(
|
|
sample_letter_template.id, sample_letter_template.service_id
|
|
), {})
|
|
|
|
notification = create_letter_notification(
|
|
data,
|
|
template,
|
|
sample_letter_template.service,
|
|
sample_api_key,
|
|
NOTIFICATION_CREATED,
|
|
billable_units=3,
|
|
)
|
|
|
|
assert notification.billable_units == 3
|