Working tests and provider stats table.

Fix for tests and import error.

Added tests and updated for code review comments.
This commit is contained in:
Nicholas Staples
2016-04-25 10:38:37 +01:00
parent ff61223d97
commit b56e324a4c
14 changed files with 365 additions and 90 deletions

View File

@@ -132,10 +132,11 @@ def test_should_allow_valid_token_with_post_body(notify_api, sample_api_key):
data = {
'email_from': 'new name',
'name': 'new name',
'users': [service.users[0].id],
'users': [str(service.users[0].id)],
'message_limit': 1000,
'restricted': False,
'active': False}
'active': False,
'created_by': str(service.users[0].id)}
token = create_jwt_token(
request_method="POST",

View File

@@ -135,7 +135,10 @@ def sample_template(notify_db,
template_type="sms",
content="This is a template",
subject_line='Subject',
user=None,
service=None):
if user is None:
user = sample_user(notify_db, notify_db_session)
if service is None:
service = sample_service(notify_db, notify_db_session)
sample_api_key(notify_db, notify_db_session, service=service)
@@ -143,7 +146,8 @@ def sample_template(notify_db,
'name': template_name,
'template_type': template_type,
'content': content,
'service': service
'service': service,
'created_by': user
}
if template_type == 'email':
data.update({
@@ -165,9 +169,12 @@ def sample_email_template(
notify_db_session,
template_name="Email Template Name",
template_type="email",
user=None,
content="This is a template",
subject_line='Email Subject',
service=None):
if user is None:
user = sample_user(notify_db, notify_db_session)
if service is None:
service = sample_service(notify_db, notify_db_session)
sample_api_key(notify_db, notify_db_session, service=service)
@@ -175,7 +182,8 @@ def sample_email_template(
'name': template_name,
'template_type': template_type,
'content': content,
'service': service
'service': service,
'created_by': user
}
if subject_line:
data.update({

View File

@@ -11,12 +11,13 @@ from app.models import Template
import pytest
def test_create_template(sample_service):
def test_create_template(sample_service, sample_user):
data = {
'name': 'Sample Template',
'template_type': "sms",
'content': "Template content",
'service': sample_service
'service': sample_service,
'created_by': sample_user
}
template = Template(**data)
dao_create_template(template)
@@ -26,13 +27,14 @@ def test_create_template(sample_service):
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template'
def test_create_email_template(sample_service):
def test_create_email_template(sample_service, sample_user):
data = {
'name': 'Sample Template',
'template_type': "email",
'subject': "subject",
'content': "Template content",
'service': sample_service
'service': sample_service,
'created_by': sample_user
}
template = Template(**data)
dao_create_template(template)
@@ -42,12 +44,13 @@ def test_create_email_template(sample_service):
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template'
def test_update_template(sample_service):
def test_update_template(sample_service, sample_user):
data = {
'name': 'Sample Template',
'template_type': "sms",
'content': "Template content",
'service': sample_service
'service': sample_service,
'created_by': sample_user
}
template = Template(**data)
dao_create_template(template)
@@ -59,7 +62,7 @@ def test_update_template(sample_service):
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'new name'
def test_get_all_templates_for_service(service_factory):
def test_get_all_templates_for_service(notify_db, notify_db_session, service_factory):
service_1 = service_factory.get('service 1', email_from='service.1')
service_2 = service_factory.get('service 2', email_from='service.2')
@@ -67,55 +70,61 @@ def test_get_all_templates_for_service(service_factory):
assert len(dao_get_all_templates_for_service(service_1.id)) == 1
assert len(dao_get_all_templates_for_service(service_2.id)) == 1
template_1 = Template(
name='Sample Template 1',
template_1 = create_sample_template(
notify_db,
notify_db_session,
template_name='Sample Template 1',
template_type="sms",
content="Template content",
service=service_1
)
template_2 = Template(
name='Sample Template 2',
template_2 = create_sample_template(
notify_db,
notify_db_session,
template_name='Sample Template 2',
template_type="sms",
content="Template content",
service=service_1
)
template_3 = Template(
name='Sample Template 3',
template_3 = create_sample_template(
notify_db,
notify_db_session,
template_name='Sample Template 3',
template_type="sms",
content="Template content",
service=service_2
)
dao_create_template(template_1)
dao_create_template(template_2)
dao_create_template(template_3)
assert Template.query.count() == 5
assert len(dao_get_all_templates_for_service(service_1.id)) == 3
assert len(dao_get_all_templates_for_service(service_2.id)) == 2
def test_get_all_templates_for_service_in_created_order(sample_service):
template_1 = Template(
name='Sample Template 1',
def test_get_all_templates_for_service_in_created_order(notify_db, notify_db_session, sample_service):
template_1 = create_sample_template(
notify_db,
notify_db_session,
template_name='Sample Template 1',
template_type="sms",
content="Template content",
service=sample_service
)
template_2 = Template(
name='Sample Template 2',
template_2 = create_sample_template(
notify_db,
notify_db_session,
template_name='Sample Template 2',
template_type="sms",
content="Template content",
service=sample_service
)
template_3 = Template(
name='Sample Template 3',
template_3 = create_sample_template(
notify_db,
notify_db_session,
template_name='Sample Template 3',
template_type="sms",
content="Template content",
service=sample_service
)
dao_create_template(template_1)
dao_create_template(template_2)
dao_create_template(template_3)
assert Template.query.count() == 3
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template 1'
@@ -148,3 +157,64 @@ def test_get_template_by_id_and_service_returns_none_if_no_template(sample_servi
with pytest.raises(NoResultFound) as e:
dao_get_template_by_id_and_service_id(template_id=fake_uuid, service_id=sample_service.id)
assert 'No row was found for one' in str(e.value)
def test_create_template_creates_a_history_record_with_current_data(sample_service, sample_user):
assert Template.query.count() == 0
assert Template.get_history_model().query.count() == 0
data = {
'name': 'Sample Template',
'template_type': "email",
'subject': "subject",
'content': "Template content",
'service': sample_service,
'created_by': sample_user
}
template = Template(**data)
dao_create_template(template)
assert Template.query.count() == 1
template_from_db = Template.query.first()
template_history = Template.get_history_model().query.first()
assert template_from_db.id == template_history.id
assert template_from_db.name == template_history.name
assert template_from_db.version == 1
assert template_from_db.version == template_history.version
assert sample_user.id == template_history.created_by_id
assert template_from_db.created_by.id == template_history.created_by_id
def test_update_template_creates_a_history_record_with_current_data(sample_service, sample_user):
assert Template.query.count() == 0
assert Template.get_history_model().query.count() == 0
data = {
'name': 'Sample Template',
'template_type': "email",
'subject': "subject",
'content': "Template content",
'service': sample_service,
'created_by': sample_user
}
template = Template(**data)
dao_create_template(template)
created = dao_get_all_templates_for_service(sample_service.id)[0]
assert created.name == 'Sample Template'
assert Template.query.count() == 1
assert Template.query.first().version == 1
assert Template.get_history_model().query.count() == 1
created.name = 'new name'
dao_update_template(created)
assert Template.query.count() == 1
assert Template.get_history_model().query.count() == 2
template_from_db = Template.query.first()
assert template_from_db.version == 2
assert Template.get_history_model().query.filter_by(name='Sample Template').one().version == 1
assert Template.get_history_model().query.filter_by(name='new name').one().version == 2

View File

@@ -486,10 +486,10 @@ def test_filter_by_status_and_template_type(notify_api,
headers=[auth_header])
notifications = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert len(notifications['notifications']) == 1
assert notifications['notifications'][0]['template']['template_type'] == 'email'
assert notifications['notifications'][0]['status'] == 'delivered'
assert response.status_code == 200
def test_get_notification_statistics(

View File

@@ -240,6 +240,33 @@ def test_should_not_create_service_with_missing_user_id_field(notify_api, fake_u
assert 'Missing data for required field.' in json_resp['message']['user_id']
def test_should_error_if_created_by_missing(notify_api, sample_user):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'email_from': 'service',
'name': 'created service',
'message_limit': 1000,
'restricted': False,
'active': False,
'user_id': str(sample_user.id)
}
auth_header = create_authorization_header(
path='/service',
method='POST',
request_body=json.dumps(data)
)
headers = [('Content-Type', 'application/json'), auth_header]
resp = client.post(
'/service',
data=json.dumps(data),
headers=headers)
json_resp = json.loads(resp.get_data(as_text=True))
assert resp.status_code == 400
assert json_resp['result'] == 'error'
assert 'Missing data for required field.' in json_resp['message']['created_by']
def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(notify_api,
notify_db,
notify_db_session,
@@ -321,8 +348,9 @@ def test_should_not_create_service_with_duplicate_name(notify_api,
'/service',
data=json.dumps(data),
headers=headers)
assert resp.status_code == 500
assert 'Key (name)=(Sample service) already exists.' in resp.get_data(as_text=True)
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert "Duplicate service name '{}'".format(sample_service.name) in json_resp['message']['name']
def test_create_service_should_throw_duplicate_key_constraint_for_existing_email_from(notify_api,
@@ -333,8 +361,9 @@ def test_create_service_should_throw_duplicate_key_constraint_for_existing_email
first_service = service_factory.get('First service', email_from='first.service')
with notify_api.test_request_context():
with notify_api.test_client() as client:
service_name = 'First SERVICE'
data = {
'name': 'First SERVICE',
'name': service_name,
'user_id': str(first_service.users[0].id),
'message_limit': 1000,
'restricted': False,
@@ -351,8 +380,9 @@ def test_create_service_should_throw_duplicate_key_constraint_for_existing_email
'/service',
data=json.dumps(data),
headers=headers)
assert resp.status_code == 500
assert 'Key (email_from)=(first.service) already exists.' in resp.get_data(as_text=True)
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert "Duplicate service name '{}'".format(service_name) in json_resp['message']['name']
def test_update_service(notify_api, sample_service):
@@ -372,7 +402,8 @@ def test_update_service(notify_api, sample_service):
data = {
'name': 'updated service name',
'email_from': 'updated.service.name'
'email_from': 'updated.service.name',
'created_by': str(sample_service.created_by.id)
}
auth_header = create_authorization_header(
@@ -400,14 +431,15 @@ def test_should_not_update_service_with_duplicate_name(notify_api,
with notify_api.test_request_context():
with notify_api.test_client() as client:
service_name = "another name"
create_sample_service(
service = create_sample_service(
notify_db,
notify_db_session,
service_name=service_name,
user=sample_user,
email_from='another.name')
data = {
'name': service_name
'name': service_name,
'created_by': str(service.created_by.id)
}
auth_header = create_authorization_header(
@@ -421,8 +453,10 @@ def test_should_not_update_service_with_duplicate_name(notify_api,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert resp.status_code == 500
assert 'Key (name)=(another name) already exists.' in resp.get_data(as_text=True)
assert resp.status_code == 400
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert "Duplicate service name '{}'".format(service_name) in json_resp['message']['name']
def test_should_not_update_service_with_duplicate_email_from(notify_api,
@@ -433,14 +467,17 @@ def test_should_not_update_service_with_duplicate_email_from(notify_api,
with notify_api.test_request_context():
with notify_api.test_client() as client:
email_from = "duplicate.name"
create_sample_service(
service_name = "duplicate name"
service = create_sample_service(
notify_db,
notify_db_session,
service_name="duplicate name",
service_name=service_name,
user=sample_user,
email_from=email_from)
data = {
'email_from': email_from
'name': service_name,
'email_from': email_from,
'created_by': str(service.created_by.id)
}
auth_header = create_authorization_header(
@@ -454,8 +491,10 @@ def test_should_not_update_service_with_duplicate_email_from(notify_api,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert resp.status_code == 500
assert 'Key (email_from)=(duplicate.name) already exists.' in resp.get_data(as_text=True)
assert resp.status_code == 400
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert "Duplicate service name '{}'".format(service_name) in json_resp['message']['name']
def test_update_service_should_404_if_id_is_invalid(notify_api, notify_db, notify_db_session):

View File

@@ -4,14 +4,15 @@ import uuid
from tests import create_authorization_header
def test_should_create_a_new_sms_template_for_a_service(notify_api, sample_service):
def test_should_create_a_new_sms_template_for_a_service(notify_api, sample_user, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template <b>content</b>',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -35,7 +36,7 @@ def test_should_create_a_new_sms_template_for_a_service(notify_api, sample_servi
assert not json_resp['data']['subject']
def test_should_create_a_new_email_template_for_a_service(notify_api, sample_service):
def test_should_create_a_new_email_template_for_a_service(notify_api, sample_user, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
@@ -43,7 +44,8 @@ def test_should_create_a_new_email_template_for_a_service(notify_api, sample_ser
'template_type': 'email',
'subject': 'subject',
'content': 'template <b>content</b>',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -67,14 +69,15 @@ def test_should_create_a_new_email_template_for_a_service(notify_api, sample_ser
assert json_resp['data']['id']
def test_should_be_error_if_service_does_not_exist_on_create(notify_api, fake_uuid):
def test_should_be_error_if_service_does_not_exist_on_create(notify_api, sample_user, fake_uuid):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template content',
'service': fake_uuid
'service': fake_uuid,
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -94,6 +97,33 @@ def test_should_be_error_if_service_does_not_exist_on_create(notify_api, fake_uu
assert json_resp['message'] == 'No result found'
def test_should_error_if_created_by_missing(notify_api, sample_user, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
service_id = str(sample_service.id)
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template content',
'service': service_id
}
data = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template'.format(service_id),
method='POST',
request_body=data
)
response = client.post(
'/service/{}/template'.format(service_id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert json_resp['result'] == 'error'
def test_should_be_error_if_service_does_not_exist_on_update(notify_api, fake_uuid):
with notify_api.test_request_context():
with notify_api.test_client() as client:
@@ -118,14 +148,15 @@ def test_should_be_error_if_service_does_not_exist_on_update(notify_api, fake_uu
assert json_resp['message'] == 'No result found'
def test_must_have_a_subject_on_an_email_template(notify_api, sample_service):
def test_must_have_a_subject_on_an_email_template(notify_api, sample_user, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'my template',
'template_type': 'email',
'content': 'template content',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -145,7 +176,7 @@ def test_must_have_a_subject_on_an_email_template(notify_api, sample_service):
assert json_resp['message'] == {'subject': ['Invalid template subject']}
def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_service):
def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_user, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
@@ -153,7 +184,8 @@ def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_servi
'template_type': 'email',
'subject': 'subject',
'content': 'template content',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -174,7 +206,8 @@ def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_servi
'template_type': 'email',
'subject': 'subject',
'content': 'template content',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -194,7 +227,7 @@ def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_servi
assert json_resp['message'][0]['subject'] == 'Duplicate template subject'
def test_should_be_able_to_update_a_template(notify_api, sample_service):
def test_should_be_able_to_update_a_template(notify_api, sample_user, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
@@ -202,7 +235,8 @@ def test_should_be_able_to_update_a_template(notify_api, sample_service):
'template_type': 'email',
'subject': 'subject',
'content': 'template content',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -220,7 +254,8 @@ def test_should_be_able_to_update_a_template(notify_api, sample_service):
json_resp = json.loads(create_response.get_data(as_text=True))
assert json_resp['data']['name'] == 'my template'
data = {
'content': 'my template has new content <script type="text/javascript">alert("foo")</script>'
'content': 'my template has new content <script type="text/javascript">alert("foo")</script>',
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
@@ -240,7 +275,7 @@ def test_should_be_able_to_update_a_template(notify_api, sample_service):
assert update_json_resp['data']['content'] == 'my template has new content alert("foo")'
def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_service):
def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_user, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
@@ -248,7 +283,8 @@ def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_se
'template_type': 'email',
'subject': 'subject 1',
'content': 'template content',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data_1 = json.dumps(data)
data = {
@@ -256,7 +292,8 @@ def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_se
'template_type': 'email',
'subject': 'subject 2',
'content': 'template content',
'service': str(sample_service.id)
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data_2 = json.dumps(data)
auth_header = create_authorization_header(
@@ -297,7 +334,7 @@ def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_se
assert update_json_resp['data'][1]['name'] == 'my template 2'
def test_should_get_only_templates_for_that_servcie(notify_api, service_factory):
def test_should_get_only_templates_for_that_service(notify_api, sample_user, service_factory):
with notify_api.test_request_context():
with notify_api.test_client() as client:
@@ -338,7 +375,8 @@ def test_should_get_only_templates_for_that_servcie(notify_api, service_factory)
'template_type': 'email',
'subject': 'subject 2',
'content': 'template content',
'service': str(service_1.id)
'service': str(service_1.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
create_auth_header = create_authorization_header(