Use new value of SMS_CHAR_COUNT_LIMIT from utils

Admin, API and utils were all defining a value for SMS_CHAR_COUNT_LIMIT.
This value has been updated in notifications-utils to allow text
messages to be 4 fragments long and notifications-api now gets the value of
SMS_CHAR_COUNT_LIMIT from notifications-utils instead of defining it in
config.

Also updated some tests to check for the higher limit.
This commit is contained in:
Katie Smith
2018-08-16 16:25:58 +01:00
parent 85af1e7fe8
commit a87be9b74a
8 changed files with 29 additions and 28 deletions

View File

@@ -126,7 +126,6 @@ class Config(object):
SQLALCHEMY_POOL_RECYCLE = 300
PAGE_SIZE = 50
API_PAGE_SIZE = 250
SMS_CHAR_COUNT_LIMIT = 495
TEST_MESSAGE_FILENAME = 'Test message'
ONE_OFF_MESSAGE_FILENAME = 'Report'
MAX_VERIFY_CODE_COUNT = 10

View File

@@ -39,6 +39,7 @@ from app.schemas import (
from app.service.utils import service_allowed_to_send_to
from app.utils import pagination_links, get_template_instance, get_public_notify_type_text
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.recipients import get_international_phone_info
notifications = Blueprint('notifications', __name__)
@@ -198,10 +199,9 @@ def create_template_object_for_notification(template, personalisation):
if (
template_object.template_type == SMS_TYPE and
template_object.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
template_object.content_count > SMS_CHAR_COUNT_LIMIT
):
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
message = 'Content has a character count greater than the limit of {}'.format(char_count)
message = 'Content has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
errors = {'content': [message]}
raise InvalidRequest(errors, status_code=400)
return template_object

View File

@@ -1,5 +1,6 @@
from sqlalchemy.orm.exc import NoResultFound
from flask import current_app
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.recipients import (
validate_and_format_phone_number,
validate_and_format_email_address,
@@ -115,9 +116,8 @@ def validate_and_format_recipient(send_to, key_type, service, notification_type,
def check_sms_content_char_count(content_count):
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
if content_count > char_count_limit:
message = 'Content for template has a character count greater than the limit of {}'.format(char_count_limit)
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)
raise BadRequestError(message=message)

View File

@@ -8,6 +8,7 @@ from flask import (
current_app,
jsonify,
request)
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.pdf import extract_page_from_pdf
from notifications_utils.template import SMSMessageTemplate
from requests import post as requests_post
@@ -42,7 +43,7 @@ def _content_count_greater_than_limit(content, template_type):
if template_type != SMS_TYPE:
return False
template = SMSMessageTemplate({'content': content, 'template_type': template_type})
return template.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
return template.content_count > SMS_CHAR_COUNT_LIMIT
@template_blueprint.route('', methods=['POST'])
@@ -61,8 +62,7 @@ def create_template(service_id):
new_template.service = fetched_service
over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type)
if over_limit:
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
message = 'Content has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
errors = {'content': [message]}
raise InvalidRequest(errors, status_code=400)
@@ -104,8 +104,7 @@ def update_template(service_id, template_id):
over_limit = _content_count_greater_than_limit(updated_template['content'], fetched_template.template_type)
if over_limit:
char_count_limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
message = 'Content has a character count greater than the limit of {}'.format(SMS_CHAR_COUNT_LIMIT)
errors = {'content': [message]}
raise InvalidRequest(errors, status_code=400)

View File

@@ -6,6 +6,7 @@ from datetime import datetime
from flask import (json, current_app)
from freezegun import freeze_time
from notifications_python_client.authentication import create_jwt_token
from notifications_utils import SMS_CHAR_COUNT_LIMIT
import app
from app.dao import notifications_dao
@@ -1020,7 +1021,6 @@ def test_create_template_raises_invalid_request_when_content_too_large(
content="((long_text))",
template_type=template_type
)
limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
template = Template.query.get(sample.id)
from app.notifications.rest import create_template_object_for_notification
try:
@@ -1028,13 +1028,14 @@ def test_create_template_raises_invalid_request_when_content_too_large(
{'long_text':
''.join(
random.choice(string.ascii_uppercase + string.digits) for _ in
range(limit + 1))})
range(SMS_CHAR_COUNT_LIMIT + 1))})
if should_error:
pytest.fail("expected an InvalidRequest")
except InvalidRequest as e:
if not should_error:
pytest.fail("do not expect an InvalidRequest")
assert e.message == {'content': ['Content has a character count greater than the limit of {}'.format(limit)]}
assert e.message == {'content': ['Content has a character count greater than the limit of {}'.format(
SMS_CHAR_COUNT_LIMIT)]}
@pytest.mark.parametrize("notification_type, send_to",

View File

@@ -1,6 +1,7 @@
import pytest
from freezegun import freeze_time
from flask import current_app
from notifications_utils import SMS_CHAR_COUNT_LIMIT
import app
from app.models import INTERNATIONAL_SMS_TYPE, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE
@@ -264,18 +265,18 @@ def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(n
assert e.value.fields == []
@pytest.mark.parametrize('char_count', [495, 0, 494, 200])
@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('char_count', [496, 500, 6000])
@pytest.mark.parametrize('char_count', [613, 700, 6000])
def test_check_sms_content_char_count_fails(char_count, notify_api):
with pytest.raises(BadRequestError) as e:
check_sms_content_char_count(char_count)
assert e.value.status_code == 400
assert e.value.message == 'Content for template has a character count greater than the limit of {}'.format(
notify_api.config['SMS_CHAR_COUNT_LIMIT'])
SMS_CHAR_COUNT_LIMIT)
assert e.value.fields == []

View File

@@ -2,6 +2,7 @@ import uuid
from unittest.mock import Mock
import pytest
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.recipients import InvalidPhoneError
from app.v2.errors import BadRequestError, TooManyRequestsError
@@ -191,14 +192,15 @@ def test_send_one_off_notification_raises_if_message_too_long(persist_mock, noti
post_data = {
'template_id': str(template.id),
'to': '07700 900 001',
'personalisation': {'name': '🚫' * 500},
'personalisation': {'name': '🚫' * 700},
'created_by': str(service.created_by_id)
}
with pytest.raises(BadRequestError) as e:
send_one_off_notification(service.id, post_data)
assert e.value.message == 'Content for template has a character count greater than the limit of 495'
assert e.value.message == 'Content for template has a character count greater than the limit of {}'.format(
SMS_CHAR_COUNT_LIMIT)
def test_send_one_off_notification_fails_if_created_by_other_service(sample_template):

View File

@@ -9,6 +9,8 @@ import pytest
import requests_mock
from PyPDF2.utils import PdfReadError
from freezegun import freeze_time
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from app.models import Template, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, TemplateHistory
from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template
@@ -471,9 +473,7 @@ def test_should_return_404_if_no_templates_for_service_with_id(client, sample_se
def test_create_400_for_over_limit_content(client, notify_api, sample_user, sample_service, fake_uuid):
limit = notify_api.config.get('SMS_CHAR_COUNT_LIMIT')
content = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(limit + 1))
content = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(SMS_CHAR_COUNT_LIMIT + 1))
data = {
'name': 'too big template',
'template_type': SMS_TYPE,
@@ -493,14 +493,13 @@ def test_create_400_for_over_limit_content(client, notify_api, sample_user, samp
json_resp = json.loads(response.get_data(as_text=True))
assert (
'Content has a character count greater than the limit of {}'
).format(limit) in json_resp['message']['content']
).format(SMS_CHAR_COUNT_LIMIT) in json_resp['message']['content']
def test_update_400_for_over_limit_content(client, notify_api, sample_user, sample_template):
limit = notify_api.config.get('SMS_CHAR_COUNT_LIMIT')
json_data = json.dumps({
'content': ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(limit + 1)),
'content': ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(
SMS_CHAR_COUNT_LIMIT + 1)),
'created_by': str(sample_user.id)
})
auth_header = create_authorization_header()
@@ -513,7 +512,7 @@ def test_update_400_for_over_limit_content(client, notify_api, sample_user, samp
json_resp = json.loads(resp.get_data(as_text=True))
assert (
'Content has a character count greater than the limit of {}'
).format(limit) in json_resp['message']['content']
).format(SMS_CHAR_COUNT_LIMIT) in json_resp['message']['content']
def test_should_return_all_template_versions_for_service_and_template_id(client, sample_template):