diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 6483ac766..8fc2f15f6 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -23,7 +23,8 @@ from app.models import ( LETTER_TYPE, NOTIFICATION_CREATED, Notification, - ScheduledNotification + ScheduledNotification, + CHOOSE_POSTAGE ) from app.dao.notifications_dao import ( dao_create_notification, @@ -31,6 +32,8 @@ from app.dao.notifications_dao import ( dao_created_scheduled_notification ) +from app.dao.templates_dao import dao_get_template_by_id + from app.v2.errors import BadRequestError from app.utils import ( cache_key_for_service_template_counter, @@ -109,7 +112,11 @@ def persist_notification( elif notification_type == EMAIL_TYPE: notification.normalised_to = format_email_address(notification.to) elif notification_type == LETTER_TYPE: - notification.postage = service.postage + 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 # if simulated create a Notification model to return but do not persist the Notification to the dB if not simulated: diff --git a/tests/app/db.py b/tests/app/db.py index a2db88ae6..02a2df85c 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -81,6 +81,7 @@ def create_service( prefix_sms=True, message_limit=1000, organisation_type='central', + postage='second' ): service = Service( 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())), prefix_sms=prefix_sms, organisation_type=organisation_type, + postage=postage ) dao_create_service(service, service.created_by, service_id, service_permissions=service_permissions) diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index 609a863d8..192644bde 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -13,7 +13,9 @@ from app.models import ( Notification, NotificationHistory, ScheduledNotification, - Template + Template, + LETTER_TYPE, + CHOOSE_POSTAGE ) from app.notifications.process_notifications import ( 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 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): 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 +@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', [ ('2016-01-01 23:00:00', '2016-01-01'), ('2016-06-01 22:59:00', '2016-06-01'), diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 91c569a00..429172011 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -17,7 +17,6 @@ from app.models import ( EMAIL_TYPE, LETTER_TYPE, SMS_TYPE, - CHOOSE_POSTAGE, Template, TemplateHistory )