mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 18:38:14 -04:00
More tests for rest and dao.
This commit is contained in:
@@ -8,7 +8,10 @@ from app.models import (Template, Service)
|
|||||||
|
|
||||||
def save_model_template(template, update_dict=None):
|
def save_model_template(template, update_dict=None):
|
||||||
if update_dict:
|
if update_dict:
|
||||||
|
update_dict.pop('id', None)
|
||||||
|
service_id = update_dict.pop('service')
|
||||||
Template.query.filter_by(id=template.id).update(update_dict)
|
Template.query.filter_by(id=template.id).update(update_dict)
|
||||||
|
template.service = Service.query.get(service_id)
|
||||||
else:
|
else:
|
||||||
db.session.add(template)
|
db.session.add(template)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
@@ -23,7 +26,7 @@ def get_model_templates(template_id=None, service_id=None):
|
|||||||
# TODO need better mapping from function params to sql query.
|
# TODO need better mapping from function params to sql query.
|
||||||
if template_id and service_id:
|
if template_id and service_id:
|
||||||
return Template.query.filter_by(
|
return Template.query.filter_by(
|
||||||
id=template_id, service=Service.get(service_id)).one()
|
id=template_id, service=Service.query.get(service_id)).one()
|
||||||
elif template_id:
|
elif template_id:
|
||||||
return Template.query.filter_by(id=template_id).one()
|
return Template.query.filter_by(id=template_id).one()
|
||||||
elif service_id:
|
elif service_id:
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ class ServiceSchema(ma.ModelSchema):
|
|||||||
class TemplateSchema(ma.ModelSchema):
|
class TemplateSchema(ma.ModelSchema):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Template
|
model = models.Template
|
||||||
exclude = ("updated_at", "created_at")
|
exclude = ("updated_at", "created_at", "service_id")
|
||||||
|
|
||||||
|
|
||||||
user_schema = UserSchema()
|
user_schema = UserSchema()
|
||||||
|
|||||||
@@ -3,11 +3,14 @@ from sqlalchemy.exc import DataError
|
|||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
from app.dao.services_dao import (
|
from app.dao.services_dao import (
|
||||||
save_model_service, get_model_services, delete_model_service)
|
save_model_service, get_model_services, delete_model_service)
|
||||||
|
from app.dao.templates_dao import (
|
||||||
|
save_model_template, get_model_templates)
|
||||||
from app.dao.users_dao import get_model_users
|
from app.dao.users_dao import get_model_users
|
||||||
from app.dao import DAOException
|
from app.dao import DAOException
|
||||||
from .. import service
|
from .. import service
|
||||||
from app import db
|
from app import db
|
||||||
from app.schemas import (services_schema, service_schema)
|
from app.schemas import (
|
||||||
|
services_schema, service_schema, template_schema, templates_schema)
|
||||||
|
|
||||||
|
|
||||||
# TODO auth to be added.
|
# TODO auth to be added.
|
||||||
@@ -71,7 +74,7 @@ def get_service(service_id=None):
|
|||||||
|
|
||||||
# TODO auth to be added.
|
# TODO auth to be added.
|
||||||
@service.route('/<int:service_id>/template/', methods=['POST'])
|
@service.route('/<int:service_id>/template/', methods=['POST'])
|
||||||
def create_template():
|
def create_template(service_id):
|
||||||
try:
|
try:
|
||||||
service = get_model_services(service_id=service_id)
|
service = get_model_services(service_id=service_id)
|
||||||
except DataError:
|
except DataError:
|
||||||
@@ -121,4 +124,4 @@ def update_template(service_id, template_id):
|
|||||||
save_model_template(template, update_dict=update_dict)
|
save_model_template(template, update_dict=update_dict)
|
||||||
except DAOException as e:
|
except DAOException as e:
|
||||||
return jsonify(result="error", message=str(e)), 400
|
return jsonify(result="error", message=str(e)), 400
|
||||||
return jsonify(data=service_template.dump(template).data), status_code
|
return jsonify(data=template_schema.dump(template).data), status_code
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import pytest
|
||||||
|
from app.dao.templates_dao import (
|
||||||
|
save_model_template, get_model_templates, delete_model_template)
|
||||||
|
from tests.app.conftest import sample_template as create_sample_template
|
||||||
|
from app.models import Template
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_template(notify_api, notify_db, notify_db_session, sample_service):
|
||||||
|
assert Template.query.count() == 0
|
||||||
|
template_name = 'Sample Template'
|
||||||
|
data = {
|
||||||
|
'name': template_name,
|
||||||
|
'template_type': "sms",
|
||||||
|
'content': "Template content",
|
||||||
|
'service': sample_service}
|
||||||
|
template = Template(**data)
|
||||||
|
save_model_template(template)
|
||||||
|
assert Template.query.count() == 1
|
||||||
|
assert Template.query.first().name == template_name
|
||||||
|
assert Template.query.first().id == template.id
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_Templates(notify_api, notify_db, notify_db_session, sample_service):
|
||||||
|
sample_template = create_sample_template(notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service=sample_service)
|
||||||
|
assert Template.query.count() == 1
|
||||||
|
assert len(get_model_templates()) == 1
|
||||||
|
template_name = "Another Template"
|
||||||
|
sample_template = create_sample_template(notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
template_name=template_name,
|
||||||
|
service=sample_service)
|
||||||
|
assert Template.query.count() == 2
|
||||||
|
assert len(get_model_templates()) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_user_Template(notify_api, notify_db, notify_db_session, sample_service):
|
||||||
|
assert Template.query.count() == 0
|
||||||
|
template_name = "Random Template"
|
||||||
|
sample_template = create_sample_template(notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
template_name=template_name,
|
||||||
|
service=sample_service)
|
||||||
|
assert get_model_templates(template_id=sample_template.id).name == template_name
|
||||||
|
assert Template.query.count() == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_template(notify_api, notify_db, notify_db_session, sample_template):
|
||||||
|
assert Template.query.count() == 1
|
||||||
|
delete_model_template(sample_template)
|
||||||
|
assert Template.query.count() == 0
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import json
|
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 app.dao.services_dao import save_model_service
|
||||||
from tests.app.conftest import sample_user as create_sample_user
|
from tests.app.conftest import sample_user as create_sample_user
|
||||||
from flask import url_for
|
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')])
|
headers=[('Content-Type', 'application/json')])
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
assert Service.query.count() == 1
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user