mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-16 10:12:32 -05:00
Templates fix with tests working.
This commit is contained in:
@@ -30,5 +30,5 @@ def get_model_templates(template_id=None, service_id=None):
|
||||
elif template_id:
|
||||
return Template.query.filter_by(id=template_id).one()
|
||||
elif service_id:
|
||||
return Template.query.filter_by(service=Service.get(service_id)).one()
|
||||
return Template.query.filter_by(service=Service.query.get(service_id)).all()
|
||||
return Template.query.all()
|
||||
|
||||
@@ -13,7 +13,7 @@ from app.dao.templates_dao import (
|
||||
from app.dao.api_key_dao import (save_model_api_key, get_model_api_keys, get_unsigned_secret)
|
||||
from app.models import ApiKey
|
||||
from app.schemas import (
|
||||
services_schema, service_schema, template_schema, api_keys_schema)
|
||||
services_schema, service_schema, template_schema, templates_schema, api_keys_schema)
|
||||
|
||||
from flask import Blueprint
|
||||
service = Blueprint('service', __name__)
|
||||
@@ -183,3 +183,21 @@ def update_template(service_id, template_id):
|
||||
except DAOException as e:
|
||||
return jsonify(result="error", message=str(e)), 400
|
||||
return jsonify(data=template_schema.dump(template).data), status_code
|
||||
|
||||
|
||||
@service.route('/<int:service_id>/template/<int:template_id>', methods=['GET'])
|
||||
@service.route('/<int:service_id>/template', methods=['GET'])
|
||||
def get_service_template(service_id, template_id=None):
|
||||
try:
|
||||
templates = get_model_templates(service_id=service_id, template_id=template_id)
|
||||
except DataError:
|
||||
return jsonify(result="error", message="Invalid template id"), 400
|
||||
except NoResultFound:
|
||||
return jsonify(result="error", message="Template not found"), 404
|
||||
if isinstance(templates, list):
|
||||
data, errors = templates_schema.dump(templates)
|
||||
else:
|
||||
data, errors = template_schema.dump(templates)
|
||||
if errors:
|
||||
return jsonify(result="error", message=str(errors))
|
||||
return jsonify(data=data)
|
||||
|
||||
@@ -547,3 +547,51 @@ def test_create_template_unicode_content(notify_api, notify_db, notify_db_sessio
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user