From 584533eb11d597c9fcd1aa750df5a4115387c00c Mon Sep 17 00:00:00 2001
From: Adam Shimali Line {{rejected.line_number}}: {{rejected.phone }}
+ {% endfor %}
+ Send text messages
- Send text messages
+ {% if upload_result.rejects %}
+ The following numbers are invalid
+ {% for rejected in upload_result.rejects %}
+ Check and confirm
+ {% else %}
-
+ {% if upload_result.valid | count > 6 %}
+ First three message in file
+ {% for recipient in upload_result.valid[:3] %}
+ {{ sms_message(message_template|replace_placeholders(
+ recipient),
+ '{}'.format(recipient['phone'])
+ )}}
+ {% endfor %}
+ Last three messages in file
+ {% for recipient in upload_result.valid[-3:] %}
+ {{ sms_message(message_template|replace_placeholders(
+ recipient),
+ '{}'.format(recipient['phone'])
+ )}}
+ {% endfor %}
+ {% else %}
+ All messages in file
+ {% for recipient in upload_result.valid %}
+ {{ sms_message(message_template|replace_placeholders(
+ recipient),
+ '{}'.format(recipient['phone'])
+ )}}
+ {% endfor %}
+ {% endif %}
+ {{ page_footer(
+ button_text = "Send {} text messages".format(upload_result.valid|count),
+ back_link = url_for(".sendsms")
+ )}}
+
+
+ {% endif %}
{% endblock %}
diff --git a/app/templates/views/send-sms.html b/app/templates/views/send-sms.html
index c413b12db..ad2843156 100644
--- a/app/templates/views/send-sms.html
+++ b/app/templates/views/send-sms.html
@@ -1,6 +1,7 @@
{% extends "withnav_template.html" %}
{% from "components/sms-message.html" import sms_message %}
{% from "components/page-footer.html" import page_footer %}
+{% from "components/form-field.html" import render_field %}
{% block page_title %}
GOV.UK Notify | Send text messages
@@ -37,7 +38,7 @@
You can also download an example CSV.
- + {{render_field(form.file)}}
{{ page_footer("Continue") }} diff --git a/config.py b/config.py index 0f03f3792..6f36584ff 100644 --- a/config.py +++ b/config.py @@ -31,6 +31,8 @@ class Config(object): DANGEROUS_SALT = 'itsdangeroussalt' TOKEN_MAX_AGE_SECONDS = 120000 + MAX_CONTENT_LENGTH = 10 * 1024 * 1024 # 10mb + class Development(Config): DEBUG = True diff --git a/tests/app/main/views/test_sms.py b/tests/app/main/views/test_sms.py index 092d1c03d..ee3eb7f57 100644 --- a/tests/app/main/views/test_sms.py +++ b/tests/app/main/views/test_sms.py @@ -1,27 +1,108 @@ -def test_should_return_sms_template_picker(notifications_admin): - response = notifications_admin.test_client().get('/sms/send') - - assert response.status_code == 200 - assert 'Choose text message template' in response.get_data(as_text=True) +from io import BytesIO +from tests.app.main import create_test_user -def test_should_redirect_to_sms_check_page(notifications_admin): - response = notifications_admin.test_client().post('/sms/send') +def test_upload_empty_csvfile_returns_to_upload_page(notifications_admin, notifications_admin_db, notify_db_session): + with notifications_admin.test_request_context(): + with notifications_admin.test_client() as client: + user = create_test_user('active') + client.login(user) - assert response.status_code == 302 - assert response.location == 'http://localhost/sms/check' + upload_data = {'file': (BytesIO(''.encode('utf-8')), 'emtpy.csv')} + response = client.post('/sms/send', data=upload_data, + follow_redirects=True) + + assert response.status_code == 200 + content = response.get_data(as_text=True) + assert 'The file emtpy.csv contained no data' in content -def test_should_return_check_sms_page(notifications_admin): - response = notifications_admin.test_client().get('/sms/check') +def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors( + notifications_admin, notifications_admin_db, notify_db_session): - assert response.status_code == 200 - assert 'Check and confirm' in response.get_data(as_text=True) - assert 'Send 10 text messages' in response.get_data(as_text=True) + with notifications_admin.test_request_context(): + with notifications_admin.test_client() as client: + user = create_test_user('active') + client.login(user) + + file_contents = 'phone\n+44 123\n+44 456'.encode('utf-8') + upload_data = {'file': (BytesIO(file_contents), 'invalid.csv')} + response = client.post('/sms/send', data=upload_data, + follow_redirects=True) + assert response.status_code == 200 + content = response.get_data(as_text=True) + assert 'There was a problem with some of the numbers' in content + assert 'The following numbers are invalid' in content + assert '+44 123' in content + assert '+44 456' in content + assert 'Go back and resolve errors' in content -def test_should_redirect_to_job(notifications_admin): - response = notifications_admin.test_client().post('/sms/check') +def test_upload_csvfile_with_valid_phone_shows_first3_and_last3_numbers( + notifications_admin, notifications_admin_db, notify_db_session): - assert response.status_code == 302 - assert response.location == 'http://localhost/jobs/job' + with notifications_admin.test_request_context(): + with notifications_admin.test_client() as client: + user = create_test_user('active') + client.login(user) + file_contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986\n+44 7700 900987\n+44 7700 900988\n+44 7700 900989'.encode('utf-8') # noqa + + upload_data = {'file': (BytesIO(file_contents), 'valid.csv')} + response = client.post('/sms/send', data=upload_data, + follow_redirects=True) + + content = response.get_data(as_text=True) + + assert response.status_code == 200 + assert 'Check and confirm' in content + assert 'First three message in file' in content + assert 'Last three messages in file' in content + assert '+44 7700 900981' in content + assert '+44 7700 900982' in content + assert '+44 7700 900983' in content + assert '+44 7700 900984' not in content + assert '+44 7700 900985' not in content + assert '+44 7700 900986' not in content + assert '+44 7700 900987' in content + assert '+44 7700 900988' in content + assert '+44 7700 900989' in content + + +def test_upload_csvfile_with_valid_phone_shows_all_if_6_or_less_numbers( + notifications_admin, notifications_admin_db, notify_db_session): + + with notifications_admin.test_request_context(): + with notifications_admin.test_client() as client: + user = create_test_user('active') + client.login(user) + + file_contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986'.encode('utf-8') # noqa + + upload_data = {'file': (BytesIO(file_contents), 'valid.csv')} + response = client.post('/sms/send', data=upload_data, + follow_redirects=True) + + content = response.get_data(as_text=True) + + assert response.status_code == 200 + assert 'Check and confirm' in content + assert 'All messages in file' in content + assert '+44 7700 900981' in content + assert '+44 7700 900982' in content + assert '+44 7700 900983' in content + assert '+44 7700 900984' in content + assert '+44 7700 900985' in content + assert '+44 7700 900986' in content + + +def test_should_redirect_to_job(notifications_admin, notifications_admin_db, + notify_db_session): + with notifications_admin.test_request_context(): + with notifications_admin.test_client() as client: + user = create_test_user('active') + client.login(user) + + response = client.post('/sms/check') + + assert response.status_code == 302 + assert response.location == 'http://localhost/jobs/job'