From 5ffd7c444855e7eed3ea9c59c5956300212d28e7 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 22 Aug 2017 11:36:10 +0100 Subject: [PATCH] =?UTF-8?q?Add=20platform=E2=80=93admin-only=20page=20to?= =?UTF-8?q?=20preview=20DVLA=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Debugging the DVLA markup stuff is quite slow, because it’s split between the API and utils, and things that work in tests don’t always work with real template content. This commit adds us a private endpoint that will let us see what DVLA markup will be generated for any template, enabling a quicker feedback loop. --- app/main/views/templates.py | 21 +++++++++++ tests/app/main/views/test_templates.py | 48 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/app/main/views/templates.py b/app/main/views/templates.py index 1871431eb..99831f0b8 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -9,12 +9,14 @@ from flask import ( flash, abort, json, + Response, ) from flask_login import login_required, current_user from dateutil.parser import parse from notifications_utils.formatters import escape_html from notifications_utils.recipients import first_column_headings +from notifications_utils.template import LetterDVLATemplate from notifications_python_client.errors import HTTPError from app.main import main @@ -146,6 +148,25 @@ def choose_template(service_id, template_type='all'): ) +@main.route("/services//templates//as-dvla") +@login_required +@user_has_permissions(admin_override=True) +def view_template_as_dvla_markup(service_id, template_id): + + template = service_api_client.get_service_template(service_id, str(template_id))['data'] + + if template['template_type'] != 'letter': + abort(404) + + return Response( + str(LetterDVLATemplate( + template, + notification_reference=1, + )).replace('', '\n'), + mimetype='text/plain', + ) + + @main.route("/services//templates/.") @login_required @user_has_permissions('view_activity', admin_override=True) diff --git a/tests/app/main/views/test_templates.py b/tests/app/main/views/test_templates.py index 2140787a2..09bf6a499 100644 --- a/tests/app/main/views/test_templates.py +++ b/tests/app/main/views/test_templates.py @@ -14,6 +14,9 @@ from tests.conftest import ( mock_get_service_template, normalize_spaces, SERVICE_ONE_ID, + active_user_with_permissions, + platform_admin_user, + mock_get_user, ) from tests import validate_route_permission, template_json, single_notification_json @@ -1163,3 +1166,48 @@ def test_should_show_hint_once_template_redacted( ) assert page.select('.hint')[0].text == 'Personalisation is hidden after sending' + + +@pytest.mark.parametrize('test_user, template_mock, expected_response_code', [ + ( + active_user_with_permissions, + mock_get_service_letter_template, + 403 + ), + ( + platform_admin_user, + mock_get_service_template, + 404 + ), + ( + platform_admin_user, + mock_get_service_email_template, + 404 + ), + ( + platform_admin_user, + mock_get_service_letter_template, + 200 + ), +]) +def test_should_show_letter_template_as_dvla_markup( + client, + mocker, + service_one, + fake_uuid, + mock_get_service, + test_user, + template_mock, + expected_response_code, +): + + client.login(test_user(fake_uuid), mocker, service_one) + template_mock(mocker) + + response = client.get(url_for( + '.view_template_as_dvla_markup', + service_id=SERVICE_ONE_ID, + template_id=fake_uuid, + )) + + assert response.status_code == expected_response_code