More tests for rest and dao.

This commit is contained in:
Nicholas Staples
2016-01-13 12:14:21 +00:00
parent dad0fff4ba
commit 99820b3e85
5 changed files with 187 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
import json
from app.models import (Service, User)
from app.models import (Service, User, Template)
from app.dao.services_dao import save_model_service
from tests.app.conftest import sample_user as create_sample_user
from flask import url_for
@@ -261,3 +261,126 @@ def test_delete_service_not_exists(notify_api, notify_db, notify_db_session, sam
headers=[('Content-Type', 'application/json')])
assert resp.status_code == 404
assert Service.query.count() == 1
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': sample_service.id
}
resp = client.post(
url_for('service.create_template', service_id=sample_service.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json')])
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_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': sample_service.id
}
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')])
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
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': sample_service.id
}
resp = client.put(
url_for('service.update_template',
service_id="123",
template_id=sample_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json')])
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': sample_service.id
}
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')])
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