From 4f287b5ecddd94c249ace38e8a71e3cb6e87a0f0 Mon Sep 17 00:00:00 2001 From: Trenton Holmes Date: Tue, 15 Mar 2022 13:58:58 -0700 Subject: [PATCH] Fixes the EmailAddress which was a dict and is now a class --- src/paperless_mail/mail.py | 12 +++++------- src/paperless_mail/tests/test_mail.py | 26 +++++++++++++++----------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py index ac1568829..a7e455829 100644 --- a/src/paperless_mail/mail.py +++ b/src/paperless_mail/mail.py @@ -16,6 +16,7 @@ from imap_tools import AND from imap_tools import MailBox from imap_tools import MailboxFolderSelectError from imap_tools import MailBoxUnencrypted +from imap_tools import MailMessage from imap_tools import MailMessageFlags from paperless_mail.models import MailAccount from paperless_mail.models import MailRule @@ -122,7 +123,7 @@ class MailAccountHandler(LoggingMixin): "Unknown title selector.", ) # pragma: nocover - def get_correspondent(self, message, rule): + def get_correspondent(self, message: MailMessage, rule): c_from = rule.assign_correspondent_from if c_from == MailRule.CORRESPONDENT_FROM_NOTHING: @@ -132,12 +133,9 @@ class MailAccountHandler(LoggingMixin): return self._correspondent_from_name(message.from_) elif c_from == MailRule.CORRESPONDENT_FROM_NAME: - if ( - message.from_values - and "name" in message.from_values - and message.from_values["name"] - ): - return self._correspondent_from_name(message.from_values["name"]) + from_values = message.from_values + if from_values is not None and len(from_values.name) > 0: + return self._correspondent_from_name(from_values.name) else: return self._correspondent_from_name(message.from_) diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py index bc56ed647..6d78f5306 100644 --- a/src/paperless_mail/tests/test_mail.py +++ b/src/paperless_mail/tests/test_mail.py @@ -9,6 +9,7 @@ from django.db import DatabaseError from django.test import TestCase from documents.models import Correspondent from documents.tests.utils import DirectoriesMixin +from imap_tools import EmailAddress from imap_tools import MailboxFolderSelectError from imap_tools import MailMessageFlags from paperless_mail import tasks @@ -19,7 +20,6 @@ from paperless_mail.models import MailRule class BogusFolderManager: - current_folder = "INBOX" def set(self, new_folder): @@ -129,7 +129,6 @@ def create_attachment( def fake_magic_from_buffer(buffer, mime=False): - if mime: if "PDF" in str(buffer): return "application/pdf" @@ -188,11 +187,19 @@ class TestMail(DirectoriesMixin, TestCase): def test_get_correspondent(self): message = namedtuple("MailMessage", []) message.from_ = "someone@somewhere.com" - message.from_values = {"name": "Someone!", "email": "someone@somewhere.com"} + message.from_values = EmailAddress( + "Someone!", + "someone@somewhere.com", + "Someone! ", + ) message2 = namedtuple("MailMessage", []) message2.from_ = "me@localhost.com" - message2.from_values = {"name": "", "email": "fake@localhost.com"} + message2.from_values = EmailAddress( + "", + "fake@localhost.com", + "", + ) me_localhost = Correspondent.objects.create(name=message2.from_) someone_else = Correspondent.objects.create(name="someone else") @@ -482,12 +489,11 @@ class TestMail(DirectoriesMixin, TestCase): password="wrong", ) - try: + with self.assertRaises(MailError) as context: self.mail_account_handler.handle_mail_account(account) - except MailError as e: - self.assertTrue(str(e).startswith("Error while authenticating account")) - else: - self.fail("Should raise exception") + self.assertTrue( + str(context).startswith("Error while authenticating account"), + ) def test_error_skip_account(self): account_faulty = MailAccount.objects.create( @@ -676,7 +682,6 @@ class TestManagementCommand(TestCase): "paperless_mail.management.commands.mail_fetcher.tasks.process_mail_accounts", ) def test_mail_fetcher(self, m): - call_command("mail_fetcher") m.assert_called_once() @@ -711,7 +716,6 @@ class TestTasks(TestCase): @mock.patch("paperless_mail.tasks.MailAccountHandler.handle_mail_account") def test_single_accounts(self, m): - MailAccount.objects.create( name="A", imap_server="A",