From e657958af492ac32117ba7b51d6cc2a5446fd3a7 Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Fri, 22 Jan 2016 10:44:34 +0000 Subject: [PATCH] Templates fix with tests working. --- app/dao/templates_dao.py | 2 +- app/service/rest.py | 20 +++++++++++++- tests/app/service/test_rest.py | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/app/dao/templates_dao.py b/app/dao/templates_dao.py index fa5ce2270..b6b7f6a8d 100644 --- a/app/dao/templates_dao.py +++ b/app/dao/templates_dao.py @@ -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() diff --git a/app/service/rest.py b/app/service/rest.py index eddab42bb..775410797 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -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('//template/', methods=['GET']) +@service.route('//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) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index f5d00a7e5..e12853a14 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -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 '/' 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