From 832445774b4e51f6cc00d1febee6a5200cf01bd3 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 15 Apr 2020 13:31:14 +0100 Subject: [PATCH] Add postal address row errors Context: - postal addresses can be made from any of the 7 address lines now, and the postcode can be in any one of the 7 - we can put errors across a whole row now, not just on individual cells This commit put errors to do with the postal address as a whole across the whole row now, rather than tying them to any one cell. --- app/main/views/send.py | 2 + app/templates/views/check/row-errors.html | 6 +++ tests/app/main/views/test_send.py | 54 +++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/app/main/views/send.py b/app/main/views/send.py index 58f569815..81f4eea49 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -711,6 +711,8 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_ ), letter_too_long=is_letter_too_long(page_count), letter_max_pages=LETTER_MAX_PAGE_COUNT, + letter_min_address_lines=PostalAddress.MIN_LINES, + letter_max_address_lines=PostalAddress.MAX_LINES, page_count=page_count ) diff --git a/app/templates/views/check/row-errors.html b/app/templates/views/check/row-errors.html index 3ddc43b29..9894a04d4 100644 --- a/app/templates/views/check/row-errors.html +++ b/app/templates/views/check/row-errors.html @@ -80,6 +80,12 @@ No content for this message {% elif item.message_too_long %} Message is too long + {% elif not item.as_postal_address.has_enough_lines %} + Address must be at least {{ letter_min_address_lines }} lines long + {% elif item.as_postal_address.has_too_many_lines %} + Address must be no more than {{ letter_max_address_lines }} lines long + {% elif not item.as_postal_address.postcode %} + Last line of the address must be a real UK postcode {% endif %} {% endcall %} diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index fd69ad8a9..a5aaed2da 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -557,6 +557,60 @@ def test_upload_csv_file_with_very_long_placeholder_shows_check_page_with_errors assert page.select('tbody tr td')[1]['colspan'] == '2' +def test_upload_csv_file_with_bad_postal_address_shows_check_page_with_errors( + logged_in_client, + service_one, + mocker, + mock_get_service_letter_template, + mock_s3_upload, + mock_get_users_by_service, + mock_get_service_statistics, + mock_get_job_doesnt_exist, + mock_get_jobs, + fake_uuid, +): + mocker.patch('app.main.views.send.get_page_count_for_letter', return_value=9) + mocker.patch( + 'app.main.views.send.s3download', + return_value=''' + address line 1, address line 3, address line 6, + Firstname Lastname, 123 Example St., SW1A 1AA + Firstname Lastname, 123 Example St., SW!A !AA + , 123 Example St., SW!A !AA + "1\n2\n3\n4\n5\n6\n7\n8" + ''' + ) + + response = logged_in_client.post( + url_for('main.send_messages', service_id=service_one['id'], template_id=fake_uuid), + data={'file': (BytesIO(''.encode('utf-8')), 'invalid.csv')}, + content_type='multipart/form-data', + follow_redirects=True + ) + + assert response.status_code == 200 + page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') + assert normalize_spaces( + page.select_one('.banner-dangerous').text + ) == ( + 'There’s a problem with invalid.csv ' + 'You need to fix 3 addresses. ' + 'Skip to file contents' + ) + assert [ + normalize_spaces(row.text) for row in page.select('tbody tr') + ] == [ + '3 Last line of the address must be a real UK postcode', + 'Firstname Lastname 123 Example St. SW!A !AA', + + '4 Address must be at least 3 lines long', + '123 Example St. SW!A !AA', + + '5 Address must be no more than 7 lines long', + '1 2 3 4 5 6 7 8', + ] + + @pytest.mark.parametrize('file_contents, expected_error,', [ ( """