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',