def test_notifification_page_shows_error_message_if_precompiled_letter_cannot_be_opened( client_request, mocker, fake_uuid, ): mock_get_notification( mocker, fake_uuid, notification_status='validation-failed', template_type='letter', is_precompiled_letter=True, ) mocker.patch( 'app.main.views.notifications.view_letter_notification_as_preview', side_effect=PdfReadError()) mocker.patch('app.main.views.notifications.pdf_page_count', side_effect=PdfReadError()) page = client_request.get( 'main.view_notification', service_id=SERVICE_ONE_ID, notification_id=fake_uuid, ) error_message = page.find('p', class_='notification-status-cancelled').text assert normalize_spaces( error_message ) == "Validation failed – Notify cannot read this PDF file"
def test_notifification_page_shows_error_message_if_precompiled_letter_cannot_be_opened( client_request, mocker, fake_uuid, ): mock_get_notification( mocker, fake_uuid, notification_status="validation-failed", template_type="letter", is_precompiled_letter=True, ) mocker.patch( "app.main.views.notifications.view_letter_notification_as_preview", side_effect=PdfReadError(), ) mocker.patch("app.main.views.notifications.pdf_page_count", side_effect=PdfReadError()) page = client_request.get( "main.view_notification", service_id=SERVICE_ONE_ID, notification_id=fake_uuid, ) error_message = page.find("p", class_="notification-status-cancelled").text assert normalize_spaces( error_message ) == "Validation failed – this isn’t a PDF file that Notification can read"
def test_notification_page_shows_error_message_if_precompiled_letter_cannot_be_opened( client_request, mocker, fake_uuid, ): notification = create_notification( notification_status='validation-failed', template_type='letter', is_precompiled_letter=True) mocker.patch('app.notification_api_client.get_notification', return_value=notification) mocker.patch( 'app.main.views.notifications.get_letter_file_data', side_effect=PdfReadError() ) mocker.patch( 'app.main.views.notifications.pdf_page_count', side_effect=PdfReadError() ) page = client_request.get( 'main.view_notification', service_id=SERVICE_ONE_ID, notification_id=fake_uuid, ) error_message = page.find('p', class_='notification-status-cancelled').text assert normalize_spaces(error_message) == \ "Validation failed – There’s a problem with your letter. Notify cannot read this PDF."
def test_merge_pdf_with_another_pdf_read_error(self, merger_mock): merger_mock.append.side_effect = PdfReadError("Another error") with self.assertRaises(PdfReadError): merge_pdf(merger_mock, BytesIO(b'test')) assert merger_mock.append.call_count == 1
def test_merge_pdf_without_EOF_maker(self, merger_mock): merger_mock.append.side_effect = PdfReadError("EOF marker not found") with self.assertRaises(PdfReadError): merge_pdf(merger_mock, BytesIO(b'test')) assert merger_mock.append.call_count == 2
def _buildOutline(self, node): """ Override the method to avoid exception PdfReadError if '/Dest' == '/__WKANCHOR_2' :param self: :param node: :return: """ dest, title, outline = None, None, None if "/A" in node and "/Title" in node: # Action, section 8.5 (only type GoTo supported) title = node["/Title"] action = node["/A"] if action["/S"] == "/GoTo": dest = action["/D"] elif "/Dest" in node and "/Title" in node: # Destination, section 8.2.1 title = node["/Title"] dest = node["/Dest"] if dest == '/__WKANCHOR_2': return None # if destination found, then create outline if dest: if isinstance(dest, ArrayObject): outline = self._buildDestination(title, dest) elif isString(dest) and dest in self._namedDests: outline = self._namedDests[dest] outline[NameObject("/Title")] = title else: raise PdfReadError("Unexpected destination %r" % dest) return outline
def test_preview_letter_template_precompiled_png_template_preview_pdf_error( notify_api, client, admin_request, sample_service, mocker ): 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' png_content = b'\x00\x02' mocker.patch('app.template.rest.get_letter_pdf', return_value=pdf_content) error_message = "PDF Error message" mocker.patch('app.template.rest.extract_page_from_pdf', side_effect=PdfReadError(error_message)) request_mock.post( 'http://localhost/notifications-template-preview/precompiled-preview.png', content=png_content, headers={'X-pdf-page-count': '1'}, status_code=404 ) request = admin_request.get( 'template.preview_letter_template_by_notification_id', service_id=notification.service_id, notification_id=notification.id, file_type='png', _expected_status=500 ) assert request['message'] == "Error extracting requested page from PDF file for notification_id {} type " \ "{} {}".format(notification.id, type(PdfReadError()), error_message)
def pdf_page_count(src_pdf): """ Returns number of pages in a pdf file :param PyPDF2.PdfFileReader src_pdf: A File object or an object that supports the standard read and seek methods """ try: pdf = PyPDF2.PdfFileReader(src_pdf) except AttributeError as e: raise PdfReadError("Could not open PDF file, stream is null", e) return pdf.numPages
def extract_page_from_pdf(src_pdf, page_number): """ Retrieves a new PDF document with the page extracted from the source PDF file. :param src_pdf: File object or an object that supports the standard read and seek methods similar to a File object. :param page_number: The page number to retrieve (pages begin at zero) """ pdf = PyPDF2.PdfFileReader(src_pdf) if pdf.numPages < page_number: raise PdfReadError( "Page number requested: {} of {} does not exist in document". format(str(page_number), str(pdf.numPages))) writer = PdfFileWriter() writer.addPage(pdf.getPage(page_number)) pdf_bytes = io.BytesIO() writer.write(pdf_bytes) pdf_bytes.seek(0) return pdf_bytes.read()