From b28474d74c91fe259e477a7ea363ea94b7bf49f4 Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Mon, 9 May 2016 15:59:34 +0100 Subject: [PATCH] Get all versions for a template endpoint added. --- app/dao/templates_dao.py | 7 ++++++- app/template/rest.py | 14 +++++++++++-- tests/app/template/test_rest_history.py | 27 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/app/dao/templates_dao.py b/app/dao/templates_dao.py index 9efee89b9..794754b82 100644 --- a/app/dao/templates_dao.py +++ b/app/dao/templates_dao.py @@ -1,7 +1,7 @@ import uuid from app import db from app.models import (Template, Service) -from sqlalchemy import asc +from sqlalchemy import (asc, desc) from app.dao.dao_utils import ( transactional, @@ -45,3 +45,8 @@ def dao_get_all_templates_for_service(service_id): ).order_by( asc(Template.updated_at), asc(Template.created_at) ).all() + + +def dao_get_template_versions(service_id, template_id): + history_model = Template.get_history_model() + return history_model.query.filter_by(service_id=service_id, id=template_id).order_by(desc(history_model.version)) diff --git a/app/template/rest.py b/app/template/rest.py index 69d668577..17e457dee 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -11,7 +11,8 @@ from app.dao.templates_dao import ( dao_update_template, dao_create_template, dao_get_template_by_id_and_service_id, - dao_get_all_templates_for_service + dao_get_all_templates_for_service, + dao_get_template_versions ) from notifications_utils.template import Template from app.dao.services_dao import dao_fetch_service_by_id @@ -105,7 +106,16 @@ def get_template_version(service_id, template_id, version): ) data, errors = template_history_schema.dump(fetched_template) if errors: - return json_resp(result=error, message=errors), 400 + return json_resp(result='error', message=errors), 400 + return jsonify(data=data) + + +@template.route('//version') +def get_template_versions(service_id, template_id): + fetched_templates = dao_get_template_versions(service_id, template_id) + data, errors = template_history_schema.dump(fetched_templates, many=True) + if errors: + return json_resp(result='error', message=errors), 400 return jsonify(data=data) diff --git a/tests/app/template/test_rest_history.py b/tests/app/template/test_rest_history.py index 8375859c5..c7a62e7ec 100644 --- a/tests/app/template/test_rest_history.py +++ b/tests/app/template/test_rest_history.py @@ -62,3 +62,30 @@ def test_404_missing_template_version(notify_api, sample_template): headers=[('Content-Type', 'application/json'), auth_header] ) assert resp.status_code == 404 + + +def test_all_versions_of_template(notify_api, sample_template): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + old_content = sample_template.content + newer_content = "Newer content" + newest_content = "Newest content" + sample_template.content = newer_content + dao_update_template(sample_template) + sample_template.content = newest_content + dao_update_template(sample_template) + auth_header = create_authorization_header() + endpoint = url_for( + 'template.get_template_versions', + service_id=sample_template.service.id, + template_id=sample_template.id + ) + resp = client.get( + endpoint, + headers=[('Content-Type', 'application/json'), auth_header] + ) + json_resp = json.loads(resp.get_data(as_text=True)) + assert len(json_resp['data']) == 3 + assert json_resp['data'][0]['content'] == newest_content + assert json_resp['data'][1]['content'] == newer_content + assert json_resp['data'][2]['content'] == old_content