From 8ad10261ec86519a66fe4ea898be7d6c32c3fb26 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 28 Jun 2017 16:49:43 +0100 Subject: [PATCH] add tests for redact_template rest --- app/dao/services_dao.py | 2 + app/template/rest.py | 6 ++- tests/app/template/test_rest.py | 92 ++++++++++++++++++++++++++++++++- 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 24c95567a..282e9e9f0 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -19,6 +19,7 @@ from app.models import ( ApiKey, Template, TemplateHistory, + TemplateRedacted, Job, NotificationHistory, Notification, @@ -218,6 +219,7 @@ def delete_service_and_all_associated_db_objects(service): _delete_commit(Job.query.filter_by(service=service)) _delete_commit(NotificationHistory.query.filter_by(service=service)) _delete_commit(Notification.query.filter_by(service=service)) + _delete_commit(TemplateRedacted.query.filter_by(service=service)) _delete_commit(Template.query.filter_by(service=service)) _delete_commit(TemplateHistory.query.filter_by(service_id=service.id)) _delete_commit(ServicePermission.query.filter_by(service_id=service.id)) diff --git a/app/template/rest.py b/app/template/rest.py index 747fb71cf..867ecad8e 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -61,8 +61,10 @@ def update_template(service_id, template_id): # if redacting, don't update anything else if data.get('redact_personalisation') is True and 'updated_by_id' in data: # we also don't need to check what was passed in redact_personalisation - its presence in the dict is enough. - dao_redact_template(fetched_template, data['updated_by_id']) - return '', 200 + # also, only + if not fetched_template.redact_personalisation: + dao_redact_template(fetched_template, data['updated_by_id']) + return 'null', 200 current_data = dict(template_schema.dump(fetched_template).data.items()) updated_template = dict(template_schema.dump(fetched_template).data.items()) diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 13e21dcfd..a8646feb8 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -1,11 +1,17 @@ +import uuid import json import random import string +from datetime import datetime, timedelta + import pytest +from freezegun import freeze_time + from app.models import Template +from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template + from tests import create_authorization_header from tests.app.conftest import sample_template as create_sample_template -from app.dao.templates_dao import dao_get_template_by_id @pytest.mark.parametrize('template_type, subject', [ @@ -539,3 +545,87 @@ def test_update_set_process_type_on_template(client, sample_template): template = dao_get_template_by_id(sample_template.id) assert template.process_type == 'priority' + + +def test_update_redact_template(admin_request, sample_template): + assert sample_template.redact_personalisation is False + + data = { + 'redact_personalisation': True, + 'updated_by_id': str(sample_template.created_by_id) + } + + dt = datetime.now() + + with freeze_time(dt): + resp = admin_request.post( + 'template.update_template', + service_id=sample_template.service_id, + template_id=sample_template.id, + _data=data + ) + + assert resp is None + + assert sample_template.redact_personalisation is True + assert sample_template.template_redacted.updated_by_id == sample_template.created_by_id + assert sample_template.template_redacted.updated_at == dt + + assert sample_template.version == 1 + + +def test_update_redact_template_ignores_other_properties(admin_request, sample_template): + data = { + 'name': 'Foo', + 'redact_personalisation': True, + 'updated_by_id': str(sample_template.created_by_id) + } + + admin_request.post( + 'template.update_template', + service_id=sample_template.service_id, + template_id=sample_template.id, + _data=data + ) + + assert sample_template.redact_personalisation is True + assert sample_template.name != 'Foo' + + +def test_update_redact_template_does_nothing_if_already_redacted(admin_request, sample_template): + dt = datetime.now() + with freeze_time(dt): + dao_redact_template(sample_template, sample_template.created_by_id) + + data = { + 'redact_personalisation': True, + 'updated_by_id': str(sample_template.created_by_id) + } + + with freeze_time(dt + timedelta(days=1)): + resp = admin_request.post( + 'template.update_template', + service_id=sample_template.service_id, + template_id=sample_template.id, + _data=data + ) + + assert resp is None + + assert sample_template.redact_personalisation is True + # make sure that it hasn't been updated + assert sample_template.template_redacted.updated_at == dt + + + +def test_update_redact_template_does_nothing_if_no_updated_by(admin_request, sample_template): + original_updated_time = sample_template.template_redacted.updated_at + admin_request.post( + 'template.update_template', + service_id=sample_template.service_id, + template_id=sample_template.id, + _data={'redact_personalisation': True} + ) + + assert sample_template.redact_personalisation is False + assert sample_template.template_redacted.updated_at == original_updated_time