diff --git a/app/main/views/send.py b/app/main/views/send.py index d0f311b88..79596f84e 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -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//template//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') diff --git a/tests/app/main/views/test_send.py b/tests/app/main/views/test_send.py index 3df6316f2..a4218e3e0 100644 --- a/tests/app/main/views/test_send.py +++ b/tests/app/main/views/test_send.py @@ -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 = ( + '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) + ) + 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 can’t send to this phone number'