mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 17:45:41 -04:00
Merge pull request #2437 from alphagov/precompiled-overlay-failed-validation
Precompiled request preview with overlay when validation fails
This commit is contained in:
@@ -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(
|
||||||
|
|||||||
@@ -235,13 +235,22 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
|
|||||||
)
|
)
|
||||||
|
|
||||||
content = base64.b64encode(pdf_file).decode('utf-8')
|
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':
|
if file_type == 'png':
|
||||||
try:
|
try:
|
||||||
page_number = page if page else "1"
|
|
||||||
|
|
||||||
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 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:
|
except PdfReadError as e:
|
||||||
raise InvalidRequest(
|
raise InvalidRequest(
|
||||||
'Error extracting requested page from PDF file for notification_id {} type {} {}'.format(
|
'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
|
status_code=500
|
||||||
)
|
)
|
||||||
|
|
||||||
url = '{}/precompiled-preview.png{}'.format(
|
if path:
|
||||||
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
url = current_app.config['TEMPLATE_PREVIEW_API_HOST'] + path
|
||||||
'?hide_notify=true' if page_number == '1' else ''
|
response_content = _get_png_preview_or_overlaid_pdf(url, content, notification.id, json=False)
|
||||||
)
|
else:
|
||||||
|
response_content = content
|
||||||
content = _get_png_preview(url, content, notification.id, json=False)
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
template_for_letter_print = {
|
template_for_letter_print = {
|
||||||
@@ -279,12 +287,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)
|
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:
|
if json:
|
||||||
resp = requests_post(
|
resp = requests_post(
|
||||||
url,
|
url,
|
||||||
|
|||||||
@@ -1098,12 +1098,23 @@ def test_preview_letter_template_precompiled_s3_error(
|
|||||||
"when calling the GetObject operation: Unauthorized".format(notification.id)
|
"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,
|
notify_api,
|
||||||
client,
|
client,
|
||||||
admin_request,
|
admin_request,
|
||||||
sample_service,
|
sample_service,
|
||||||
mocker
|
mocker,
|
||||||
|
filetype,
|
||||||
|
post_url,
|
||||||
|
overlay
|
||||||
):
|
):
|
||||||
|
|
||||||
template = create_template(sample_service,
|
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:
|
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)
|
||||||
|
|
||||||
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content)
|
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content)
|
||||||
|
|
||||||
mock_post = request_mock.post(
|
mock_post = request_mock.post(
|
||||||
'http://localhost/notifications-template-preview/precompiled-preview.png',
|
'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
|
||||||
)
|
)
|
||||||
@@ -1138,13 +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,
|
||||||
)
|
)
|
||||||
|
|
||||||
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', [
|
||||||
@@ -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.get_letter_pdf', return_value=pdf_content)
|
||||||
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=png_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(
|
admin_request.get(
|
||||||
'template.preview_letter_template_by_notification_id',
|
'template.preview_letter_template_by_notification_id',
|
||||||
|
|||||||
Reference in New Issue
Block a user