mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	add conditions to unittests
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								src/paperless_mail/tests/samples/sample.html.pdf.webp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/paperless_mail/tests/samples/sample.html.pdf.webp
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 2.8 KiB | 
| @@ -268,7 +268,7 @@ class TestParser(TestCase): | ||||
|     @mock.patch("documents.loggers.LoggingMixin.log")  # Disable log output | ||||
|     def test_tika_parse(self, m): | ||||
|         html = '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><p>Some Text</p></body></html>' | ||||
|         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 <someone@example.de>\n\n2022-10-15 09:23\n\nSubject HTML Message\n\nTo someone@example.de\n\nAttachments IntM6gnXFm00FEV5.png (6.89 KiB), 600+kbfile.txt (0.59 MiB)\n\nSome Text \n\nand an embedded image.\n\n\x0c" | ||||
|         self.assertEqual(expected, extracted) | ||||
|         mock_response.raise_for_status.assert_called_once() | ||||
|  | ||||
|     def test_transform_inline_html(self): | ||||
|         class MailAttachmentMock: | ||||
| @@ -391,13 +418,18 @@ class TestParser(TestCase): | ||||
|         self.assertTrue(result[0][0] in resulting_html) | ||||
|         self.assertFalse("<script" in resulting_html.lower()) | ||||
|  | ||||
|     @mock.patch("paperless_mail.parsers.requests.post") | ||||
|     @mock.patch("documents.loggers.LoggingMixin.log")  # Disable log output | ||||
|     def test_generate_pdf_from_html(self, m): | ||||
|     def test_generate_pdf_from_html(self, m, mock_post: mock.MagicMock): | ||||
|         class MailAttachmentMock: | ||||
|             def __init__(self, payload, content_id): | ||||
|                 self.payload = payload | ||||
|                 self.content_id = content_id | ||||
|  | ||||
|         mock_response = mock.MagicMock() | ||||
|         mock_response.content = b"Content" | ||||
|         mock_post.return_value = mock_response | ||||
|  | ||||
|         result = None | ||||
|  | ||||
|         with open(os.path.join(self.SAMPLE_FILES, "sample.html")) as html_file: | ||||
| @@ -409,12 +441,30 @@ class TestParser(TestCase): | ||||
|                 ] | ||||
|                 result = self.parser.generate_pdf_from_html(html, attachments) | ||||
|  | ||||
|         pdf_path = os.path.join(self.parser.tempdir, "test_generate_pdf_from_html.pdf") | ||||
|         self.assertEqual( | ||||
|             self.parser.gotenberg_server + "/forms/chromium/convert/html", | ||||
|             mock_post.call_args.args[0], | ||||
|         ) | ||||
|         self.assertEqual({}, mock_post.call_args.kwargs["headers"]) | ||||
|         self.assertEqual( | ||||
|             { | ||||
|                 "marginTop": "0.1", | ||||
|                 "marginBottom": "0.1", | ||||
|                 "marginLeft": "0.1", | ||||
|                 "marginRight": "0.1", | ||||
|                 "paperWidth": "8.27", | ||||
|                 "paperHeight": "11.7", | ||||
|                 "scale": "1.0", | ||||
|             }, | ||||
|             mock_post.call_args.kwargs["data"], | ||||
|         ) | ||||
|  | ||||
|         with open(pdf_path, "wb") as file: | ||||
|             file.write(result) | ||||
|             file.close() | ||||
|         # read to assert it is a file like object. | ||||
|         mock_post.call_args.kwargs["files"]["cidpart1pNdUSz0sD3NqVtPgexamplede"][ | ||||
|             1 | ||||
|         ].read() | ||||
|         mock_post.call_args.kwargs["files"]["index.html"][1].read() | ||||
|  | ||||
|         extracted = extract_text(pdf_path) | ||||
|         expected = "Some Text\n\n  This image should not be shown.\n\nand an embedded image.\n\nParagraph unchanged.\n\n\x0c" | ||||
|         self.assertEqual(expected, extracted) | ||||
|         mock_response.raise_for_status.assert_called_once() | ||||
|  | ||||
|         self.assertEqual(b"Content", result) | ||||
|   | ||||
| @@ -62,7 +62,7 @@ class TestParserLive(TestCase): | ||||
|     @mock.patch("documents.loggers.LoggingMixin.log")  # Disable log output | ||||
|     def test_tika_parse(self, m): | ||||
|         html = '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><p>Some Text</p></body></html>' | ||||
|         expected_text = "\n\n\n\n\n\n\n\n\nSome Text\n" | ||||
|         expected_text = "Some Text" | ||||
|  | ||||
|         tika_server_original = self.parser.tika_server | ||||
|  | ||||
| @@ -79,7 +79,7 @@ class TestParserLive(TestCase): | ||||
|  | ||||
|         # Check successful parsing | ||||
|         parsed = self.parser.tika_parse(html) | ||||
|         self.assertEqual(expected_text, parsed) | ||||
|         self.assertEqual(expected_text, parsed.strip()) | ||||
|  | ||||
|     @pytest.mark.skipif( | ||||
|         "GOTENBERG_LIVE" not in os.environ, | ||||
| @@ -124,7 +124,10 @@ class TestParserLive(TestCase): | ||||
|             file.write(self.parser.generate_pdf_from_mail(mail)) | ||||
|             file.close() | ||||
|  | ||||
|         converted = os.path.join(parser.tempdir, "test_generate_pdf_from_mail.webp") | ||||
|         converted = os.path.join( | ||||
|             self.parser.tempdir, | ||||
|             "test_generate_pdf_from_mail.webp", | ||||
|         ) | ||||
|         run_convert( | ||||
|             density=300, | ||||
|             scale="500x5000>", | ||||
| @@ -149,14 +152,12 @@ class TestParserLive(TestCase): | ||||
|             f"PDF looks different. Check if {converted} looks weird.", | ||||
|         ) | ||||
|  | ||||
|     # Only run if convert is available | ||||
|     @pytest.mark.skipif( | ||||
|         "PAPERLESS_TEST_SKIP_CONVERT" in os.environ, | ||||
|         reason="PAPERLESS_TEST_SKIP_CONVERT set, skipping Test", | ||||
|         "GOTENBERG_LIVE" not in os.environ, | ||||
|         reason="No gotenberg server", | ||||
|     ) | ||||
|     @mock.patch("documents.loggers.LoggingMixin.log")  # Disable log output | ||||
|     def test_generate_pdf_from_html(self, m): | ||||
|         # TODO | ||||
|     def test_generate_pdf_from_html_no_convert(self, m): | ||||
|         class MailAttachmentMock: | ||||
|             def __init__(self, payload, content_id): | ||||
|                 self.payload = payload | ||||
| @@ -179,7 +180,44 @@ class TestParserLive(TestCase): | ||||
|             file.write(result) | ||||
|             file.close() | ||||
|  | ||||
|         converted = os.path.join(parser.tempdir, "test_generate_pdf_from_html.webp") | ||||
|         extracted = extract_text(pdf_path) | ||||
|         expected = "Some Text\n\n  This image should not be shown.\n\nand an embedded image.\n\nParagraph unchanged.\n\n\x0c" | ||||
|         self.assertEqual(expected, extracted) | ||||
|  | ||||
|     @pytest.mark.skipif( | ||||
|         "GOTENBERG_LIVE" not in os.environ, | ||||
|         reason="No gotenberg server", | ||||
|     ) | ||||
|     # Only run if convert is available | ||||
|     @pytest.mark.skipif( | ||||
|         "PAPERLESS_TEST_SKIP_CONVERT" in os.environ, | ||||
|         reason="PAPERLESS_TEST_SKIP_CONVERT set, skipping Test", | ||||
|     ) | ||||
|     @mock.patch("documents.loggers.LoggingMixin.log")  # Disable log output | ||||
|     def test_generate_pdf_from_html(self, m): | ||||
|         class MailAttachmentMock: | ||||
|             def __init__(self, payload, content_id): | ||||
|                 self.payload = payload | ||||
|                 self.content_id = content_id | ||||
|  | ||||
|         result = None | ||||
|  | ||||
|         with open(os.path.join(self.SAMPLE_FILES, "sample.html")) as html_file: | ||||
|             with open(os.path.join(self.SAMPLE_FILES, "sample.png"), "rb") as png_file: | ||||
|                 html = html_file.read() | ||||
|                 png = png_file.read() | ||||
|                 attachments = [ | ||||
|                     MailAttachmentMock(png, "part1.pNdUSz0s.D3NqVtPg@example.de"), | ||||
|                 ] | ||||
|                 result = self.parser.generate_pdf_from_html(html, attachments) | ||||
|  | ||||
|         pdf_path = os.path.join(self.parser.tempdir, "sample.html.pdf") | ||||
|  | ||||
|         with open(pdf_path, "wb") as file: | ||||
|             file.write(result) | ||||
|             file.close() | ||||
|  | ||||
|         converted = os.path.join(self.parser.tempdir, "sample.html.pdf.webp") | ||||
|         run_convert( | ||||
|             density=300, | ||||
|             scale="500x5000>", | ||||
| @@ -195,9 +233,10 @@ class TestParserLive(TestCase): | ||||
|         thumb_hash = self.hashfile(converted) | ||||
|  | ||||
|         # The created pdf is not reproducible. But the converted image should always look the same. | ||||
|         expected_hash = ( | ||||
|             "267d61f0ab8f128a037002a424b2cb4bfe18a81e17f0b70f15d241688ed47d1a" | ||||
|         expected_hash = self.hashfile( | ||||
|             os.path.join(self.SAMPLE_FILES, "sample.html.pdf.webp"), | ||||
|         ) | ||||
|  | ||||
|         self.assertEqual( | ||||
|             thumb_hash, | ||||
|             expected_hash, | ||||
| @@ -206,6 +245,10 @@ class TestParserLive(TestCase): | ||||
|         ) | ||||
|  | ||||
|     @staticmethod | ||||
|     @pytest.mark.skipif( | ||||
|         "GOTENBERG_LIVE" not in os.environ, | ||||
|         reason="No gotenberg server", | ||||
|     ) | ||||
|     def test_is_online_image_still_available(): | ||||
|         """ | ||||
|         A public image is used in the html sample file. We have no control | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 phail
					phail