add redact to notification with template schema.

So that when the admin gets notifications, the template they return
also has a "redact_personalisation" boolean attached to it. Note, it
won't do the redacting on the api - that'll be part of the admin.

Under the hood, this uses an association_proxy, which is essentially
black magic. But it proxies the `redact_personalisation` property of
`TemplateRedacted` onto the `Template` object, so that Marshmallow
can pick it up.

Note: NOT currently added to NotificationWithTemplateHistory
This commit is contained in:
Leo Hemsted
2017-06-28 16:10:22 +01:00
parent 29fc81090e
commit bd71ee9d02
3 changed files with 44 additions and 2 deletions

View File

@@ -443,6 +443,8 @@ class Template(db.Model):
default=NORMAL
)
redact_personalisation = association_proxy('template_redacted', 'redact_personalisation')
def get_link(self):
# TODO: use "/v2/" route once available
return url_for(

View File

@@ -297,6 +297,11 @@ class BaseTemplateSchema(BaseSchema):
strict = True
class TemplateRedactedSchema(BaseSchema):
class Meta:
model = models.TemplateRedacted
class TemplateSchema(BaseTemplateSchema):
created_by = field_for(models.Template, 'created_by', required=True)
@@ -440,7 +445,7 @@ class NotificationWithTemplateSchema(BaseSchema):
template = fields.Nested(
TemplateSchema,
only=['id', 'version', 'name', 'template_type', 'content', 'subject'],
only=['id', 'version', 'name', 'template_type', 'content', 'subject', 'redact_personalisation'],
dump_only=True
)
job = fields.Nested(JobSchema, only=["id", "original_file_name"], dump_only=True)

View File

@@ -11,6 +11,7 @@ from freezegun import freeze_time
from app import encryption
from app.dao.users_dao import save_model_user
from app.dao.services_dao import dao_remove_user_from_service
from app.dao.templates_dao import dao_redact_template
from app.models import (
User, Organisation, Rate, Service, ServicePermission, Notification,
DVLA_ORG_LAND_REGISTRY,
@@ -19,7 +20,7 @@ from app.models import (
)
from tests import create_authorization_header
from tests.app.db import create_template, create_service_inbound_api, create_user
from tests.app.db import create_template, create_service_inbound_api, create_user, create_notification
from tests.app.conftest import (
sample_service as create_service,
sample_user_service_permission as create_user_service_permission,
@@ -2263,3 +2264,37 @@ def test_send_one_off_notification(admin_request, sample_template, mocker):
noti = Notification.query.one()
assert response['id'] == str(noti.id)
def test_get_notification_for_service_includes_template_redacted(admin_request, sample_notification):
resp = admin_request.get(
'service.get_notification_for_service',
service_id=sample_notification.service_id,
notification_id=sample_notification.id
)
assert resp['id'] == str(sample_notification.id)
assert resp['template']['redact_personalisation'] is False
def test_get_all_notifications_for_service_includes_template_redacted(admin_request, sample_service):
normal_template = create_template(sample_service)
redacted_template = create_template(sample_service)
dao_redact_template(redacted_template, sample_service.created_by_id)
with freeze_time('2000-01-01'):
redacted_noti = create_notification(redacted_template)
with freeze_time('2000-01-02'):
normal_noti = create_notification(normal_template)
resp = admin_request.get(
'service.get_all_notifications_for_service',
service_id=sample_service.id
)
assert resp['notifications'][0]['id'] == str(normal_noti.id)
assert resp['notifications'][0]['template']['redact_personalisation'] is False
assert resp['notifications'][1]['id'] == str(redacted_noti.id)
assert resp['notifications'][1]['template']['redact_personalisation'] is True