Updates to template endpoints:

- moved into templates rest class
- updated dao
- removed delete methods
- constraint on subject line
This commit is contained in:
Martyn Inglis
2016-02-22 12:55:18 +00:00
parent 443691402f
commit 9bb95a53ec
7 changed files with 540 additions and 383 deletions

View File

@@ -1,3 +1,5 @@
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.exc import IntegrityError
from app.dao.templates_dao import (
dao_create_template,
dao_get_template_by_id_and_service_id,
@@ -6,6 +8,7 @@ from app.dao.templates_dao import (
)
from tests.app.conftest import sample_template as create_sample_template
from app.models import Template
import pytest
def test_create_template(sample_service):
@@ -23,6 +26,36 @@ 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):
data = {
'name': 'Sample Template',
'template_type': "email",
'subject': "subject",
'content': "Template content",
'service': sample_service
}
template = Template(**data)
dao_create_template(template)
assert Template.query.count() == 1
assert len(dao_get_all_templates_for_service(sample_service.id)) == 1
assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template'
def test_create_email_template_fails_if_no_subject(sample_service):
data = {
'name': 'Sample Template',
'template_type': "email",
'content': "Template content",
'service': sample_service
}
template = Template(**data)
with pytest.raises(IntegrityError) as e:
dao_create_template(template)
assert 'new row for relation "templates" violates check constraint "ch_email_template_has_subject"' in str(e.value)
def test_update_template(sample_service):
data = {
'name': 'Sample Template',

View File

@@ -1,9 +1,8 @@
import json
import uuid
from flask import url_for
from app.dao.users_dao import save_model_user
from app.models import User, Template, Service
from app.models import User
from tests import create_authorization_header
@@ -342,261 +341,3 @@ def test_update_service_should_404_if_id_is_invalid(notify_api, notify_db):
headers=[('Content-Type', 'application/json'), auth_header]
)
assert resp.status_code == 404
def test_create_template(notify_api, notify_db, notify_db_session, sample_service):
"""
Tests POST endpoint '/<service_id>/template' a template can be created
from a service.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
assert Template.query.count() == 0
template_name = "template name"
template_type = "sms"
template_content = "This is a template"
data = {
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': str(sample_service.id)
}
auth_header = create_authorization_header(path=url_for('service.create_template',
service_id=sample_service.id),
method='POST',
request_body=json.dumps(data))
resp = client.post(
url_for('service.create_template', service_id=sample_service.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 201
assert Template.query.count() == 1
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == template_name
assert json_resp['data']['template_type'] == template_type
assert json_resp['data']['content'] == template_content
def test_create_template_service_not_exists(notify_api, notify_db, notify_db_session, sample_service):
"""
Tests POST endpoint '/<service_id>/template' a template can be created
from a service.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
assert Template.query.count() == 0
template_name = "template name"
template_type = "sms"
template_content = "This is a template"
data = {
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': str(sample_service.id)
}
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(path=url_for('service.create_template',
service_id=missing_service_id),
method='POST',
request_body=json.dumps(data))
resp = client.post(
url_for('service.create_template', service_id=missing_service_id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
assert Template.query.count() == 0
json_resp = json.loads(resp.get_data(as_text=True))
assert "Service not found" in json_resp['message']
def test_update_template(notify_api, notify_db, notify_db_session, sample_template):
"""
Tests PUT endpoint '/<service_id>/template/<template_id>' a template can be
updated.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
assert Template.query.count() == 1
sample_service = Service.query.first()
old_name = sample_template.name
template_name = "new name"
template_type = "sms"
template_content = "content has been changed"
data = {
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': str(sample_service.id)
}
auth_header = create_authorization_header(path=url_for('service.update_template',
service_id=sample_service.id,
template_id=sample_template.id),
method='PUT',
request_body=json.dumps(data))
resp = client.put(
url_for('service.update_template',
service_id=sample_service.id,
template_id=sample_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 200
assert Template.query.count() == 1
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == template_name
assert json_resp['data']['template_type'] == template_type
assert json_resp['data']['content'] == template_content
assert old_name != template_name
def test_update_template_service_not_exists(notify_api, notify_db, notify_db_session,
sample_template):
"""
Tests PUT endpoint '/<service_id>/template/<template_id>' a 404 if service
doesn't exist.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
assert Template.query.count() == 1
template_name = "new name"
template_type = "sms"
template_content = "content has been changed"
data = {
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': str(sample_template.service_id)
}
missing_service_id = uuid.uuid4()
auth_header = create_authorization_header(path=url_for('service.update_template',
service_id=missing_service_id,
template_id=sample_template.id),
method='PUT',
request_body=json.dumps(data))
resp = client.put(
url_for('service.update_template',
service_id=missing_service_id,
template_id=sample_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
json_resp = json.loads(resp.get_data(as_text=True))
assert "Service not found" in json_resp['message']
assert template_name != sample_template.name
def test_update_template_template_not_exists(notify_api, notify_db, notify_db_session,
sample_template):
"""
Tests PUT endpoint '/<service_id>/template/<template_id>' a 404 if template
doesn't exist.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
assert Template.query.count() == 1
sample_service = Service.query.first()
template_name = "new name"
template_type = "sms"
template_content = "content has been changed"
data = {
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': str(sample_service.id)
}
auth_header = create_authorization_header(path=url_for('service.update_template',
service_id=sample_service.id,
template_id="123"),
method='PUT',
request_body=json.dumps(data))
resp = client.put(
url_for('service.update_template',
service_id=sample_service.id,
template_id="123"),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 404
json_resp = json.loads(resp.get_data(as_text=True))
assert "Template not found" in json_resp['message']
assert template_name != sample_template.name
def test_create_template_unicode_content(notify_api, notify_db, notify_db_session, sample_service):
"""
Tests POST endpoint '/<service_id>/template/<template_id>' a template is
created and the content encoding is respected after saving and loading
from the db.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
assert Template.query.count() == 0
template_name = "template name"
template_type = "sms"
template_content = 'Россия'
data = {
'name': template_name,
'template_type': template_type,
'content': template_content,
'service': str(sample_service.id)
}
auth_header = create_authorization_header(path=url_for('service.create_template',
service_id=sample_service.id),
method='POST',
request_body=json.dumps(data))
resp = client.post(
url_for('service.create_template', service_id=sample_service.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 201
assert Template.query.count() == 1
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == template_name
assert json_resp['data']['template_type'] == template_type
assert json_resp['data']['content'] == template_content
def test_get_template_list(notify_api, notify_db, notify_db_session, sample_template):
"""
Tests GET endpoint '/' to retrieve entire template list.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header(
service_id=sample_template.service_id,
path=url_for(
'service.get_service_template',
service_id=sample_template.service_id),
method='GET')
response = client.get(
url_for(
'service.get_service_template',
service_id=sample_template.service_id),
headers=[auth_header])
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
assert len(json_resp['data']) == 1
assert json_resp['data'][0]['name'] == sample_template.name
assert json_resp['data'][0]['id'] == sample_template.id
def test_get_template(notify_api, notify_db, notify_db_session, sample_template):
"""
Tests GET endpoint '/<template_id>' to retrieve a single template.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header(
service_id=sample_template.service_id,
path=url_for(
'service.get_service_template',
template_id=sample_template.id,
service_id=sample_template.service_id),
method='GET')
resp = client.get(url_for(
'service.get_service_template',
template_id=sample_template.id,
service_id=sample_template.service_id),
headers=[auth_header])
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == sample_template.name
assert json_resp['data']['id'] == sample_template.id

View File

@@ -1,41 +1,413 @@
import json
from flask import url_for
import uuid
from tests import create_authorization_header
def test_get_template_list(notify_api, notify_db, notify_db_session, sample_template):
"""
Tests GET endpoint '/' to retrieve entire template list.
"""
def test_should_create_a_new_sms_template_for_a_service(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header(service_id=sample_template.service_id,
path=url_for('template.get_template'),
method='GET')
response = client.get(url_for('template.get_template'),
headers=[auth_header])
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template content',
'service': str(sample_service.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 == 201
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['data']['name'] == 'my template'
assert json_resp['data']['template_type'] == 'sms'
assert json_resp['data']['content'] == 'template content'
assert json_resp['data']['service'] == str(sample_service.id)
assert json_resp['data']['id']
assert not json_resp['data']['subject']
def test_should_create_a_new_email_template_for_a_service(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'my template',
'template_type': 'email',
'subject': 'subject',
'content': 'template content',
'service': str(sample_service.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 == 201
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['data']['name'] == 'my template'
assert json_resp['data']['template_type'] == 'email'
assert json_resp['data']['content'] == 'template content'
assert json_resp['data']['service'] == str(sample_service.id)
assert json_resp['data']['subject'] == 'subject'
assert json_resp['data']['id']
def test_should_be_error_if_service_does_not_exist_on_create(notify_api):
with notify_api.test_request_context():
with notify_api.test_client() as client:
bad_id = str(uuid.uuid4())
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template content',
'service': bad_id
}
data = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template'.format(bad_id),
method='POST',
request_body=data
)
response = client.post(
'/service/{}/template'.format(bad_id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Service not found'
def test_should_be_error_if_service_does_not_exist_on_update(notify_api):
with notify_api.test_request_context():
with notify_api.test_client() as client:
bad_id = str(uuid.uuid4())
data = {
'name': 'my template'
}
data = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template/123'.format(bad_id),
method='POST',
request_body=data
)
response = client.post(
'/service/{}/template/123'.format(bad_id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Template not found'
def test_must_have_a_subject_on_an_email_template(notify_api, 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)
}
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
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 500
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Failed to create template'
def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'my template',
'template_type': 'email',
'subject': 'subject',
'content': 'template content',
'service': str(sample_service.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 == 201
data = {
'name': 'my template',
'template_type': 'email',
'subject': 'subject',
'content': 'template content',
'service': str(sample_service.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 json_resp['result'] == 'error'
assert json_resp['message'] == 'Duplicate template subject'
def test_should_be_able_to_update_a_template(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'my template',
'template_type': 'email',
'subject': 'subject',
'content': 'template content',
'service': str(sample_service.id)
}
data = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template'.format(sample_service.id),
method='POST',
request_body=data
)
create_response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert create_response.status_code == 201
json_resp = json.loads(create_response.get_data(as_text=True))
assert json_resp['data']['name'] == 'my template'
data = {
'name': 'my template has a new name'
}
data = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template/{}'.format(sample_service.id, json_resp['data']['id']),
method='POST',
request_body=data
)
update_response = client.post(
'/service/{}/template/{}'.format(sample_service.id, json_resp['data']['id']),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert update_response.status_code == 200
update_json_resp = json.loads(update_response.get_data(as_text=True))
assert update_json_resp['data']['name'] == 'my template has a new name'
def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': 'my template 1',
'template_type': 'email',
'subject': 'subject 1',
'content': 'template content',
'service': str(sample_service.id)
}
data_1 = json.dumps(data)
data = {
'name': 'my template 2',
'template_type': 'email',
'subject': 'subject 2',
'content': 'template content',
'service': str(sample_service.id)
}
data_2 = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template'.format(sample_service.id),
method='POST',
request_body=data_1
)
client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data_1
)
auth_header = create_authorization_header(
path='/service/{}/template'.format(sample_service.id),
method='POST',
request_body=data_2
)
client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data_2
)
auth_header = create_authorization_header(
path='/service/{}/template'.format(sample_service.id),
method='GET'
)
response = client.get(
'/service/{}/template'.format(sample_service.id),
headers=[auth_header]
)
assert response.status_code == 200
update_json_resp = json.loads(response.get_data(as_text=True))
assert update_json_resp['data'][0]['name'] == 'my template 1'
assert update_json_resp['data'][1]['name'] == 'my template 2'
def test_should_get_only_templates_for_that_servcie(notify_api, service_factory):
with notify_api.test_request_context():
with notify_api.test_client() as client:
service_1 = service_factory.get('service 1')
service_2 = service_factory.get('service 2')
auth_header_1 = create_authorization_header(
path='/service/{}/template'.format(service_1.id),
method='GET'
)
response_1 = client.get(
'/service/{}/template'.format(service_1.id),
headers=[auth_header_1]
)
auth_header_2 = create_authorization_header(
path='/service/{}/template'.format(service_2.id),
method='GET'
)
response_2 = client.get(
'/service/{}/template'.format(service_2.id),
headers=[auth_header_2]
)
assert response_1.status_code == 200
assert response_2.status_code == 200
json_resp_1 = json.loads(response_1.get_data(as_text=True))
json_resp_2 = json.loads(response_2.get_data(as_text=True))
assert len(json_resp_1['data']) == 1
assert len(json_resp_2['data']) == 1
data = {
'name': 'my template 2',
'template_type': 'email',
'subject': 'subject 2',
'content': 'template content',
'service': str(service_1.id)
}
data = json.dumps(data)
create_auth_header = create_authorization_header(
path='/service/{}/template'.format(service_1.id),
method='POST',
request_body=data
)
client.post(
'/service/{}/template'.format(service_1.id),
headers=[('Content-Type', 'application/json'), create_auth_header],
data=data
)
response_3 = client.get(
'/service/{}/template'.format(service_1.id),
headers=[auth_header_1]
)
response_4 = client.get(
'/service/{}/template'.format(service_2.id),
headers=[auth_header_2]
)
assert response_3.status_code == 200
assert response_4.status_code == 200
json_resp_3 = json.loads(response_3.get_data(as_text=True))
json_resp_4 = json.loads(response_4.get_data(as_text=True))
assert len(json_resp_3['data']) == 2
assert len(json_resp_4['data']) == 1
def test_should_return_empty_array_if_no_templates_for_service(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header(
path='/service/{}/template'.format(sample_service.id),
method='GET'
)
response = client.get(
'/service/{}/template'.format(sample_service.id),
headers=[auth_header]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
assert len(json_resp['data']) == 1
assert json_resp['data'][0]['name'] == sample_template.name
assert json_resp['data'][0]['id'] == sample_template.id
assert len(json_resp['data']) == 0
def test_get_template(notify_api, notify_db, notify_db_session, sample_template):
"""
Tests GET endpoint '/<template_id>' to retrieve a single template.
"""
def test_should_return_404_if_no_templates_for_service_with_id(notify_api, sample_service):
with notify_api.test_request_context():
with notify_api.test_client() as client:
auth_header = create_authorization_header(service_id=sample_template.service_id,
path=url_for('template.get_template',
template_id=sample_template.id),
method='GET')
resp = client.get(url_for(
'template.get_template', template_id=sample_template.id),
headers=[auth_header])
assert resp.status_code == 200
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['name'] == sample_template.name
assert json_resp['data']['id'] == sample_template.id
auth_header = create_authorization_header(
path='/service/{}/template/{}'.format(sample_service.id, 111),
method='GET'
)
response = client.get(
'/service/{}/template/{}'.format(sample_service.id, 111),
headers=[auth_header]
)
assert response.status_code == 404
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'Template not found'