mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 05:59:44 -04:00
Merge pull request #3193 from alphagov/validate-against-empty-messages
Validate CSVs against rows with empty messages
This commit is contained in:
@@ -8,41 +8,44 @@ MockRecipients = namedtuple(
|
||||
'RecipientCSV',
|
||||
[
|
||||
'rows_with_bad_recipients',
|
||||
'rows_with_missing_data'
|
||||
'rows_with_missing_data',
|
||||
'rows_with_message_too_long',
|
||||
'rows_with_empty_message'
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"rows_with_bad_recipients,rows_with_missing_data,template_type,expected_errors",
|
||||
"rows_with_bad_recipients, rows_with_missing_data, "
|
||||
"rows_with_message_too_long, rows_with_empty_message, template_type, expected_errors",
|
||||
[
|
||||
(
|
||||
[], [],
|
||||
[], [], [], [],
|
||||
'sms',
|
||||
[]
|
||||
),
|
||||
(
|
||||
{2}, [],
|
||||
{2}, [], [], [],
|
||||
'sms',
|
||||
['fix 1 phone number']
|
||||
),
|
||||
(
|
||||
{2, 4, 6}, [],
|
||||
{2, 4, 6}, [], [], [],
|
||||
'sms',
|
||||
['fix 3 phone numbers']
|
||||
),
|
||||
(
|
||||
{1}, [],
|
||||
{1}, [], [], [],
|
||||
'email',
|
||||
['fix 1 email address']
|
||||
),
|
||||
(
|
||||
{2, 4, 6}, [],
|
||||
{2, 4, 6}, [], [], [],
|
||||
'email',
|
||||
['fix 3 email addresses']
|
||||
),
|
||||
(
|
||||
{2}, {3},
|
||||
{2}, {3}, [], [],
|
||||
'sms',
|
||||
[
|
||||
'fix 1 phone number',
|
||||
@@ -50,21 +53,51 @@ MockRecipients = namedtuple(
|
||||
]
|
||||
),
|
||||
(
|
||||
{2, 4, 6, 8}, {3, 6, 9, 12},
|
||||
{2, 4, 6, 8}, {3, 6, 9, 12}, [], [],
|
||||
'sms',
|
||||
[
|
||||
'fix 4 phone numbers',
|
||||
'enter missing data in 4 rows'
|
||||
]
|
||||
)
|
||||
),
|
||||
(
|
||||
{}, {}, {3}, [],
|
||||
'sms',
|
||||
[
|
||||
'shorten the message in 1 row'
|
||||
]
|
||||
),
|
||||
(
|
||||
{}, {}, {3, 12}, [],
|
||||
'sms',
|
||||
[
|
||||
'shorten the messages in 2 rows'
|
||||
]
|
||||
),
|
||||
(
|
||||
{}, {}, {}, {2},
|
||||
'sms',
|
||||
[
|
||||
'check you have content for the empty message in 1 row'
|
||||
]
|
||||
),
|
||||
(
|
||||
{}, {}, {}, {2, 4, 8},
|
||||
'sms',
|
||||
[
|
||||
'check you have content for the empty messages in 3 rows'
|
||||
]
|
||||
),
|
||||
]
|
||||
)
|
||||
def test_get_errors_for_csv(
|
||||
rows_with_bad_recipients, rows_with_missing_data,
|
||||
rows_with_bad_recipients, rows_with_missing_data, rows_with_message_too_long, rows_with_empty_message,
|
||||
template_type,
|
||||
expected_errors
|
||||
):
|
||||
assert get_errors_for_csv(
|
||||
MockRecipients(rows_with_bad_recipients, rows_with_missing_data),
|
||||
MockRecipients(
|
||||
rows_with_bad_recipients, rows_with_missing_data, rows_with_message_too_long, rows_with_empty_message
|
||||
),
|
||||
template_type
|
||||
) == expected_errors
|
||||
|
||||
@@ -414,12 +414,50 @@ def test_upload_csv_file_with_errors_shows_check_page_with_errors(
|
||||
|
||||
assert response.status_code == 200
|
||||
content = response.get_data(as_text=True)
|
||||
assert 'There is a problem with invalid.csv' in content
|
||||
assert 'There’s a problem with invalid.csv' in content
|
||||
assert '+447700900986' in content
|
||||
assert 'Missing' in content
|
||||
assert 'Upload your file again' in content
|
||||
|
||||
|
||||
def test_upload_csv_file_with_empty_message_shows_check_page_with_errors(
|
||||
logged_in_client,
|
||||
service_one,
|
||||
mocker,
|
||||
mock_get_empty_service_template_with_optional_placeholder,
|
||||
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.s3download',
|
||||
return_value="""
|
||||
phone number, show_placeholder
|
||||
+447700900986, yes
|
||||
+447700900986, no
|
||||
"""
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
with logged_in_client.session_transaction() as session:
|
||||
assert 'file_uploads' not in session
|
||||
|
||||
assert response.status_code == 200
|
||||
content = response.get_data(as_text=True)
|
||||
assert 'There’s a problem with invalid.csv' in content
|
||||
assert 'check you have content for the empty message in 1 row' in content
|
||||
|
||||
|
||||
@pytest.mark.parametrize('file_contents, expected_error,', [
|
||||
(
|
||||
"""
|
||||
@@ -490,8 +528,8 @@ def test_upload_csv_file_with_errors_shows_check_page_with_errors(
|
||||
+447700900986, example
|
||||
""",
|
||||
(
|
||||
'There is a problem with invalid.csv '
|
||||
'You need to enter missing data in 1 row '
|
||||
'There’s a problem with invalid.csv '
|
||||
'You need to enter missing data in 1 row. '
|
||||
'Skip to file contents'
|
||||
)
|
||||
),
|
||||
@@ -503,8 +541,8 @@ def test_upload_csv_file_with_errors_shows_check_page_with_errors(
|
||||
+447700900986, example
|
||||
""",
|
||||
(
|
||||
'There is a problem with invalid.csv '
|
||||
'You need to enter missing data in 1 row '
|
||||
'There’s a problem with invalid.csv '
|
||||
'You need to enter missing data in 1 row. '
|
||||
'Skip to file contents'
|
||||
)
|
||||
),
|
||||
@@ -2855,8 +2893,8 @@ def test_check_messages_shows_data_errors_before_trial_mode_errors_for_letters(
|
||||
)
|
||||
|
||||
assert normalize_spaces(page.select_one('.banner-dangerous').text) == (
|
||||
'There is a problem with example.xlsx '
|
||||
'You need to enter missing data in 2 rows '
|
||||
'There’s a problem with example.xlsx '
|
||||
'You need to enter missing data in 2 rows. '
|
||||
'Skip to file contents'
|
||||
)
|
||||
assert not page.select('.table-field-index a')
|
||||
|
||||
@@ -850,6 +850,20 @@ def mock_get_service_template_with_placeholders(mocker):
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_get_empty_service_template_with_optional_placeholder(mocker):
|
||||
def _get(service_id, template_id, version=None):
|
||||
template = template_json(
|
||||
service_id, template_id, name="Optional content", content="((show_placeholder??Some content))"
|
||||
)
|
||||
return {'data': template}
|
||||
|
||||
return mocker.patch(
|
||||
'app.service_api_client.get_service_template',
|
||||
side_effect=_get
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_get_service_template_with_multiple_placeholders(mocker):
|
||||
def _get(service_id, template_id, version=None):
|
||||
|
||||
Reference in New Issue
Block a user