mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-25 16:53:42 -04:00
add tests for redact_template rest
This commit is contained in:
@@ -19,6 +19,7 @@ from app.models import (
|
|||||||
ApiKey,
|
ApiKey,
|
||||||
Template,
|
Template,
|
||||||
TemplateHistory,
|
TemplateHistory,
|
||||||
|
TemplateRedacted,
|
||||||
Job,
|
Job,
|
||||||
NotificationHistory,
|
NotificationHistory,
|
||||||
Notification,
|
Notification,
|
||||||
@@ -218,6 +219,7 @@ def delete_service_and_all_associated_db_objects(service):
|
|||||||
_delete_commit(Job.query.filter_by(service=service))
|
_delete_commit(Job.query.filter_by(service=service))
|
||||||
_delete_commit(NotificationHistory.query.filter_by(service=service))
|
_delete_commit(NotificationHistory.query.filter_by(service=service))
|
||||||
_delete_commit(Notification.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(Template.query.filter_by(service=service))
|
||||||
_delete_commit(TemplateHistory.query.filter_by(service_id=service.id))
|
_delete_commit(TemplateHistory.query.filter_by(service_id=service.id))
|
||||||
_delete_commit(ServicePermission.query.filter_by(service_id=service.id))
|
_delete_commit(ServicePermission.query.filter_by(service_id=service.id))
|
||||||
|
|||||||
@@ -61,8 +61,10 @@ def update_template(service_id, template_id):
|
|||||||
# if redacting, don't update anything else
|
# if redacting, don't update anything else
|
||||||
if data.get('redact_personalisation') is True and 'updated_by_id' in data:
|
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.
|
# 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'])
|
# also, only
|
||||||
return '', 200
|
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())
|
current_data = dict(template_schema.dump(fetched_template).data.items())
|
||||||
updated_template = dict(template_schema.dump(fetched_template).data.items())
|
updated_template = dict(template_schema.dump(fetched_template).data.items())
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
|
import uuid
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from app.models import Template
|
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 import create_authorization_header
|
||||||
from tests.app.conftest import sample_template as create_sample_template
|
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', [
|
@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)
|
template = dao_get_template_by_id(sample_template.id)
|
||||||
assert template.process_type == 'priority'
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user