When requesting a png for a single page, show overlay as appropriate

If the png page is invalid then show the overlay for that page
If the png page is not invalid, even if other pages are invalid, then
don't shown the overlay for that page

These keeps the behaviour were if you get the pdf for a pdf which has
invalid pages then the whole PDF is requested (not just a single page)
and all of the pages include the overlay

Tests have been improved to be more explicit about what we are testing
This commit is contained in:
David McDonald
2020-03-09 13:43:06 +00:00
parent 9eda30f4fd
commit d9243b8b8a
2 changed files with 88 additions and 13 deletions

View File

@@ -245,11 +245,15 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
status_code=500
)
content = base64.b64encode(pdf_file).decode('utf-8')
overlay = metadata.get("message") == "content-outside-printable-area"
page_number = page if page else "1"
content = base64.b64encode(pdf_file).decode('utf-8')
content_outside_printable_area = metadata.get("message") == "content-outside-printable-area"
if overlay:
show_overlay_for_page = False
if content_outside_printable_area and page_number in metadata.get('invalid_pages', '[]'):
show_overlay_for_page = True
if show_overlay_for_page or (content_outside_printable_area and file_type == "pdf"):
path = '/precompiled/overlay.{}'.format(file_type)
query_string = '?page_number={}'.format(page_number) if file_type == 'png' else ''
content = pdf_file
@@ -262,7 +266,7 @@ def preview_letter_template_by_notification_id(service_id, notification_id, file
if file_type == 'png':
try:
pdf_page = extract_page_from_pdf(BytesIO(pdf_file), int(page_number) - 1)
content = pdf_page if overlay else base64.b64encode(pdf_page).decode('utf-8')
content = pdf_page if show_overlay_for_page 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(

View File

@@ -1143,23 +1143,27 @@ def test_preview_letter_template_precompiled_s3_error(
@pytest.mark.parametrize(
"requested_page, filetype, message, expected_post_url",
"requested_page, message, expected_post_url",
[
("", 'png', "", 'precompiled-preview.png'),
("1", 'png', "content-outside-printable-area", 'precompiled/overlay.png?page_number=1'),
("2", 'png', "content-outside-printable-area", 'precompiled/overlay.png?page_number=2'),
("3", 'png', "content-outside-printable-area", 'precompiled/overlay.png?page_number=3'),
("", 'pdf', "content-outside-printable-area", 'precompiled/overlay.pdf')
# page defaults to 1, page is valid, no overlay shown
("", "", 'precompiled-preview.png'),
# page is valid, no overlay shown
("1", "", 'precompiled-preview.png'),
# page is invalid, overlay shown
("1", "content-outside-printable-area", 'precompiled/overlay.png?page_number=1'),
# page is valid, no overlay shown
("2", "content-outside-printable-area", 'precompiled-preview.png'),
# page is invalid, overlay shown
("3", "content-outside-printable-area", 'precompiled/overlay.png?page_number=3'),
]
)
def test_preview_letter_template_precompiled_png_file_type_or_pdf_with_overlay(
def test_preview_letter_template_precompiled_for_png_file_shows_overlay_where_appropriate(
notify_api,
client,
admin_request,
sample_service,
mocker,
requested_page,
filetype,
message,
expected_post_url,
):
@@ -1206,7 +1210,74 @@ def test_preview_letter_template_precompiled_png_file_type_or_pdf_with_overlay(
page=requested_page,
service_id=notification.service_id,
notification_id=notification.id,
file_type=filetype,
file_type="png",
)
with pytest.raises(ValueError):
mock_post.last_request.json()
assert mock_get_letter_pdf.called_once_with(notification)
assert base64.b64decode(response['content']) == expected_returned_content
assert response["metadata"] == metadata
@pytest.mark.parametrize(
"invalid_pages",
[
"[1,3]",
"[2,4]", # it shouldn't make a difference if the error was on the first page or not
]
)
def test_preview_letter_template_precompiled_for_pdf_file_shows_overlay_if_content_outside_printable_area(
notify_api,
client,
admin_request,
sample_service,
mocker,
invalid_pages,
):
template = create_template(sample_service,
template_type='letter',
template_name='Pre-compiled PDF',
subject='Pre-compiled PDF',
hidden=True)
notification = create_notification(template)
with set_config_values(notify_api, {
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
}):
with requests_mock.Mocker() as request_mock:
pdf_content = b'\x00\x01'
expected_returned_content = b'\x00\x02'
metadata = {
"message": "content-outside-printable-area",
"invalid_pages": invalid_pages,
"page_count": "4"
}
mock_get_letter_pdf = mocker.patch(
'app.template.rest.get_letter_pdf_and_metadata',
return_value=(pdf_content, metadata)
)
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content)
mock_post = request_mock.post(
'http://localhost/notifications-template-preview/precompiled/overlay.pdf',
content=expected_returned_content,
headers={'X-pdf-page-count': '4'},
status_code=200
)
response = admin_request.get(
'template.preview_letter_template_by_notification_id',
service_id=notification.service_id,
notification_id=notification.id,
file_type="pdf",
)
with pytest.raises(ValueError):