diff --git a/src/paperless_mail/tests/samples/simple_text.eml b/src/paperless_mail/tests/samples/simple_text.eml new file mode 100644 index 000000000..49080bde8 --- /dev/null +++ b/src/paperless_mail/tests/samples/simple_text.eml @@ -0,0 +1,23 @@ +Return-Path: +Delivered-To: mail@someserver.de +Received: from mail.someserver.org ([::1]) + by e1acdba3bd07 with LMTP + id KBKZGD2YR2NTCgQAjubtDA + (envelope-from ) + for ; Wed, 10 Oct 2022 11:40:46 +0200 +Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 2BC9064C1616 + for ; Wed, 12 Oct 2022 21:40:46 +0200 (CEST) +Message-ID: <6e99e34d-e20a-80c4-ea61-d8234b612be9@someserver.de> +Date: Wed, 12 Oct 2022 21:40:43 +0200 +MIME-Version: 1.0 +User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 + Thunderbird/102.3.1 +Content-Language: en-US +To: some@one.de +From: Some One +Content-Type: text/plain; charset=UTF-8; format=flowed +Content-Transfer-Encoding: 7bit +X-Last-TLS-Session-Version: TLSv1.3 +Subject: Simple Text Mail + +This is just a simple Text Mail. diff --git a/src/paperless_mail/tests/test_eml.py b/src/paperless_mail/tests/test_eml.py new file mode 100644 index 000000000..e1aebf351 --- /dev/null +++ b/src/paperless_mail/tests/test_eml.py @@ -0,0 +1,39 @@ +import hashlib +import os + +from django.test import TestCase +from paperless_mail.parsers import MailDocumentParser + + +class TestParser(TestCase): + SAMPLE_FILES = os.path.join(os.path.dirname(__file__), "samples") + + def test_thumbnail(self): + def hashfile(file): + buf_size = 65536 # An arbitrary (but fixed) buffer + sha256 = hashlib.sha256() + with open(file, "rb") as f: + while True: + data = f.read(buf_size) + if not data: + break + sha256.update(data) + return sha256.hexdigest() + + parser = MailDocumentParser(None) + thumb = parser.get_thumbnail( + os.path.join(self.SAMPLE_FILES, "simple_text.eml"), + "message/rfc822", + ) + self.assertTrue(os.path.isfile(thumb)) + thumb_hash = hashfile(thumb) + + # The created intermediary pdf is not reproducible. But the thumbnail image should always look the same. + expected_hash = ( + "18a2513c80584e538c4a129e8a2b0ce19bf0276eab9c95b72fa93e941db38d12" + ) + self.assertEqual( + thumb_hash, + expected_hash, + "Thumbnail file hash not as expected.", + )