From c95123914a6207dc4c179d4406727768b9bec8cf Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 5 Apr 2016 08:55:43 +0100 Subject: [PATCH] Fix re-uploading of CSV files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The problem --- 1. Upload a CSV with problems 2. See the validation errors on the page 3. Try to re-upload the file 4. Get this error: > Method Not Allowed > The method is not allowed for the requested URL. https://www.pivotaltracker.com/story/show/116882241 The cause --- The POST route for the check page (where you see errors in your files) was removed here: https://github.com/alphagov/notifications-admin/commit/f3fd5f6b15b9841111819d9526cf80b20b6ababd#diff-1dd8b3bf53dfd9382c7721051f3d930dR280 The fix --- This commit adds: - a simple route which re-calls the initial ‘I have uploaded a file’ route - a test to make sure that both of these routes are POSTed to --- app/main/views/send.py | 11 +++++++++++ tests/app/main/views/test_send.py | 23 +++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/main/views/send.py b/app/main/views/send.py index c0d0297d3..5985be0c9 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -252,6 +252,17 @@ def check_messages(service_id, template_type, upload_id): ) +@main.route("/services///check/", methods=['POST']) +@login_required +@user_has_permissions('send_texts', 'send_emails', 'send_letters') +def recheck_messages(service_id, template_type, upload_id): + + if not session.get('upload_data'): + return redirect(url_for('main.choose_template', service_id=service_id, template_type=template_type)) + + return send_messages(service_id, session['upload_data'].get('template_id')) + + @main.route("/services//start-job/", methods=['POST']) @login_required @user_has_permissions('send_texts', 'send_emails', 'send_letters') diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 12afc7884..dd0e626d9 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -25,19 +25,26 @@ def test_upload_csvfile_with_errors_shows_check_page_with_errors( with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - response = client.post( + initial_upload = client.post( url_for('main.send_messages', service_id=12345, template_id=54321), data={'file': (BytesIO(contents.encode('utf-8')), 'invalid.csv')}, content_type='multipart/form-data', follow_redirects=True ) - assert response.status_code == 200 - content = response.get_data(as_text=True) - assert 'There was a problem with invalid.csv' in content - assert '+44 123' in content - assert '+44 456' in content - assert 'Not a UK mobile number' in content - assert 'Re-upload your file' in content + reupload = client.post( + url_for('main.check_messages', service_id=12345, template_type='sms', upload_id='abc123'), + data={'file': (BytesIO(contents.encode('utf-8')), 'invalid.csv')}, + content_type='multipart/form-data', + follow_redirects=True + ) + for response in [initial_upload, reupload]: + assert response.status_code == 200 + content = response.get_data(as_text=True) + assert 'There was a problem with invalid.csv' in content + assert '+44 123' in content + assert '+44 456' in content + assert 'Not a UK mobile number' in content + assert 'Re-upload your file' in content def test_send_test_sms_message_to_self(