From 7032bac178d0ae933c3c951d87f13cabe8f524ca Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 3 Apr 2020 14:28:36 +0100 Subject: [PATCH] Start allowing any three lines in addresses This is part of moving away from `postcode` and towards `address line 7` We think it will be easier for people to map their existing data to our API if we let them fill in any 3 lines, instead of requiring at least 1, 2, and postcode specifically. --- app/schema_validation/definitions.py | 23 ------------------- app/v2/notifications/notification_schemas.py | 4 ++-- app/v2/notifications/post_notifications.py | 18 +++++++++++++-- .../test_post_letter_notifications.py | 19 ++++++--------- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/app/schema_validation/definitions.py b/app/schema_validation/definitions.py index 8ab38d4b8..76c950531 100644 --- a/app/schema_validation/definitions.py +++ b/app/schema_validation/definitions.py @@ -27,29 +27,6 @@ personalisation = { } -letter_personalisation = dict( - personalisation, - properties={ - "address_line_1": { - "type": "string", - "minLength": 1, - "validationMessage": "address_line_1 is required" - }, - "address_line_2": { - "type": "string", - "minLength": 1, - "validationMessage": "address_line_2 is required" - }, - "postcode": { - "type": "string", - "minLength": 1, - "validationMessage": "postcode is required" - }, - }, - required=["address_line_1", "address_line_2", "postcode"], -) - - https_url = { "type": "string", "format": "uri", diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index 733eb8aef..685f58424 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -4,7 +4,7 @@ from app.models import ( NOTIFICATION_STATUS_LETTER_RECEIVED, TEMPLATE_TYPES ) -from app.schema_validation.definitions import (uuid, personalisation, letter_personalisation) +from app.schema_validation.definitions import (uuid, personalisation) template = { @@ -226,7 +226,7 @@ post_letter_request = { "properties": { "reference": {"type": "string"}, "template_id": uuid, - "personalisation": letter_personalisation + "personalisation": personalisation }, "required": ["template_id", "personalisation"], "additionalProperties": False diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index bd86fb2a7..9299b08ab 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -348,8 +348,22 @@ def process_letter_notification(*, letter_data, api_key, template, reply_to_text template=template, reply_to_text=reply_to_text) - if not PostalAddress.from_personalisation(letter_data['personalisation']).postcode: - raise ValidationError(message='Must be a real UK postcode') + address = PostalAddress.from_personalisation(letter_data['personalisation']) + + if not address.has_enough_lines: + raise ValidationError( + message=f'Address must be at least {PostalAddress.MIN_LINES} lines' + ) + + if address.has_too_many_lines: + raise ValidationError( + message=f'Address must be no more than {PostalAddress.MAX_LINES} lines' + ) + + if not address.postcode: + raise ValidationError( + message='Must be a real UK postcode' + ) test_key = api_key.key_type == KEY_TYPE_TEST diff --git a/tests/app/v2/notifications/test_post_letter_notifications.py b/tests/app/v2/notifications/test_post_letter_notifications.py index 14c7fb4b6..509e728a9 100644 --- a/tests/app/v2/notifications/test_post_letter_notifications.py +++ b/tests/app/v2/notifications/test_post_letter_notifications.py @@ -270,9 +270,7 @@ def test_post_letter_notification_returns_400_for_empty_personalisation( assert error_json['status_code'] == 400 assert all([e['error'] == 'ValidationError' for e in error_json['errors']]) assert set([e['message'] for e in error_json['errors']]) == { - 'personalisation address_line_1 is required', - 'personalisation address_line_2 is required', - 'personalisation postcode is required' + 'Address must be at least 3 lines', } @@ -344,15 +342,12 @@ def test_notification_returns_400_if_address_doesnt_have_underscores( error_json = letter_request(client, data, service_id=sample_letter_template.service_id, _expected_status=400) assert error_json['status_code'] == 400 - assert len(error_json['errors']) == 2 - assert { - 'error': 'ValidationError', - 'message': 'personalisation address_line_1 is a required property' - } in error_json['errors'] - assert { - 'error': 'ValidationError', - 'message': 'personalisation address_line_2 is a required property' - } in error_json['errors'] + assert error_json['errors'] == [ + { + 'error': 'ValidationError', + 'message': 'Address must be at least 3 lines' + } + ] def test_returns_a_429_limit_exceeded_if_rate_limit_exceeded(