Update validators to use is_message_too_long()

- update check_sms_content_char_count to use the SMSTemplate.is_message_too_long function, and updated the error message to align with the message returned by the admin app.
- Update the the code used by version 1 of the api to use the validate_template method.
  - I did find a couple of services still using the old api, however, this change should not affect them as I checked the messages being sent and they are not too long.
  - We will be sending a message to them to see if they can upgrade.
- Update the log message for authenication to include the URL - makes it easier to track if a service is using version 1 of the api.
This commit is contained in:
Rebecca Law
2020-03-04 17:04:11 +00:00
parent 08ec06295a
commit a994e8fb6e
8 changed files with 63 additions and 35 deletions

View File

@@ -8,12 +8,12 @@ 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_content_char_count,
check_if_service_can_send_files_by_email,
check_notification_content_is_not_empty,
check_service_over_daily_message_limit,
check_template_is_for_notification_type,
check_template_is_active,
check_sms_content_char_count,
check_service_over_api_rate_limit,
check_service_email_reply_to_id,
check_service_sms_sender_id,
@@ -21,8 +21,9 @@ from app.notifications.validators import (
check_reply_to,
service_can_send_to_recipient,
validate_and_format_recipient,
validate_template
validate_template,
)
from app.utils import get_template_instance
from app.v2.errors import (
BadRequestError,
@@ -277,20 +278,40 @@ def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(s
@pytest.mark.parametrize('char_count', [612, 0, 494, 200])
def test_check_sms_content_char_count_passes(char_count, notify_api):
assert check_sms_content_char_count(char_count) is None
@pytest.mark.parametrize('show_prefix', [True, False])
@pytest.mark.parametrize('template_type', ['sms', 'email', 'letter'])
def test_check_content_char_count_passes(notify_db_session, show_prefix, char_count, template_type):
service = create_service(prefix_sms=show_prefix)
t = create_template(service=service, content='a' * char_count, template_type=template_type)
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id, service_id=service.id)
template_with_content = get_template_instance(template=template.__dict__, values={})
assert check_content_char_count(template_with_content) is None
@pytest.mark.parametrize('char_count', [613, 700, 6000])
def test_check_sms_content_char_count_fails(char_count, notify_api):
@pytest.mark.parametrize('show_prefix', [True, False])
def test_check_content_char_count_fails(notify_db_session, show_prefix, char_count):
with pytest.raises(BadRequestError) as e:
check_sms_content_char_count(char_count)
service = create_service(prefix_sms=show_prefix)
t = create_template(service=service, content='a' * char_count, template_type='sms')
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id, service_id=service.id)
template_with_content = get_template_instance(template=template.__dict__, values={})
check_content_char_count(template_with_content)
assert e.value.status_code == 400
assert e.value.message == 'Content for template has a character count greater than the limit of {}'.format(
SMS_CHAR_COUNT_LIMIT)
assert e.value.message == f'Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. ' \
f'Your message is {char_count} characters'
assert e.value.fields == []
@pytest.mark.parametrize('template_type', ['email', 'letter'])
def test_check_content_char_count_passes_for_long_email_or_letter(sample_service, template_type):
t = create_template(service=sample_service, content='a' * 1000, template_type=template_type)
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=t.id,
service_id=t.service_id)
template_with_content = get_template_instance(template=template.__dict__, values={})
assert check_content_char_count(template_with_content) is None
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 = templates_dao.dao_get_template_by_id_and_service_id(
@@ -321,7 +342,12 @@ def test_check_notification_content_is_not_empty_fails(
assert e.value.fields == []
def test_validate_template(mocker, fake_uuid, sample_service):
def test_validate_template(sample_service):
template = create_template(sample_service, template_type="email")
validate_template(template.id, {}, sample_service, "email")
def test_validate_template_calls_all_validators(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')
@@ -329,12 +355,14 @@ def test_validate_template(mocker, fake_uuid, sample_service):
'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')
mock_check_message_is_too_long = mocker.patch('app.notifications.validators.check_content_char_count')
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")
mock_check_message_is_too_long.assert_called_once_with("content")
@pytest.mark.parametrize('key_type', ['team', 'live', 'test'])