diff --git a/app/main/views/notifications.py b/app/main/views/notifications.py index 70978931d..81f72343c 100644 --- a/app/main/views/notifications.py +++ b/app/main/views/notifications.py @@ -14,6 +14,7 @@ from app import ( current_service ) from app.main import main +from app.template_previews import TemplatePreview from app.utils import ( user_has_permissions, get_help_argument, @@ -41,20 +42,18 @@ def get_status_arg(filter_args): return REQUESTED_STATUSES -@main.route("/services//notification/") +@main.route("/services//notification/") @login_required @user_has_permissions('view_activity', admin_override=True) def view_notification(service_id, notification_id): - notification = notification_api_client.get_notification(service_id, notification_id) + notification = notification_api_client.get_notification(service_id, str(notification_id)) template = get_template( notification['template'], current_service, letter_preview_url=url_for( - '.view_template_version_preview', + '.view_letter_notification_as_image', service_id=service_id, - template_id=notification['template']['id'], - version=notification['template_version'], - filetype='png', + notification_id=notification_id, ), show_recipient=True, redact_missing_personalisation=True, @@ -86,6 +85,28 @@ def view_notification(service_id, notification_id): ) +@main.route("/services//notification/.png") +@login_required +@user_has_permissions('view_activity', admin_override=True) +def view_letter_notification_as_image(service_id, notification_id): + + notification = notification_api_client.get_notification(service_id, notification_id) + + template = get_template( + notification['template'], + current_service, + letter_preview_url=url_for( + '.view_letter_notification_as_image', + service_id=service_id, + notification_id=notification_id, + ), + ) + + template.values = notification['personalisation'] + + return TemplatePreview.from_utils_template(template, 'png', page=request.args.get('page')) + + @main.route("/services//notification/.json") @user_has_permissions('view_activity', admin_override=True) def view_notification_updates(service_id, notification_id): diff --git a/tests/app/main/views/test_notifications.py b/tests/app/main/views/test_notifications.py index 0233f3117..c3edc957f 100644 --- a/tests/app/main/views/test_notifications.py +++ b/tests/app/main/views/test_notifications.py @@ -1,4 +1,5 @@ from freezegun import freeze_time +from flask import url_for import pytest from app.utils import ( @@ -8,6 +9,7 @@ from app.utils import ( DELIVERED_STATUSES, ) +from notifications_utils.template import LetterImageTemplate from tests.conftest import mock_get_notification, SERVICE_ONE_ID, normalize_spaces @@ -134,3 +136,28 @@ def test_notification_page_shows_status_of_letter_notification( 'Estimated delivery date: 6 January' ) assert page.select('p.notification-status') == [] + + +def test_should_show_image_of_letter_notification( + logged_in_client, + fake_uuid, + mocker +): + + mock_get_notification(mocker, fake_uuid, template_type='letter') + + mocked_preview = mocker.patch( + 'app.main.views.templates.TemplatePreview.from_utils_template', + return_value='foo' + ) + + response = logged_in_client.get(url_for( + 'main.view_letter_notification_as_image', + service_id=SERVICE_ONE_ID, + notification_id=fake_uuid, + )) + + assert response.status_code == 200 + assert response.get_data(as_text=True) == 'foo' + assert isinstance(mocked_preview.call_args[0][0], LetterImageTemplate) + assert mocked_preview.call_args[0][1] == 'png' diff --git a/tests/conftest.py b/tests/conftest.py index bc5739459..e5c9f2256 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1703,6 +1703,7 @@ def mock_get_notification( service_id, '5407f4db-51c7-4150-8758-35412d42186a', content='hello ((name))', + subject='blah', redact_personalisation=redact_personalisation, type_=template_type, )