Also show overlay on document download of failed letter

And of course test this new flex
This commit is contained in:
Pea Tyczynska
2019-04-04 18:38:31 +01:00
parent 2f52a8fd64
commit 63fe210202
3 changed files with 23 additions and 13 deletions

View File

@@ -136,7 +136,6 @@ def get_letter_pdf(notification):
s3 = boto3.resource('s3') s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name) bucket = s3.Bucket(bucket_name)
item = next(x for x in bucket.objects.filter(Prefix=prefix)) item = next(x for x in bucket.objects.filter(Prefix=prefix))
obj = s3.Object( obj = s3.Object(

View File

@@ -251,10 +251,9 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
if request.args.get('overlay'): if request.args.get('overlay'):
content = pdf_page content = pdf_page
url = '{}/precompiled/overlay.png{}{}'.format( url = '{}/precompiled/overlay.png{}'.format(
current_app.config['TEMPLATE_PREVIEW_API_HOST'], current_app.config['TEMPLATE_PREVIEW_API_HOST'],
'?invert=1', '?invert=1',
'&hide_notify=true' if page_number == '1' else ''
) )
else: else:
url = '{}/precompiled-preview.png{}'.format( url = '{}/precompiled-preview.png{}'.format(
@@ -262,7 +261,14 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
'?hide_notify=true' if page_number == '1' else '' '?hide_notify=true' if page_number == '1' else ''
) )
content = _get_png_preview(url, content, notification.id, json=False) content = _get_png_preview_or_overlaid_pdf(url, content, notification.id, json=False)
elif file_type == 'pdf' and request.args.get('overlay'):
content = pdf_file
url = '{}/precompiled/overlay.pdf{}'.format(
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
'?invert=1',
)
content = _get_png_preview_or_overlaid_pdf(url, content, notification.id, json=False)
else: else:
template_for_letter_print = { template_for_letter_print = {
@@ -287,12 +293,12 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
file_type, file_type,
'?page={}'.format(page) if page else '' '?page={}'.format(page) if page else ''
) )
content = _get_png_preview(url, data, notification.id, json=True) content = _get_png_preview_or_overlaid_pdf(url, data, notification.id, json=True)
return jsonify({"content": content}) return jsonify({"content": content})
def _get_png_preview(url, data, notification_id, json=True): def _get_png_preview_or_overlaid_pdf(url, data, notification_id, json=True):
if json: if json:
resp = requests_post( resp = requests_post(
url, url,

View File

@@ -1099,15 +1099,20 @@ def test_preview_letter_template_precompiled_s3_error(
@pytest.mark.parametrize( @pytest.mark.parametrize(
"post_url, overlay", "filetype, post_url, overlay",
[('precompiled-preview.png', None), ('precompiled/overlay.png?invert=1', 1)] [
('png', 'precompiled-preview.png', None),
('png', 'precompiled/overlay.png?invert=1', 1),
('pdf', 'precompiled/overlay.pdf?invert=1', 1)
]
) )
def test_preview_letter_template_precompiled_png_file_type( def test_preview_letter_template_precompiled_png_file_type_or_pdf_with_overlay(
notify_api, notify_api,
client, client,
admin_request, admin_request,
sample_service, sample_service,
mocker, mocker,
filetype,
post_url, post_url,
overlay overlay
): ):
@@ -1127,7 +1132,7 @@ def test_preview_letter_template_precompiled_png_file_type(
with requests_mock.Mocker() as request_mock: with requests_mock.Mocker() as request_mock:
pdf_content = b'\x00\x01' pdf_content = b'\x00\x01'
png_content = b'\x00\x02' expected_returned_content = b'\x00\x02'
mock_get_letter_pdf = mocker.patch('app.template.rest.get_letter_pdf', return_value=pdf_content) mock_get_letter_pdf = mocker.patch('app.template.rest.get_letter_pdf', return_value=pdf_content)
@@ -1135,7 +1140,7 @@ def test_preview_letter_template_precompiled_png_file_type(
mock_post = request_mock.post( mock_post = request_mock.post(
'http://localhost/notifications-template-preview/{}'.format(post_url), 'http://localhost/notifications-template-preview/{}'.format(post_url),
content=png_content, content=expected_returned_content,
headers={'X-pdf-page-count': '1'}, headers={'X-pdf-page-count': '1'},
status_code=200 status_code=200
) )
@@ -1144,14 +1149,14 @@ def test_preview_letter_template_precompiled_png_file_type(
'template.preview_letter_template_by_notification_id', 'template.preview_letter_template_by_notification_id',
service_id=notification.service_id, service_id=notification.service_id,
notification_id=notification.id, notification_id=notification.id,
file_type='png', file_type=filetype,
overlay=overlay, overlay=overlay,
) )
with pytest.raises(ValueError): with pytest.raises(ValueError):
mock_post.last_request.json() mock_post.last_request.json()
assert mock_get_letter_pdf.called_once_with(notification) assert mock_get_letter_pdf.called_once_with(notification)
assert base64.b64decode(resp['content']) == png_content assert base64.b64decode(resp['content']) == expected_returned_content
@pytest.mark.parametrize('page_number,expect_preview_url', [ @pytest.mark.parametrize('page_number,expect_preview_url', [