From f8bca5765dcb4e164f5f4947048024a91005ba5a Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 22 Apr 2021 14:42:54 +0100 Subject: [PATCH] Validate length of broadcast templates on creation This is a belt-and-braces check because the admin app already checks this. But since we do it for SMS already it makes sense to replicate it for broadcast templates. --- app/template/rest.py | 24 ++++++++++++++++++------ tests/app/template/test_rest.py | 16 +++++++++++++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/template/rest.py b/app/template/rest.py index 0ff792660..0933ac919 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -5,7 +5,10 @@ import botocore from flask import Blueprint, current_app, jsonify, request from notifications_utils import SMS_CHAR_COUNT_LIMIT from notifications_utils.pdf import extract_page_from_pdf -from notifications_utils.template import SMSMessageTemplate +from notifications_utils.template import ( + BroadcastMessageTemplate, + SMSMessageTemplate, +) from PyPDF2.utils import PdfReadError from requests import post as requests_post from sqlalchemy.orm.exc import NoResultFound @@ -28,7 +31,13 @@ from app.dao.templates_dao import ( ) from app.errors import InvalidRequest, register_errors from app.letters.utils import get_letter_pdf_and_metadata -from app.models import LETTER_TYPE, SECOND_CLASS, SMS_TYPE, Template +from app.models import ( + BROADCAST_TYPE, + LETTER_TYPE, + SECOND_CLASS, + SMS_TYPE, + Template, +) from app.notifications.validators import check_reply_to, service_has_permission from app.schema_validation import validate from app.schemas import ( @@ -48,10 +57,13 @@ register_errors(template_blueprint) def _content_count_greater_than_limit(content, template_type): - if template_type != SMS_TYPE: - return False - template = SMSMessageTemplate({'content': content, 'template_type': template_type}) - return template.is_message_too_long() + if template_type == SMS_TYPE: + template = SMSMessageTemplate({'content': content, 'template_type': template_type}) + return template.is_message_too_long() + if template_type == BROADCAST_TYPE: + template = BroadcastMessageTemplate({'content': content, 'template_type': template_type}) + return template.is_message_too_long() + return False def validate_parent_folder(template_json): diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 79610d09c..f67075621 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -731,14 +731,24 @@ def test_should_return_404_if_no_templates_for_service_with_id(client, sample_se assert json_resp['message'] == 'No result found' -def test_create_400_for_over_limit_content(client, notify_api, sample_user, sample_service, fake_uuid): +@pytest.mark.parametrize('template_type', ( + SMS_TYPE, BROADCAST_TYPE, +)) +def test_create_400_for_over_limit_content( + client, + notify_api, + sample_user, + fake_uuid, + template_type, +): + sample_service = create_service(service_permissions=[template_type]) content = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(SMS_CHAR_COUNT_LIMIT + 1)) data = { 'name': 'too big template', - 'template_type': SMS_TYPE, + 'template_type': template_type, 'content': content, 'service': str(sample_service.id), - 'created_by': str(sample_user.id) + 'created_by': str(sample_service.created_by.id) } data = json.dumps(data) auth_header = create_authorization_header()