mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 17:01:35 -05:00
Choose postage when persisting a notification
This commit is contained in:
@@ -23,7 +23,8 @@ from app.models import (
|
|||||||
LETTER_TYPE,
|
LETTER_TYPE,
|
||||||
NOTIFICATION_CREATED,
|
NOTIFICATION_CREATED,
|
||||||
Notification,
|
Notification,
|
||||||
ScheduledNotification
|
ScheduledNotification,
|
||||||
|
CHOOSE_POSTAGE
|
||||||
)
|
)
|
||||||
from app.dao.notifications_dao import (
|
from app.dao.notifications_dao import (
|
||||||
dao_create_notification,
|
dao_create_notification,
|
||||||
@@ -31,6 +32,8 @@ from app.dao.notifications_dao import (
|
|||||||
dao_created_scheduled_notification
|
dao_created_scheduled_notification
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from app.dao.templates_dao import dao_get_template_by_id
|
||||||
|
|
||||||
from app.v2.errors import BadRequestError
|
from app.v2.errors import BadRequestError
|
||||||
from app.utils import (
|
from app.utils import (
|
||||||
cache_key_for_service_template_counter,
|
cache_key_for_service_template_counter,
|
||||||
@@ -109,6 +112,10 @@ def persist_notification(
|
|||||||
elif notification_type == EMAIL_TYPE:
|
elif notification_type == EMAIL_TYPE:
|
||||||
notification.normalised_to = format_email_address(notification.to)
|
notification.normalised_to = format_email_address(notification.to)
|
||||||
elif notification_type == LETTER_TYPE:
|
elif notification_type == LETTER_TYPE:
|
||||||
|
template = dao_get_template_by_id(template_id, template_version)
|
||||||
|
if service.has_permission(CHOOSE_POSTAGE) and template.postage:
|
||||||
|
notification.postage = template.postage
|
||||||
|
else:
|
||||||
notification.postage = service.postage
|
notification.postage = service.postage
|
||||||
|
|
||||||
# if simulated create a Notification model to return but do not persist the Notification to the dB
|
# if simulated create a Notification model to return but do not persist the Notification to the dB
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ def create_service(
|
|||||||
prefix_sms=True,
|
prefix_sms=True,
|
||||||
message_limit=1000,
|
message_limit=1000,
|
||||||
organisation_type='central',
|
organisation_type='central',
|
||||||
|
postage='second'
|
||||||
):
|
):
|
||||||
service = Service(
|
service = Service(
|
||||||
name=service_name,
|
name=service_name,
|
||||||
@@ -90,6 +91,7 @@ def create_service(
|
|||||||
created_by=user or create_user(email='{}@digital.cabinet-office.gov.uk'.format(uuid.uuid4())),
|
created_by=user or create_user(email='{}@digital.cabinet-office.gov.uk'.format(uuid.uuid4())),
|
||||||
prefix_sms=prefix_sms,
|
prefix_sms=prefix_sms,
|
||||||
organisation_type=organisation_type,
|
organisation_type=organisation_type,
|
||||||
|
postage=postage
|
||||||
)
|
)
|
||||||
|
|
||||||
dao_create_service(service, service.created_by, service_id, service_permissions=service_permissions)
|
dao_create_service(service, service.created_by, service_id, service_permissions=service_permissions)
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ from app.models import (
|
|||||||
Notification,
|
Notification,
|
||||||
NotificationHistory,
|
NotificationHistory,
|
||||||
ScheduledNotification,
|
ScheduledNotification,
|
||||||
Template
|
Template,
|
||||||
|
LETTER_TYPE,
|
||||||
|
CHOOSE_POSTAGE
|
||||||
)
|
)
|
||||||
from app.notifications.process_notifications import (
|
from app.notifications.process_notifications import (
|
||||||
create_content_for_notification,
|
create_content_for_notification,
|
||||||
@@ -27,6 +29,8 @@ from app.utils import cache_key_for_service_template_counter
|
|||||||
from app.v2.errors import BadRequestError
|
from app.v2.errors import BadRequestError
|
||||||
from tests.app.conftest import sample_api_key as create_api_key
|
from tests.app.conftest import sample_api_key as create_api_key
|
||||||
|
|
||||||
|
from tests.app.db import create_service, create_template
|
||||||
|
|
||||||
|
|
||||||
def test_create_content_for_notification_passes(sample_email_template):
|
def test_create_content_for_notification_passes(sample_email_template):
|
||||||
template = Template.query.get(sample_email_template.id)
|
template = Template.query.get(sample_email_template.id)
|
||||||
@@ -477,6 +481,41 @@ def test_persist_email_notification_stores_normalised_email(
|
|||||||
assert persisted_notification.normalised_to == expected_recipient_normalised
|
assert persisted_notification.normalised_to == expected_recipient_normalised
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"service_permissions, template_postage, expected_postage",
|
||||||
|
[
|
||||||
|
([LETTER_TYPE], "first", "second"),
|
||||||
|
([LETTER_TYPE, CHOOSE_POSTAGE], "first", "first"),
|
||||||
|
([LETTER_TYPE, CHOOSE_POSTAGE], None, "second"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_persist_letter_notification_finds_correct_postage(
|
||||||
|
mocker,
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service_permissions,
|
||||||
|
template_postage,
|
||||||
|
expected_postage
|
||||||
|
):
|
||||||
|
service = create_service(service_permissions=service_permissions, postage="second")
|
||||||
|
api_key = create_api_key(notify_db, notify_db_session, service=service)
|
||||||
|
template = create_template(service, template_type=LETTER_TYPE, postage=template_postage)
|
||||||
|
mocker.patch('app.dao.templates_dao.dao_get_template_by_id', return_value=template)
|
||||||
|
persist_notification(
|
||||||
|
template_id=template.id,
|
||||||
|
template_version=template.version,
|
||||||
|
recipient="Jane Doe, 10 Downing Street, London",
|
||||||
|
service=service,
|
||||||
|
personalisation=None,
|
||||||
|
notification_type=LETTER_TYPE,
|
||||||
|
api_key_id=api_key.id,
|
||||||
|
key_type=api_key.key_type,
|
||||||
|
)
|
||||||
|
persisted_notification = Notification.query.all()[0]
|
||||||
|
|
||||||
|
assert persisted_notification.postage == expected_postage
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('utc_time, day_in_key', [
|
@pytest.mark.parametrize('utc_time, day_in_key', [
|
||||||
('2016-01-01 23:00:00', '2016-01-01'),
|
('2016-01-01 23:00:00', '2016-01-01'),
|
||||||
('2016-06-01 22:59:00', '2016-06-01'),
|
('2016-06-01 22:59:00', '2016-06-01'),
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ from app.models import (
|
|||||||
EMAIL_TYPE,
|
EMAIL_TYPE,
|
||||||
LETTER_TYPE,
|
LETTER_TYPE,
|
||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
CHOOSE_POSTAGE,
|
|
||||||
Template,
|
Template,
|
||||||
TemplateHistory
|
TemplateHistory
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user