From f581ff44d09d679f75c85737ce09302c900907e8 Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Fri, 29 Apr 2016 15:40:35 +0100 Subject: [PATCH] Only check on csv is the file extension. Update validator logic Update message for the validator. --- app/main/validators.py | 4 ++-- tests/app/main/views/test_send.py | 26 ++++++++++++++++++++++++++ tests/conftest.py | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/main/validators.py b/app/main/validators.py index 846fad424..64125fc1b 100644 --- a/app/main/validators.py +++ b/app/main/validators.py @@ -20,8 +20,8 @@ class CsvFileValidator(object): self.message = message def __call__(self, form, field): - if not form.file.data.mimetype == 'text/csv': - raise ValidationError(self.message) + if not field.data.filename.lower().endswith('.csv'): + raise ValidationError("{} is not a CSV file".format(field.data.filename)) class ValidEmailDomainRegex(object): diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index b5a795305..0fa5bd0c0 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -51,6 +51,32 @@ def test_upload_csvfile_with_errors_shows_check_page_with_errors( assert 'Re-upload your file' in content +def test_upload_csv_invalid_extension(app_, + api_user_active, + mock_login, + mock_get_service, + mock_get_service_template, + mock_s3_upload, + mock_has_permissions, + mock_get_users_by_service, + mock_get_service_statistics, + fake_uuid): + contents = u'phone number,name\n+44 123,test1\n+44 456,test2' + with app_.test_request_context(): + filename = 'invalid.txt' + with app_.test_client() as client: + client.login(api_user_active) + resp = client.post( + url_for('main.send_messages', service_id=fake_uuid, template_id=fake_uuid), + data={'file': (BytesIO(contents.encode('utf-8')), filename)}, + content_type='multipart/form-data', + follow_redirects=True + ) + + assert resp.status_code == 200 + assert "{} is not a CSV file".format(filename) in resp.get_data(as_text=True) + + def test_send_test_sms_message_to_self( app_, mocker, diff --git a/tests/conftest.py b/tests/conftest.py index 3696aad4a..8878bcbbe 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -635,7 +635,7 @@ def mock_get_no_api_keys(mocker): @pytest.fixture(scope='function') -def mock_login(mocker, mock_get_user, mock_update_user): +def mock_login(mocker, mock_get_user, mock_update_user, mock_events): def _verify_code(user_id, code, code_type): return True, ''