diff --git a/app/notifications/validators.py b/app/notifications/validators.py index 8fd129896..2f8d7ae9e 100644 --- a/app/notifications/validators.py +++ b/app/notifications/validators.py @@ -121,6 +121,12 @@ def check_sms_content_char_count(content_count): raise BadRequestError(message=message) +def check_if_notification_content_is_not_empty(template_with_content): + if len(template_with_content.__str__()) == 0: + message = 'This message is empty.' + raise BadRequestError(message=message) + + def validate_template(template_id, personalisation, service, notification_type): try: template = templates_dao.dao_get_template_by_id_and_service_id( @@ -137,6 +143,7 @@ def validate_template(template_id, personalisation, service, notification_type): template_with_content = create_content_for_notification(template, personalisation) if template.template_type == SMS_TYPE: check_sms_content_char_count(template_with_content.content_count) + check_if_notification_content_is_not_empty(template_with_content) return template, template_with_content diff --git a/tests/app/notifications/test_validators.py b/tests/app/notifications/test_validators.py index 5692c160e..aae00199d 100644 --- a/tests/app/notifications/test_validators.py +++ b/tests/app/notifications/test_validators.py @@ -4,8 +4,11 @@ from flask import current_app from notifications_utils import SMS_CHAR_COUNT_LIMIT import app +from app.dao import templates_dao from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE +from app.notifications.process_notifications import create_content_for_notification from app.notifications.validators import ( + check_if_notification_content_is_not_empty, check_service_over_daily_message_limit, check_template_is_for_notification_type, check_template_is_active, @@ -286,6 +289,30 @@ def test_check_sms_content_char_count_fails(char_count, notify_api): assert e.value.fields == [] +def test_check_if_notification_content_is_not_empty_passes(notify_api, mocker, sample_service): + template_id = create_template(sample_service, content="Content is not empty").id + template = templates_dao.dao_get_template_by_id_and_service_id( + template_id=template_id, + service_id=sample_service.id + ) + template_with_content = create_content_for_notification(template, {}) + assert check_if_notification_content_is_not_empty(template_with_content) is None + + +def test_check_if_notification_content_is_not_empty_fails(notify_api, mocker, sample_service): + template_id = create_template(sample_service, content="").id + template = templates_dao.dao_get_template_by_id_and_service_id( + template_id=template_id, + service_id=sample_service.id + ) + template_with_content = create_content_for_notification(template, {}) + with pytest.raises(BadRequestError) as e: + check_if_notification_content_is_not_empty(template_with_content) + assert e.value.status_code == 400 + assert e.value.message == 'This message is empty.' + assert e.value.fields == [] + + @pytest.mark.parametrize('key_type', ['team', 'live', 'test']) def test_that_when_exceed_rate_limit_request_fails( key_type,