diff --git a/app/config.py b/app/config.py index 201517a09..eabd7391f 100644 --- a/app/config.py +++ b/app/config.py @@ -51,6 +51,8 @@ class Config(object): SESSION_COOKIE_SECURE = True SESSION_REFRESH_EACH_REQUEST = True SHOW_STYLEGUIDE = True + # TODO: move to utils + SMS_CHAR_COUNT_LIMIT = 459 TOKEN_MAX_AGE_SECONDS = 3600 WTF_CSRF_ENABLED = True WTF_CSRF_TIME_LIMIT = None diff --git a/app/main/views/send.py b/app/main/views/send.py index 79596f84e..5572b7036 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -638,14 +638,23 @@ def _check_notification(service_id, template_id, exception=None): def get_template_error_dict(exception): + # TODO: Make API return some computer-friendly identifier as well as the end user error messages if 'service is in trial mode' in exception.message: error = 'not-allowed-to-send-to' + elif 'Exceeded send limits' in exception.message: + error = 'too-many-messages' + elif 'Content for template has a character count greater than the limit of' in exception.message: + error = 'message-too-long' else: raise exception return { 'error': error, - 'SMS_CHAR_COUNT_LIMIT': 0 + 'SMS_CHAR_COUNT_LIMIT': current_app.config['SMS_CHAR_COUNT_LIMIT'], + 'current_service': current_service, + + # used to trigger CSV specific err msg content, so not needed for single notification errors. + 'original_file_name': False } @@ -668,6 +677,10 @@ def send_notification(service_id, template_id): personalisation=session['placeholders'] ) except HTTPError as exception: + current_app.logger.info('Service {} could not send notification: "{}"'.format( + current_service['id'], + exception.message + )) return _check_notification(service_id, template_id, exception) session.pop('placeholders') diff --git a/app/templates/partials/check/message-too-long.html b/app/templates/partials/check/message-too-long.html index 9b3e2b8f4..b0192facb 100644 --- a/app/templates/partials/check/message-too-long.html +++ b/app/templates/partials/check/message-too-long.html @@ -1,3 +1,5 @@ +{% from "components/banner.html" import banner_wrapper %} +
{% call banner_wrapper(type='dangerous') %}

diff --git a/app/templates/partials/check/too-many-messages.html b/app/templates/partials/check/too-many-messages.html index c81a0e75b..217ebad33 100644 --- a/app/templates/partials/check/too-many-messages.html +++ b/app/templates/partials/check/too-many-messages.html @@ -1,3 +1,5 @@ +{% from "components/banner.html" import banner_wrapper %} +
{% call banner_wrapper(type='dangerous') %}

diff --git a/app/templates/views/notifications/check.html b/app/templates/views/notifications/check.html index 727b07e21..02656faae 100644 --- a/app/templates/views/notifications/check.html +++ b/app/templates/views/notifications/check.html @@ -14,9 +14,9 @@ %} {% include "partials/check/not-allowed-to-send-to.html" %} {% endwith %} - {% elif error == 'more_rows_than_can_send' %} + {% elif error == 'too-many-messages' %} {% include "partials/check/too-many-messages.html" %} - {% elif error == 'row_errors' %} + {% elif error == 'message-too-long' %} {# the only row_errors we can get when sending one off messages is that the message is too long #} {% include "partials/check/message-too-long.html" %} {% else %} diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index a4218e3e0..dce07f62e 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -1730,24 +1730,48 @@ def test_send_notification_redirects_to_view_page( ) +TRIAL_MODE_MSG = ( + 'Can’t send to this recipient when service is in trial mode – ' + 'see https://www.notifications.service.gov.uk/trial-mode' +) +TOO_LONG_MSG = 'Content for template has a character count greater than the limit of 495' +SERVICE_DAILY_LIMIT_MSG = 'Exceeded send limits (1000) for today' + + +@pytest.mark.parametrize('exception_msg, expected_h1, expected_err_details', [ + ( + TRIAL_MODE_MSG, + 'You can’t send to this phone number', + 'In trial mode you can only send to yourself and members of your team' + ), + ( + TOO_LONG_MSG, + 'Message too long', + 'Text messages can’t be longer than 459 characters. Your message is 678 characters.' + ), + ( + SERVICE_DAILY_LIMIT_MSG, + 'Daily limit reached', + 'You can only send 1000 messages per day in trial mode.' + ), +]) def test_send_notification_shows_error_if_400( client_request, service_one, fake_uuid, mocker, - mock_get_service_template + mock_get_service_template_with_placeholders, + exception_msg, + expected_h1, + expected_err_details ): - trial_mode_msg = ( - 'Can’t send to this recipient when service is in trial mode – ' - 'see https://www.notifications.service.gov.uk/trial-mode' - ) mocker.patch( 'app.notification_api_client.send_notification', - side_effect=HTTPError(response=Mock(status_code=400), message=trial_mode_msg) + side_effect=HTTPError(response=Mock(status_code=400), message=exception_msg) ) with client_request.session_transaction() as session: session['recipient'] = '07700900001' - session['placeholders'] = {'a': 'b'} + session['placeholders'] = {'name': 'a' * 500} page = client_request.post( 'main.send_notification', @@ -1756,4 +1780,6 @@ def test_send_notification_shows_error_if_400( _expected_status=200 ) - assert ' '.join(page.h1.text.split()) == 'You can’t send to this phone number' + assert ' '.join(page.h1.text.split()) == expected_h1 + assert ' '.join(page.h1.parent.p.text.split()) == expected_err_details + assert not page.find('input[type=submit]')