From 89a8d8912a1ac1ed660ab8e4cbb78102b2665cca Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 10 Aug 2020 09:35:19 +0100 Subject: [PATCH] Set postage and international for letters uploaded with a CSV If the letter is outside of the United Kingdom, set the postage and international flag. --- app/celery/tasks.py | 8 ++++---- app/notifications/validators.py | 6 +++--- tests/app/celery/test_tasks.py | 15 +++++++++++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 6bad3643a..a1bb23391 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -340,9 +340,9 @@ def save_letter( ): notification = encryption.decrypt(encrypted_notification) - recipient = PostalAddress.from_personalisation( + postal_address = PostalAddress.from_personalisation( Columns(notification['personalisation']) - ).normalised + ) service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id(notification['template'], version=notification['template_version']) @@ -354,8 +354,8 @@ def save_letter( saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], - postage=template.postage, - recipient=recipient, + postage=postal_address.postage if postal_address.international else template.postage, + recipient=postal_address.normalised, service=service, personalisation=notification['personalisation'], notification_type=LETTER_TYPE, diff --git a/app/notifications/validators.py b/app/notifications/validators.py index 7a66c27a9..91747d14d 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -242,7 +242,7 @@ def validate_address(service, letter_data): raise ValidationError( message='Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,' ) - if address.postage == 'united-kingdom': - return None # use postage from template - else: + if address.international: return address.postage + else: + return None diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index e76d2814a..f903016ad 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -1035,8 +1035,14 @@ def test_save_letter_saves_letter_to_database( assert notification_db.reply_to_text == contact_block.contact_block -@pytest.mark.parametrize('postage', ['first', 'second']) -def test_save_letter_saves_letter_to_database_with_correct_postage(mocker, notify_db_session, postage): +@pytest.mark.parametrize('last_line_of_address, postage, expected_postage, expected_international', + [('SW1 1AA', 'first', 'first', False), + ('SW1 1AA', 'second', 'second', False), + ('New Zealand', 'second', 'rest-of-world', True), + ('France', 'first', 'europe', True)]) +def test_save_letter_saves_letter_to_database_with_correct_postage( + mocker, notify_db_session, last_line_of_address, postage, expected_postage, expected_international +): service = create_service(service_permissions=[LETTER_TYPE]) template = create_template(service=service, template_type=LETTER_TYPE, postage=postage) letter_job = create_job(template=template) @@ -1045,7 +1051,7 @@ def test_save_letter_saves_letter_to_database_with_correct_postage(mocker, notif notification_json = _notification_json( template=letter_job.template, to='Foo', - personalisation={'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': 'Flob'}, + personalisation={'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': last_line_of_address}, job_id=letter_job.id, row_number=1 ) @@ -1058,7 +1064,8 @@ def test_save_letter_saves_letter_to_database_with_correct_postage(mocker, notif notification_db = Notification.query.one() assert notification_db.id == notification_id - assert notification_db.postage == postage + assert notification_db.postage == expected_postage + assert notification_db.international == expected_international def test_save_letter_saves_letter_to_database_with_formatted_postcode(mocker, notify_db_session):