diff --git a/app/main/forms.py b/app/main/forms.py index f6f2716f3..66c506976 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -853,6 +853,11 @@ class LetterAddressForm(StripWhitespaceForm): f'Last line of the address must be a real UK postcode' ) + if address.has_invalid_characters: + raise ValidationError( + 'Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,' + ) + class EmailTemplateForm(BaseTemplateForm): subject = TextAreaField( diff --git a/app/templates/views/check/row-errors.html b/app/templates/views/check/row-errors.html index 839edddb7..4ffa028e4 100644 --- a/app/templates/views/check/row-errors.html +++ b/app/templates/views/check/row-errors.html @@ -90,6 +90,8 @@ {% else %} Last line of the address must be a real UK postcode {% endif %} + {% elif item.as_postal_address.has_invalid_characters %} + Address lines must not start with any of the following characters: @ ( ) = [ ] ” \ / , {% endif %} {% endcall %} diff --git a/app/utils.py b/app/utils.py index ebd1a89e9..a155395ab 100644 --- a/app/utils.py +++ b/app/utils.py @@ -733,7 +733,17 @@ LETTER_VALIDATION_MESSAGES = { f'Validation failed because the address must be no more ' f'than {PostalAddress.MAX_LINES} lines long.' ), - } + }, + 'invalid-char-in-address': { + 'title': 'There’s a problem with the address for this letter', + 'detail': ( + "Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ," + ), + 'summary': ( + "Validation failed because address lines must not start with any of the " + "following characters: @ ( ) = [ ] ” \\ / ," + ), + }, } diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 3d9975846..1733b05d5 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -614,6 +614,7 @@ def test_upload_csv_file_with_bad_postal_address_shows_check_page_with_errors( Firstname Lastname, 123 Example St., France , 123 Example St., SW!A !AA "1\n2\n3\n4\n5\n6\n7\n8" + =Firstname Lastname, 123 Example St., SW1A 1AA ''' ) @@ -630,7 +631,7 @@ def test_upload_csv_file_with_bad_postal_address_shows_check_page_with_errors( page.select_one('.banner-dangerous').text ) == ( 'There’s a problem with invalid.csv ' - 'You need to fix 4 addresses. ' + 'You need to fix 5 addresses. ' 'Skip to file contents' ) assert [ @@ -647,6 +648,9 @@ def test_upload_csv_file_with_bad_postal_address_shows_check_page_with_errors( '6 Address must be no more than 7 lines long', '1 2 3 4 5 6 7 8', + + '7 Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,', + '=Firstname Lastname 123 Example St. SW1A 1AA', ] @@ -2538,6 +2542,11 @@ def test_send_one_off_letter_address_populates_address_fields_in_session( ['international_letters'], 'Last line of the address must be a UK postcode or another country', ), + ( + 'a\n(b\nSW1A 1AA', + [], + 'Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,', + ), ]) def test_send_one_off_letter_address_rejects_bad_addresses( client_request, diff --git a/tests/app/test_utils.py b/tests/app/test_utils.py index bedb4eb97..a53723f00 100644 --- a/tests/app/test_utils.py +++ b/tests/app/test_utils.py @@ -545,6 +545,18 @@ def test_get_letter_validation_error_for_unknown_error(): 'Validation failed because the address must be no more than 7 lines long.' ), ), + ( + 'invalid-char-in-address', + None, + 'There’s a problem with the address for this letter', + ( + 'Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,' + ), + ( + 'Validation failed because address lines must not start with any of the following ' + 'characters: @ ( ) = [ ] ” \\ / ,' + ), + ), ]) def test_get_letter_validation_error_for_known_errors( client_request,