Message limit added and all tests passing.

This commit is contained in:
Nicholas Staples
2016-04-29 10:36:59 +01:00
parent 87a02cba14
commit f71dbe9c0f
9 changed files with 135 additions and 36 deletions

View File

@@ -28,11 +28,11 @@ from app.dao.notifications_dao import (
update_notification_reference_by_id,
update_notification_status_by_reference,
dao_get_template_statistics_for_service,
get_character_count_of_content,
get_sms_message_count,
get_notifications_for_service
)
from notifications_utils.template import get_sms_fragment_count
from tests.app.conftest import sample_job
from tests.app.conftest import sample_notification
@@ -1089,18 +1089,6 @@ def test_should_limit_notifications_return_by_day_limit_plus_one(notify_db, noti
assert len(all_notifications) == 2
@pytest.mark.parametrize(
"content,encoding,expected_length",
[
("The quick brown fox jumped over the lazy dog", "utf-8", 44),
("", "utf-8", 3),
("'First line.\n", 'utf-8', 13),
("\t\n\r", 'utf-8', 3)
])
def test_get_character_count_of_content(content, encoding, expected_length):
assert get_character_count_of_content(content, encoding=encoding) == expected_length
@pytest.mark.parametrize(
"char_count, expected_sms_fragment_count",
[
@@ -1114,4 +1102,4 @@ def test_get_character_count_of_content(content, encoding, expected_length):
(461, 4)
])
def test_sms_fragment_count(char_count, expected_sms_fragment_count):
assert get_sms_message_count(char_count) == expected_sms_fragment_count
assert get_sms_fragment_count(char_count) == expected_sms_fragment_count

View File

@@ -1,5 +1,7 @@
from datetime import datetime
import uuid
import random
import string
import app.celery.tasks
from tests import create_authorization_header
from tests.app.conftest import sample_notification as create_sample_notification
@@ -7,7 +9,7 @@ from tests.app.conftest import sample_job as create_sample_job
from tests.app.conftest import sample_service as create_sample_service
from tests.app.conftest import sample_email_template as create_sample_email_template
from tests.app.conftest import sample_template as create_sample_template
from flask import json
from flask import (json, current_app, url_for)
from app.models import Service
from app.dao.templates_dao import dao_get_all_templates_for_service
from app.dao.services_dao import dao_update_service
@@ -764,6 +766,37 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
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):
with notify_api.test_request_context():
with notify_api.test_client() as client:
template = create_sample_template(notify_db, notify_db_session, content="((long_text))")
limit = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
json_data = json.dumps({
'to': sample_user.mobile_number,
'template': template.id,
'personalisation': {
'long_text': ''.join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(limit + 1))
}
})
endpoint = url_for('notifications.send_notification', notification_type='sms')
auth_header = create_authorization_header(
service_id=template.service.id,
request_body=json_data,
path=endpoint,
method='POST')
resp = client.post(
path=endpoint,
data=json_data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['message']['content'][0] == (
'Content has a character count greater'
' than the limit of {}').format(limit)
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker):
with notify_api.test_request_context():

View File

@@ -1,5 +1,7 @@
import json
import uuid
import random
import string
from app.models import Template
from tests import create_authorization_header
@@ -224,7 +226,7 @@ def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_user,
assert response.status_code == 400
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'][0]['subject'] == 'Duplicate template subject'
assert json_resp['message']['subject'][0] == 'Duplicate template subject'
def test_should_be_able_to_update_a_template(notify_api, sample_user, sample_service):
@@ -479,3 +481,59 @@ def test_should_return_404_if_no_templates_for_service_with_id(notify_api, sampl
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'No result found'
def test_create_400_for_over_limit_content(notify_api, sample_user, sample_service, fake_uuid):
with notify_api.test_request_context():
with notify_api.test_client() as client:
limit = notify_api.config.get('SMS_CHAR_COUNT_LIMIT')
content = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(limit + 1))
data = {
'name': 'too big template',
'template_type': 'sms',
'content': content,
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template'.format(sample_service.id),
method='POST',
request_body=data
)
response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert response.status_code == 400
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']
def test_update_400_for_over_limit_content(notify_api, sample_user, sample_template):
with notify_api.test_request_context():
with notify_api.test_client() as client:
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)),
'created_by': str(sample_user.id)
})
auth_header = create_authorization_header(
path='/service/{}/template/{}'.format(sample_template.service.id, sample_template.id),
method='POST',
request_body=json_data
)
resp = client.post(
'/service/{}/template/{}'.format(sample_template.service.id, sample_template.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=json_data
)
assert resp.status_code == 400
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']