Check for invalid chars in letter addresses when sending through the API

This uses the new method in notifications-utils to validate for invalid
characters in address blocks.
This commit is contained in:
Katie Smith
2020-07-28 14:43:31 +01:00
parent 4931fc396c
commit c9f663fe76
2 changed files with 41 additions and 19 deletions

View File

@@ -409,6 +409,10 @@ def validate_address(service, letter_data):
raise ValidationError( raise ValidationError(
message='Must be a real UK postcode' message='Must be a real UK postcode'
) )
if address.has_invalid_characters:
raise ValidationError(
message='Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,'
)
def process_precompiled_letter_notifications(*, letter_data, api_key, service, template, reply_to_text): def process_precompiled_letter_notifications(*, letter_data, api_key, service, template, reply_to_text):

View File

@@ -176,31 +176,49 @@ def test_post_letter_notification_stores_country(
) )
@pytest.mark.parametrize('permissions, expected_error', ( @pytest.mark.parametrize('permissions, personalisation, expected_error', (
( (
[LETTER_TYPE], [LETTER_TYPE],
'Must be a real UK postcode', {
),
(
[LETTER_TYPE, INTERNATIONAL_LETTERS],
'Last line of address must be a real UK postcode or another country',
),
))
def test_post_letter_notification_throws_error_for_bad_postcode(
client, notify_db_session, mocker, permissions, expected_error
):
service = create_service(service_permissions=[LETTER_TYPE])
template = create_template(service, template_type="letter", postage="first")
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
data = {
'template_id': str(template.id),
'personalisation': {
'address_line_1': 'Her Royal Highness Queen Elizabeth II', 'address_line_1': 'Her Royal Highness Queen Elizabeth II',
'address_line_2': 'Buckingham Palace', 'address_line_2': 'Buckingham Palace',
'address_line_3': 'London', 'address_line_3': 'London',
'postcode': 'not a real postcode', 'postcode': 'not a real postcode',
'name': 'Lizzie' 'name': 'Lizzie'
} },
'Must be a real UK postcode',
),
(
[LETTER_TYPE],
{
'address_line_1': 'Her Royal Highness Queen Elizabeth II',
'address_line_2': ']Buckingham Palace',
'postcode': 'SW1A 1AA',
'name': 'Lizzie'
},
'Address lines must not start with any of the following characters: @ ( ) = [ ] ” \\ / ,',
),
(
[LETTER_TYPE, INTERNATIONAL_LETTERS],
{
'address_line_1': 'Her Royal Highness Queen Elizabeth II',
'address_line_2': 'Buckingham Palace',
'address_line_3': 'London',
'postcode': 'not a real postcode',
'name': 'Lizzie'
},
'Last line of address must be a real UK postcode or another country',
),
))
def test_post_letter_notification_throws_error_for_bad_address(
client, notify_db_session, mocker, permissions, personalisation, expected_error
):
service = create_service(service_permissions=permissions)
template = create_template(service, template_type="letter", postage="first")
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
data = {
'template_id': str(template.id),
'personalisation': personalisation
} }
error_json = letter_request(client, data, service_id=service.id, _expected_status=400) error_json = letter_request(client, data, service_id=service.id, _expected_status=400)
@@ -208,7 +226,7 @@ def test_post_letter_notification_throws_error_for_bad_postcode(
assert error_json['status_code'] == 400 assert error_json['status_code'] == 400
assert error_json['errors'] == [{ assert error_json['errors'] == [{
'error': 'ValidationError', 'error': 'ValidationError',
'message': 'Must be a real UK postcode' 'message': expected_error
}] }]