Merge pull request #2437 from alphagov/precompiled-overlay-failed-validation

Precompiled request preview with overlay when validation fails
This commit is contained in:
Pea (Malgorzata Tyczynska)
2019-04-12 16:14:56 +01:00
committed by GitHub
3 changed files with 40 additions and 21 deletions

View File

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

View File

@@ -235,13 +235,22 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
)
content = base64.b64encode(pdf_file).decode('utf-8')
overlay = request.args.get('overlay')
page_number = page if page else "1"
if overlay:
path = '/precompiled/overlay.{}'.format(file_type)
content = pdf_file
elif file_type == 'png':
query_string = '?hide_notify=true' if page_number == '1' else ''
path = '/precompiled-preview.png' + query_string
else:
path = None
if file_type == 'png':
try:
page_number = page if page else "1"
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1)
content = base64.b64encode(pdf_page).decode('utf-8')
content = pdf_page if overlay else base64.b64encode(pdf_page).decode('utf-8')
except PdfReadError as e:
raise InvalidRequest(
'Error extracting requested page from PDF file for notification_id {} type {} {}'.format(
@@ -249,12 +258,11 @@ 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 ''
)
content = _get_png_preview(url, content, notification.id, json=False)
if path:
url = current_app.config['TEMPLATE_PREVIEW_API_HOST'] + path
response_content = _get_png_preview_or_overlaid_pdf(url, content, notification.id, json=False)
else:
response_content = content
else:
template_for_letter_print = {
@@ -279,12 +287,12 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
file_type,
'?page={}'.format(page) if page else ''
)
content = _get_png_preview(url, data, notification.id, json=True)
response_content = _get_png_preview_or_overlaid_pdf(url, data, notification.id, json=True)
return jsonify({"content": content})
return jsonify({"content": response_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:
resp = requests_post(
url,

View File

@@ -1098,12 +1098,23 @@ def test_preview_letter_template_precompiled_s3_error(
"when calling the GetObject operation: Unauthorized".format(notification.id)
def test_preview_letter_template_precompiled_png_file_type(
@pytest.mark.parametrize(
"filetype, post_url, overlay",
[
('png', 'precompiled-preview.png', None),
('png', 'precompiled/overlay.png', 1),
('pdf', 'precompiled/overlay.pdf', 1)
]
)
def test_preview_letter_template_precompiled_png_file_type_or_pdf_with_overlay(
notify_api,
client,
admin_request,
sample_service,
mocker
mocker,
filetype,
post_url,
overlay
):
template = create_template(sample_service,
@@ -1121,15 +1132,15 @@ def test_preview_letter_template_precompiled_png_file_type(
with requests_mock.Mocker() as request_mock:
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)
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',
content=png_content,
'http://localhost/notifications-template-preview/{}'.format(post_url),
content=expected_returned_content,
headers={'X-pdf-page-count': '1'},
status_code=200
)
@@ -1138,13 +1149,14 @@ 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=filetype,
overlay=overlay,
)
with pytest.raises(ValueError):
mock_post.last_request.json()
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', [
@@ -1180,7 +1192,7 @@ def test_preview_letter_template_precompiled_png_file_type_hide_notify_tag_only_
mocker.patch('app.template.rest.get_letter_pdf', return_value=pdf_content)
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=png_content)
mock_get_png_preview = mocker.patch('app.template.rest._get_png_preview', return_value=encoded)
mock_get_png_preview = mocker.patch('app.template.rest._get_png_preview_or_overlaid_pdf', return_value=encoded)
admin_request.get(
'template.preview_letter_template_by_notification_id',