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,7 +8,6 @@ from flask import (
from app import api_user, authenticated_service
from app.config import QueueNames
from app.dao import (
templates_dao,
notifications_dao
)
from app.errors import (
@@ -25,10 +24,9 @@ from app.notifications.process_notifications import (
simulated_recipient
)
from app.notifications.validators import (
check_template_is_for_notification_type,
check_template_is_active,
check_rate_limiting,
service_has_permission,
validate_template
)
from app.schemas import (
email_notification_schema,
@@ -102,14 +100,12 @@ def send_notification(notification_type):
check_rate_limiting(authenticated_service, api_user)
template = templates_dao.dao_get_template_by_id_and_service_id(
template, template_with_content = validate_template(
template_id=notification_form['template'],
service_id=authenticated_service.id)
check_template_is_for_notification_type(notification_type, template.template_type)
check_template_is_active(template)
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
personalisation=notification_form.get('personalisation', {}),
service=authenticated_service,
notification_type=notification_type
)
_service_allowed_to_send_to(notification_form, authenticated_service)
if not service_has_permission(notification_type, authenticated_service.permissions):
@@ -147,7 +143,7 @@ def send_notification(notification_type):
data=get_notification_return_data(
notification_model.id,
notification_form,
template_object)
template_with_content)
), 201

View File

@@ -124,9 +124,10 @@ def validate_and_format_recipient(send_to, key_type, service, notification_type,
return validate_and_format_email_address(email_address=send_to)
def check_sms_content_char_count(content_count):
if content_count > SMS_CHAR_COUNT_LIMIT:
message = 'Content for template has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
def check_content_char_count(template_with_content):
if template_with_content.is_message_too_long():
message = f"Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. " \
f"Your message is {template_with_content.content_count_without_prefix} characters"
raise BadRequestError(message=message)
@@ -151,9 +152,10 @@ def validate_template(template_id, personalisation, service, notification_type):
check_template_is_active(template)
template_with_content = create_content_for_notification(template, personalisation)
check_notification_content_is_not_empty(template_with_content)
if template.template_type == SMS_TYPE:
check_sms_content_char_count(template_with_content.content_count)
check_content_char_count(template_with_content)
return template, template_with_content