Undo bug for previewing precompiled letters

https://github.com/alphagov/notifications-api/pull/2742/files#diff-d9a6761afaff4a491e60b64f6063654fL265
introduced a change in behaviour which causes template preview to 500
for a precompiled letter that is invalid but not because the content is
outside the printable area.

The changes it back to the previous behaviour, where we b64encode and
utf-8 decode for a page that is invalid for reasons that are not content
outside the printable area, for example a non portrait a4 letter.

I didn't end up writing a unit test for this because I really can't
figure out the existing behaviour we expect for this use case. This
method is too big and complex but I'm confident by looking at the change
in the commit mentioned above that this puts behaviour back to how it
was. Manual testing also confirms the fix.

I've also taken this opportunity to improve two test names whilst I was
looking at things.
This commit is contained in:
David McDonald
2020-03-11 14:10:48 +00:00
parent 5902960516
commit 483c00b3a2
2 changed files with 8 additions and 3 deletions

View File

@@ -263,7 +263,10 @@ 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 page_is_in_invalid_pages else base64.b64encode(pdf_page).decode('utf-8')
if content_outside_printable_area and page_is_in_invalid_pages:
content = pdf_page
else:
content = 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(