From c646c16067dde34d491ed5813353b029dd6c00a7 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 2 Apr 2020 17:32:26 +0100 Subject: [PATCH] Add postcode validation check for one-off letters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We’re doing this everywhere else now, so this completes the story. It uses the same regex as elsewhere and the error messaging is consistent (but not uniform) with the other places. --- app/main/forms.py | 5 +++++ tests/app/main/views/test_send.py | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index eca50ab26..46592daf9 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -769,6 +769,11 @@ class LetterAddressForm(StripWhitespaceForm): f'Address must be no more than {PostalAddress.MAX_LINES} lines long' ) + if not address.postcode: + raise ValidationError( + f'Last line of the address must be a real UK postcode' + ) + class EmailTemplateForm(BaseTemplateForm): subject = TextAreaField( diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 8547bec48..a43a06420 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -2204,38 +2204,38 @@ def test_send_one_off_letter_address_shows_form( @pytest.mark.parametrize(['form_data', 'expected_placeholders'], [ # minimal - ('\n'.join(['a', 'b', 'c']), { + ('\n'.join(['a', 'b', 'sw1a1aa']), { 'address_line_1': 'a', 'address_line_2': 'b', 'address_line_3': '', 'address_line_4': '', 'address_line_5': '', 'address_line_6': '', - 'address_line_7': 'c', - 'postcode': 'c', + 'address_line_7': 'SW1A 1AA', + 'postcode': 'SW1A 1AA', }), # maximal - ('\n'.join(['a', 'b', 'c', 'd', 'e', 'f', 'g']), { + ('\n'.join(['a', 'b', 'c', 'd', 'e', 'f', 'sw1a1aa']), { 'address_line_1': 'a', 'address_line_2': 'b', 'address_line_3': 'c', 'address_line_4': 'd', 'address_line_5': 'e', 'address_line_6': 'f', - 'address_line_7': 'g', - 'postcode': 'g', + 'address_line_7': 'SW1A 1AA', + 'postcode': 'SW1A 1AA', }), # it ignores empty lines and strips whitespace from each line. # It also strips extra whitespace from the middle of lines. - ('\n a\ta \n\n\n \n\n\n\nb b \r\nc', { + ('\n a\ta \n\n\n \n\n\n\nb b \r\n sw1a1aa \n\n', { 'address_line_1': 'a\ta', 'address_line_2': 'b b', 'address_line_3': '', 'address_line_4': '', 'address_line_5': '', 'address_line_6': '', - 'address_line_7': 'c', - 'postcode': 'c', + 'address_line_7': 'SW1A 1AA', + 'postcode': 'SW1A 1AA', }), ]) def test_send_one_off_letter_address_populates_address_fields_in_session( @@ -2271,6 +2271,7 @@ def test_send_one_off_letter_address_populates_address_fields_in_session( ('', 'Cannot be empty'), ('a\n\n\n\nb', 'Address must be at least 3 lines long'), ('\n'.join(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']), 'Address must be no more than 7 lines long'), + ('\n'.join(['a', 'b', 'c', 'd', 'e', 'f', 'g']), 'Last line of the address must be a real UK postcode'), ]) def test_send_one_off_letter_address_rejects_bad_addresses( client_request, @@ -2309,7 +2310,7 @@ def test_send_one_off_letter_address_goes_to_next_placeholder(client_request, mo 'main.send_one_off_letter_address', service_id=SERVICE_ONE_ID, template_id=template_data['id'], - _data={'address': 'a\nb\nc'}, + _data={'address': 'a\nb\nSW1A 1AA'}, # step 0-6 represent address line 1-6 and postcode. step 7 is the first non address placeholder _expected_redirect=url_for( 'main.send_one_off_step',