display errors on the end page.

The following errors may happen:
* Number outside of service if service in trial mode
* Message too long for sms
* Service over daily limit

We need to handle these. They only return on send, rather than in a
separate validation step (for now).
This commit is contained in:
Leo Hemsted
2017-06-29 15:31:44 +01:00
parent f1ecee8469
commit 70914bfe8a
2 changed files with 62 additions and 10 deletions

View File

@@ -19,6 +19,7 @@ from flask import (
from flask_login import login_required, current_user
from notifications_python_client.errors import HTTPError
from notifications_utils.columns import Columns
from notifications_utils.recipients import (
RecipientCSV,
@@ -280,7 +281,7 @@ def send_test_step(service_id, template_id, step_index):
back_link = get_back_link(service_id, template_id, step_index)
template.values = get_receipient_and_placeholders_from_session(template.template_type)
template.values = get_recipient_and_placeholders_from_session(template.template_type)
template.values[current_placeholder] = None
if (
@@ -530,7 +531,7 @@ def get_normalised_placeholders_from_session():
}
def get_receipient_and_placeholders_from_session(template_type):
def get_recipient_and_placeholders_from_session(template_type):
placeholders = get_normalised_placeholders_from_session()
if template_type == 'sms':
@@ -601,6 +602,10 @@ def get_back_link(service_id, template_id, step_index):
@login_required
@user_has_permissions('manage_templates')
def check_notification(service_id, template_id):
return _check_notification(service_id, template_id)
def _check_notification(service_id, template_id, exception=None):
db_template = service_api_client.get_service_template(service_id, template_id)['data']
template = get_template(
@@ -621,16 +626,29 @@ def check_notification(service_id, template_id):
):
return redirect(back_link)
template.values = get_receipient_and_placeholders_from_session(template.template_type)
template.values = get_recipient_and_placeholders_from_session(template.template_type)
return render_template(
'views/notifications/check.html',
template=template,
back_link=back_link,
help=get_help_argument(),
**(get_template_error_dict(exception) if exception else {})
)
def get_template_error_dict(exception):
if 'service is in trial mode' in exception.message:
error = 'not-allowed-to-send-to'
else:
raise exception
return {
'error': error,
'SMS_CHAR_COUNT_LIMIT': 0
}
@main.route("/services/<service_id>/template/<template_id>/notification/check", methods=['POST'])
@login_required
@user_has_permissions('manage_templates')
@@ -642,12 +660,15 @@ def send_notification(service_id, template_id):
template_id=template_id,
))
noti = notification_api_client.send_notification(
service_id,
template_id=template_id,
recipient=session['recipient'],
personalisation=session['placeholders']
)
try:
noti = notification_api_client.send_notification(
service_id,
template_id=template_id,
recipient=session['recipient'],
personalisation=session['placeholders']
)
except HTTPError as exception:
return _check_notification(service_id, template_id, exception)
session.pop('placeholders')
session.pop('recipient')

View File

@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import uuid
from unittest.mock import Mock
from io import BytesIO
from os import path
from glob import glob
@@ -9,6 +10,7 @@ from functools import partial
import pytest
from bs4 import BeautifulSoup
from flask import url_for
from notifications_python_client.errors import HTTPError
from notifications_utils.template import LetterPreviewTemplate, LetterImageTemplate
from notifications_utils.recipients import RecipientCSV
@@ -1726,3 +1728,32 @@ def test_send_notification_redirects_to_view_page(
notification_id=fake_uuid,
_external=True
)
def test_send_notification_shows_error_if_400(
client_request,
service_one,
fake_uuid,
mocker,
mock_get_service_template
):
trial_mode_msg = (
'Cant 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)
)
with client_request.session_transaction() as session:
session['recipient'] = '07700900001'
session['placeholders'] = {'a': 'b'}
page = client_request.post(
'main.send_notification',
service_id=service_one['id'],
template_id=fake_uuid,
_expected_status=200
)
assert ' '.join(page.h1.text.split()) == 'You cant send to this phone number'