From fc1345acdf6754a534cccd50d05e8c6ba48b3afe Mon Sep 17 00:00:00 2001 From: Adam Shimali Date: Tue, 10 May 2016 12:39:35 +0100 Subject: [PATCH 1/3] Trigger dependent build for preview --- scripts/trigger-dependent-build.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/trigger-dependent-build.sh b/scripts/trigger-dependent-build.sh index 7dee89d50..cf1405dfe 100755 --- a/scripts/trigger-dependent-build.sh +++ b/scripts/trigger-dependent-build.sh @@ -13,5 +13,4 @@ # travis token # -curl -vvv -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Travis-API-Version: 3" -H "Authorization: token $auth_token" -d '{"request":{"branch":"master"}}' https://api.travis-ci.org/repo/alphagov%2Fnotifications-functional-tests/requests - +curl -vvv -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Travis-API-Version: 3" -H "Authorization: token $auth_token" -d '{"request":{"branch":"master","config":{"env":{"global":["ENVIRONMENT=preview"]}}}}' https://api.travis-ci.org/repo/alphagov%2Fnotifications-functional-tests/requests From 8f8245885e0992bfbf46bcc31cd863ff5eaa44ab Mon Sep 17 00:00:00 2001 From: Adam Shimali Date: Tue, 10 May 2016 14:15:38 +0100 Subject: [PATCH 2/3] Pass branch name to dependent build --- scripts/trigger-dependent-build.sh | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/scripts/trigger-dependent-build.sh b/scripts/trigger-dependent-build.sh index cf1405dfe..389e1c120 100755 --- a/scripts/trigger-dependent-build.sh +++ b/scripts/trigger-dependent-build.sh @@ -7,10 +7,6 @@ # - ./trigger-dependent-build # -# An authorization token generated by running: -# gem install travis -# travis login -# travis token -# +echo "Triggering dependent build for $TRAVIS_BRANCH" -curl -vvv -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Travis-API-Version: 3" -H "Authorization: token $auth_token" -d '{"request":{"branch":"master","config":{"env":{"global":["ENVIRONMENT=preview"]}}}}' https://api.travis-ci.org/repo/alphagov%2Fnotifications-functional-tests/requests +curl -vvv -s -X POST -H "Content-Type: application/json" -H "Accept: application/json" -H "Travis-API-Version: 3" -H "Authorization: token $auth_token" -d '{"request":{"branch":"master","config":{"env":{"global":["ENVIRONMENT='$TRAVIS_BRANCH'"]}}}}' https://api.travis-ci.org/repo/alphagov%2Fnotifications-functional-tests/requests From 083d3d75ae990e11e862fed47ced42c4142f812d Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Tue, 10 May 2016 14:55:59 +0100 Subject: [PATCH 3/3] Add user details to template schema dump. --- app/schemas.py | 14 ++++++++++++-- app/template/rest.py | 17 ++++++++++------- tests/app/template/test_rest_history.py | 6 +++++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/schemas.py b/app/schemas.py index d01a4e978..16bcac996 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -123,9 +123,19 @@ class TemplateSchema(BaseTemplateSchema): raise ValidationError('Invalid template subject', 'subject') -class TemplateHistorySchema(BaseTemplateSchema): +class TemplateHistorySchema(BaseSchema): - created_by = field_for(models.Template, 'created_by', required=True) + class Meta: + # Use the base model class that the history class is created from + model = models.Template + # We have to use a method here because the relationship field on the + # history object is not created. + created_by = fields.Method("populate_created_by", dump_only=True) + created_at = field_for(models.Template, 'created_at', format='%Y-%m-%d %H:%M:%S.%f') + + def populate_created_by(self, data): + usr = models.User.query.filter_by(id=data.created_by_id).one() + return {'id': str(usr.id), 'name': usr.name, 'email_address': usr.email_address} class NotificationsStatisticsSchema(BaseSchema): diff --git a/app/template/rest.py b/app/template/rest.py index 17e457dee..d520a3e06 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -99,12 +99,13 @@ def get_template_by_id_and_service_id(service_id, template_id): @template.route('//version/') def get_template_version(service_id, template_id, version): - fetched_template = dao_get_template_by_id_and_service_id( - template_id=template_id, - service_id=service_id, - version=version + data, errors = template_history_schema.dump( + dao_get_template_by_id_and_service_id( + template_id=template_id, + service_id=service_id, + version=version + ) ) - data, errors = template_history_schema.dump(fetched_template) if errors: return json_resp(result='error', message=errors), 400 return jsonify(data=data) @@ -112,8 +113,10 @@ def get_template_version(service_id, template_id, version): @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) + data, errors = template_history_schema.dump( + dao_get_template_versions(service_id, template_id), + 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 c7a62e7ec..ce3c2619d 100644 --- a/tests/app/template/test_rest_history.py +++ b/tests/app/template/test_rest_history.py @@ -1,11 +1,13 @@ import json +from datetime import (datetime, date) from flask import url_for from app.models import Template +from freezegun import freeze_time from app.dao.templates_dao import dao_update_template from tests import create_authorization_header -def test_template_history_version(notify_api, sample_template): +def test_template_history_version(notify_api, sample_user, sample_template): with notify_api.test_request_context(): with notify_api.test_client() as client: auth_header = create_authorization_header() @@ -23,6 +25,8 @@ def test_template_history_version(notify_api, sample_template): assert json_resp['data']['id'] == str(sample_template.id) assert json_resp['data']['content'] == sample_template.content assert json_resp['data']['version'] == 1 + assert json_resp['data']['created_by']['name'] == sample_user.name + assert datetime.strptime(json_resp['data']['created_at'], '%Y-%m-%d %H:%M:%S.%f').date() == date.today() def test_previous_template_history_version(notify_api, sample_template):