From c0da7a27ed7a43b7312cff2feeca4fe5b8d5bf74 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Wed, 1 Dec 2021 16:59:30 +0000 Subject: [PATCH] Fix not showing errors for invalid contact uploads This code should behave the same way as other CSV uploads [1], but we had to write it in a hurry [2] and the way we show an error with the upload field was based off that for PDF uploads, where we show custom button text instead of an error [3]. This fixes the inconsistency, so that we see the same errors for CSV uploads here as in other parts of the app. [1]: https://github.com/alphagov/notifications-admin/blob/6b52735dac02e2284de667676a672d01047995b8/app/templates/views/send.html#L25 [2]: https://github.com/alphagov/notifications-admin/commit/1c02476ee7b3d66cf4db4f9b8616918084caeeda#diff-aedd12af78c9737f1c3344d2afbb9c00878eccbcc754b2b3d9e6864c2ad2f7c3R32 [3]: https://github.com/alphagov/notifications-admin/commit/3b3f74bbf08e7abda58886f3f05537e1192a2a4f --- .../views/uploads/contact-list/upload.html | 3 +-- .../views/uploads/test_upload_contact_list.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/templates/views/uploads/contact-list/upload.html b/app/templates/views/uploads/contact-list/upload.html index 39e6bac5e..4cf7064f2 100644 --- a/app/templates/views/uploads/contact-list/upload.html +++ b/app/templates/views/uploads/contact-list/upload.html @@ -40,8 +40,7 @@ {{ file_upload( form.file, allowed_file_extensions=allowed_file_extensions, - button_text='Upload your file again' if error else 'Choose file', - show_errors=False + button_text='Choose file', )}} diff --git a/tests/app/main/views/uploads/test_upload_contact_list.py b/tests/app/main/views/uploads/test_upload_contact_list.py index 40a0ee6bb..a105ff7bf 100644 --- a/tests/app/main/views/uploads/test_upload_contact_list.py +++ b/tests/app/main/views/uploads/test_upload_contact_list.py @@ -276,6 +276,22 @@ def test_upload_csv_file_shows_error_banner_for_too_many_rows( ) +def test_upload_csv_shows_error_with_invalid_extension( + client_request, +): + + page = client_request.post( + 'main.upload_contact_list', + service_id=SERVICE_ONE_ID, + _data={'file': (BytesIO(''.encode('utf-8')), 'invalid.txt')}, + _follow_redirects=True, + ) + + assert normalize_spaces(page.select_one('.file-upload-label .error-message').text) == ( + "invalid.txt is not a spreadsheet that Notify can read" + ) + + def test_upload_csv_file_sanitises_and_truncates_file_name_in_metadata( client_request, mocker,