From 2f52a8fd64f1418dbc246c57aa38080ebcd15ad4 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Thu, 4 Apr 2019 12:07:08 +0100 Subject: [PATCH] Call overlaid preview for precompiled letters that fail validation Also test precompiled letter preview with overlay --- app/template/rest.py | 16 ++++++++++++---- tests/app/template/test_rest.py | 13 ++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/template/rest.py b/app/template/rest.py index ac4bc9a64..be09cb74e 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -249,10 +249,18 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file status_code=500 ) - url = '{}/precompiled-preview.png{}'.format( - current_app.config['TEMPLATE_PREVIEW_API_HOST'], - '?hide_notify=true' if page_number == '1' else '' - ) + if request.args.get('overlay'): + content = pdf_page + url = '{}/precompiled/overlay.png{}{}'.format( + current_app.config['TEMPLATE_PREVIEW_API_HOST'], + '?invert=1', + '&hide_notify=true' if page_number == '1' else '' + ) + else: + url = '{}/precompiled-preview.png{}'.format( + current_app.config['TEMPLATE_PREVIEW_API_HOST'], + '?hide_notify=true' if page_number == '1' else '' + ) content = _get_png_preview(url, content, notification.id, json=False) else: diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 2963bd3ec..030ac6674 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -1098,12 +1098,18 @@ def test_preview_letter_template_precompiled_s3_error( "when calling the GetObject operation: Unauthorized".format(notification.id) +@pytest.mark.parametrize( + "post_url, overlay", + [('precompiled-preview.png', None), ('precompiled/overlay.png?invert=1', 1)] +) def test_preview_letter_template_precompiled_png_file_type( notify_api, client, admin_request, sample_service, - mocker + mocker, + post_url, + overlay ): template = create_template(sample_service, @@ -1128,7 +1134,7 @@ def test_preview_letter_template_precompiled_png_file_type( mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content) mock_post = request_mock.post( - 'http://localhost/notifications-template-preview/precompiled-preview.png', + 'http://localhost/notifications-template-preview/{}'.format(post_url), content=png_content, headers={'X-pdf-page-count': '1'}, status_code=200 @@ -1138,7 +1144,8 @@ def test_preview_letter_template_precompiled_png_file_type( 'template.preview_letter_template_by_notification_id', service_id=notification.service_id, notification_id=notification.id, - file_type='png' + file_type='png', + overlay=overlay, ) with pytest.raises(ValueError):