Validate against messages with no content

This commit is contained in:
Pea Tyczynska
2019-11-08 13:44:27 +00:00
parent 28ae7705c2
commit 9c804f701b
2 changed files with 34 additions and 0 deletions

View File

@@ -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

View File

@@ -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,