diff --git a/src/paperless_mail/tests/samples/sample.html.pdf.webp b/src/paperless_mail/tests/samples/sample.html.pdf.webp new file mode 100644 index 000000000..534157660 Binary files /dev/null and b/src/paperless_mail/tests/samples/sample.html.pdf.webp differ diff --git a/src/paperless_mail/tests/test_parsers.py b/src/paperless_mail/tests/test_parsers.py index a0c19e6f5..2bc0fbdad 100644 --- a/src/paperless_mail/tests/test_parsers.py +++ b/src/paperless_mail/tests/test_parsers.py @@ -268,7 +268,7 @@ class TestParser(TestCase): @mock.patch("documents.loggers.LoggingMixin.log") # Disable log output def test_tika_parse(self, m): html = '
Some Text
' - expected_text = "\n\n\n\n\n\n\n\n\nSome Text\n" + expected_text = "Some Text" tika_server_original = self.parser.tika_server @@ -285,7 +285,7 @@ class TestParser(TestCase): # Check successful parsing parsed = self.parser.tika_parse(html) - self.assertEqual(expected_text, parsed) + self.assertEqual(expected_text, parsed.strip()) @mock.patch("paperless_mail.parsers.MailDocumentParser.generate_pdf_from_mail") @mock.patch("paperless_mail.parsers.MailDocumentParser.generate_pdf_from_html") @@ -355,19 +355,46 @@ class TestParser(TestCase): ) as html_expected_handle: self.assertHTMLEqual(html_expected_handle.read(), html_handle.read()) - @mock.patch("documents.loggers.LoggingMixin.log") # Disable log output - def test_generate_pdf_from_mail(self, m): + @mock.patch("paperless_mail.parsers.requests.post") + @mock.patch("paperless_mail.parsers.MailDocumentParser.mail_to_html") + def test_generate_pdf_from_mail( + self, + mock_mail_to_html: mock.MagicMock, + mock_post: mock.MagicMock, + ): + mock_response = mock.MagicMock() + mock_response.content = b"Content" + mock_post.return_value = mock_response + + mock_mail_to_html.return_value = "Testresponse" + mail = self.parser.get_parsed(os.path.join(self.SAMPLE_FILES, "html.eml")) - pdf_path = os.path.join(self.parser.tempdir, "test_generate_pdf_from_mail.pdf") + retval = self.parser.generate_pdf_from_mail(mail) + self.assertEqual(b"Content", retval) - with open(pdf_path, "wb") as file: - file.write(self.parser.generate_pdf_from_mail(mail)) - file.close() + mock_generate_pdf_from_mail.assert_called_once_with( + self.parser.get_parsed(None), + ) + mock_generate_pdf_from_html.assert_called_once_with( + self.parser.get_parsed(None).html, + self.parser.get_parsed(None).attachments, + ) + self.assertEqual( + self.parser.gotenberg_server + "/forms/pdfengines/merge", + mock_post.call_args.args[0], + ) + self.assertEqual({}, mock_post.call_args.kwargs["headers"]) + self.assertEqual( + b"Mail Return", + mock_post.call_args.kwargs["files"]["1_mail.pdf"][1].read(), + ) + self.assertEqual( + b"HTML Return", + mock_post.call_args.kwargs["files"]["2_html.pdf"][1].read(), + ) - extracted = extract_text(pdf_path) - expected = "From Name