Use new Template method .is_message_empty()

This method has been now added to Template subclasses
used by sms, emails and letters, so we can use it to valdiate if
message is not empty.

Use new template method .is_message_empty()

Refactor function name and add a test
This commit is contained in:
Pea Tyczynska
2019-11-19 17:05:50 +00:00
parent 9c804f701b
commit f4ba82225b
2 changed files with 37 additions and 12 deletions

View File

@@ -121,8 +121,8 @@ def check_sms_content_char_count(content_count):
raise BadRequestError(message=message) raise BadRequestError(message=message)
def check_if_notification_content_is_not_empty(template_with_content): def check_notification_content_is_not_empty(template_with_content):
if len(template_with_content.__str__()) == 0: if template_with_content.is_message_empty():
message = 'This message is empty.' message = 'This message is empty.'
raise BadRequestError(message=message) raise BadRequestError(message=message)
@@ -140,10 +140,12 @@ def validate_template(template_id, personalisation, service, notification_type):
check_template_is_for_notification_type(notification_type, template.template_type) check_template_is_for_notification_type(notification_type, template.template_type)
check_template_is_active(template) check_template_is_active(template)
template_with_content = create_content_for_notification(template, personalisation) template_with_content = create_content_for_notification(template, personalisation)
check_notification_content_is_not_empty(template_with_content)
if template.template_type == SMS_TYPE: if template.template_type == SMS_TYPE:
check_sms_content_char_count(template_with_content.content_count) 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 return template, template_with_content

View File

@@ -8,18 +8,19 @@ from app.dao import templates_dao
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE
from app.notifications.process_notifications import create_content_for_notification from app.notifications.process_notifications import create_content_for_notification
from app.notifications.validators import ( from app.notifications.validators import (
check_if_notification_content_is_not_empty, check_notification_content_is_not_empty,
check_service_over_daily_message_limit, check_service_over_daily_message_limit,
check_template_is_for_notification_type, check_template_is_for_notification_type,
check_template_is_active, check_template_is_active,
service_can_send_to_recipient,
check_sms_content_char_count, check_sms_content_char_count,
check_service_over_api_rate_limit, check_service_over_api_rate_limit,
validate_and_format_recipient,
check_service_email_reply_to_id, check_service_email_reply_to_id,
check_service_sms_sender_id, check_service_sms_sender_id,
check_service_letter_contact_id, check_service_letter_contact_id,
check_reply_to, check_reply_to,
service_can_send_to_recipient,
validate_and_format_recipient,
validate_template
) )
from app.v2.errors import ( from app.v2.errors import (
@@ -289,30 +290,52 @@ def test_check_sms_content_char_count_fails(char_count, notify_api):
assert e.value.fields == [] assert e.value.fields == []
def test_check_if_notification_content_is_not_empty_passes(notify_api, mocker, sample_service): def test_check_notification_content_is_not_empty_passes(notify_api, mocker, sample_service):
template_id = create_template(sample_service, content="Content is not empty").id template_id = create_template(sample_service, content="Content is not empty").id
template = templates_dao.dao_get_template_by_id_and_service_id( template = templates_dao.dao_get_template_by_id_and_service_id(
template_id=template_id, template_id=template_id,
service_id=sample_service.id service_id=sample_service.id
) )
template_with_content = create_content_for_notification(template, {}) template_with_content = create_content_for_notification(template, {})
assert check_if_notification_content_is_not_empty(template_with_content) is None assert check_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): @pytest.mark.parametrize('template_content,notification_values', [
template_id = create_template(sample_service, content="").id ("", {}),
("((placeholder))", {"placeholder": ""})
])
def test_check_notification_content_is_not_empty_fails(
notify_api, mocker, sample_service, template_content, notification_values
):
template_id = create_template(sample_service, content=template_content).id
template = templates_dao.dao_get_template_by_id_and_service_id( template = templates_dao.dao_get_template_by_id_and_service_id(
template_id=template_id, template_id=template_id,
service_id=sample_service.id service_id=sample_service.id
) )
template_with_content = create_content_for_notification(template, {}) template_with_content = create_content_for_notification(template, notification_values)
with pytest.raises(BadRequestError) as e: with pytest.raises(BadRequestError) as e:
check_if_notification_content_is_not_empty(template_with_content) check_notification_content_is_not_empty(template_with_content)
assert e.value.status_code == 400 assert e.value.status_code == 400
assert e.value.message == 'This message is empty.' assert e.value.message == 'This message is empty.'
assert e.value.fields == [] assert e.value.fields == []
def test_validate_template(mocker, fake_uuid, sample_service):
template = create_template(sample_service, template_type="email")
mock_check_type = mocker.patch('app.notifications.validators.check_template_is_for_notification_type')
mock_check_if_active = mocker.patch('app.notifications.validators.check_template_is_active')
mock_create_conent = mocker.patch(
'app.notifications.validators.create_content_for_notification', return_value="content"
)
mock_check_not_empty = mocker.patch('app.notifications.validators.check_notification_content_is_not_empty')
validate_template(template.id, {}, sample_service, "email")
mock_check_type.assert_called_once_with("email", "email")
mock_check_if_active.assert_called_once_with(template)
mock_create_conent.assert_called_once_with(template, {})
mock_check_not_empty.assert_called_once_with("content")
@pytest.mark.parametrize('key_type', ['team', 'live', 'test']) @pytest.mark.parametrize('key_type', ['team', 'live', 'test'])
def test_that_when_exceed_rate_limit_request_fails( def test_that_when_exceed_rate_limit_request_fails(
key_type, key_type,