Merge pull request #547 from alphagov/only-character-limit-sms

Only put a character limit on SMS notifications
This commit is contained in:
Chris Hill-Scott
2016-07-21 17:30:20 +01:00
committed by GitHub
2 changed files with 39 additions and 12 deletions

View File

@@ -226,7 +226,7 @@ def send_notification(notification_type):
raise InvalidRequest(error, status_code=429) raise InvalidRequest(error, status_code=429)
notification, errors = ( notification, errors = (
sms_template_notification_schema if notification_type == 'sms' else email_notification_schema sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
).load(request.get_json()) ).load(request.get_json())
if errors: if errors:
@@ -256,7 +256,10 @@ def send_notification(notification_type):
errors = {'template': [message]} errors = {'template': [message]}
raise InvalidRequest(errors, status_code=400) raise InvalidRequest(errors, status_code=400)
if template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT'): if (
template_object.template_type == SMS_TYPE and
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
):
char_count = current_app.config.get('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(char_count)
errors = {'content': [message]} errors = {'content': [message]}

View File

@@ -306,13 +306,33 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
assert test_string in json_resp['message'] assert test_string in json_resp['message']
def test_should_not_allow_template_content_too_large(notify_api, notify_db, notify_db_session, sample_user): @pytest.mark.parametrize(
'template_type, should_error', [
('sms', True),
('email', False)
]
)
def test_should_not_allow_template_content_too_large(
notify_api,
notify_db,
notify_db_session,
sample_user,
template_type,
mocker,
should_error
):
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
template = create_sample_template(notify_db, notify_db_session, content="((long_text))") mocker.patch('app.celery.tasks.send_email.apply_async')
template = create_sample_template(
notify_db,
notify_db_session,
content="((long_text))",
template_type=template_type
)
limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT') limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
json_data = json.dumps({ json_data = json.dumps({
'to': sample_user.mobile_number, 'to': sample_user.mobile_number if template_type == 'sms' else sample_user.email_address,
'template': template.id, 'template': template.id,
'personalisation': { 'personalisation': {
'long_text': ''.join( 'long_text': ''.join(
@@ -322,14 +342,18 @@ def test_should_not_allow_template_content_too_large(notify_api, notify_db, noti
auth_header = create_authorization_header(service_id=template.service_id) auth_header = create_authorization_header(service_id=template.service_id)
resp = client.post( resp = client.post(
path='/notifications/sms', path='/notifications/{}'.format(template_type),
data=json_data, data=json_data,
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header]
assert resp.status_code == 400 )
json_resp = json.loads(resp.get_data(as_text=True)) if should_error:
assert json_resp['message']['content'][0] == ( assert resp.status_code == 400
'Content has a character count greater' json_resp = json.loads(resp.get_data(as_text=True))
' than the limit of {}').format(limit) assert json_resp['message']['content'][0] == (
'Content has a character count greater'
' than the limit of {}').format(limit)
else:
assert resp.status_code == 201
@freeze_time("2016-01-01 11:09:00.061258") @freeze_time("2016-01-01 11:09:00.061258")