Fixes the EmailAddress which was a dict and is now a class

This commit is contained in:
Trenton Holmes 2022-03-15 13:58:58 -07:00
parent 02dc395880
commit 4f287b5ecd
2 changed files with 20 additions and 18 deletions

View File

@ -16,6 +16,7 @@ from imap_tools import AND
from imap_tools import MailBox from imap_tools import MailBox
from imap_tools import MailboxFolderSelectError from imap_tools import MailboxFolderSelectError
from imap_tools import MailBoxUnencrypted from imap_tools import MailBoxUnencrypted
from imap_tools import MailMessage
from imap_tools import MailMessageFlags from imap_tools import MailMessageFlags
from paperless_mail.models import MailAccount from paperless_mail.models import MailAccount
from paperless_mail.models import MailRule from paperless_mail.models import MailRule
@ -122,7 +123,7 @@ class MailAccountHandler(LoggingMixin):
"Unknown title selector.", "Unknown title selector.",
) # pragma: nocover ) # pragma: nocover
def get_correspondent(self, message, rule): def get_correspondent(self, message: MailMessage, rule):
c_from = rule.assign_correspondent_from c_from = rule.assign_correspondent_from
if c_from == MailRule.CORRESPONDENT_FROM_NOTHING: if c_from == MailRule.CORRESPONDENT_FROM_NOTHING:
@ -132,12 +133,9 @@ class MailAccountHandler(LoggingMixin):
return self._correspondent_from_name(message.from_) return self._correspondent_from_name(message.from_)
elif c_from == MailRule.CORRESPONDENT_FROM_NAME: elif c_from == MailRule.CORRESPONDENT_FROM_NAME:
if ( from_values = message.from_values
message.from_values if from_values is not None and len(from_values.name) > 0:
and "name" in message.from_values return self._correspondent_from_name(from_values.name)
and message.from_values["name"]
):
return self._correspondent_from_name(message.from_values["name"])
else: else:
return self._correspondent_from_name(message.from_) return self._correspondent_from_name(message.from_)

View File

@ -9,6 +9,7 @@ from django.db import DatabaseError
from django.test import TestCase from django.test import TestCase
from documents.models import Correspondent from documents.models import Correspondent
from documents.tests.utils import DirectoriesMixin from documents.tests.utils import DirectoriesMixin
from imap_tools import EmailAddress
from imap_tools import MailboxFolderSelectError from imap_tools import MailboxFolderSelectError
from imap_tools import MailMessageFlags from imap_tools import MailMessageFlags
from paperless_mail import tasks from paperless_mail import tasks
@ -19,7 +20,6 @@ from paperless_mail.models import MailRule
class BogusFolderManager: class BogusFolderManager:
current_folder = "INBOX" current_folder = "INBOX"
def set(self, new_folder): def set(self, new_folder):
@ -129,7 +129,6 @@ def create_attachment(
def fake_magic_from_buffer(buffer, mime=False): def fake_magic_from_buffer(buffer, mime=False):
if mime: if mime:
if "PDF" in str(buffer): if "PDF" in str(buffer):
return "application/pdf" return "application/pdf"
@ -188,11 +187,19 @@ class TestMail(DirectoriesMixin, TestCase):
def test_get_correspondent(self): def test_get_correspondent(self):
message = namedtuple("MailMessage", []) message = namedtuple("MailMessage", [])
message.from_ = "someone@somewhere.com" message.from_ = "someone@somewhere.com"
message.from_values = {"name": "Someone!", "email": "someone@somewhere.com"} message.from_values = EmailAddress(
"Someone!",
"someone@somewhere.com",
"Someone! <someone@somewhere.com>",
)
message2 = namedtuple("MailMessage", []) message2 = namedtuple("MailMessage", [])
message2.from_ = "me@localhost.com" 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_) me_localhost = Correspondent.objects.create(name=message2.from_)
someone_else = Correspondent.objects.create(name="someone else") someone_else = Correspondent.objects.create(name="someone else")
@ -482,12 +489,11 @@ class TestMail(DirectoriesMixin, TestCase):
password="wrong", password="wrong",
) )
try: with self.assertRaises(MailError) as context:
self.mail_account_handler.handle_mail_account(account) self.mail_account_handler.handle_mail_account(account)
except MailError as e: self.assertTrue(
self.assertTrue(str(e).startswith("Error while authenticating account")) str(context).startswith("Error while authenticating account"),
else: )
self.fail("Should raise exception")
def test_error_skip_account(self): def test_error_skip_account(self):
account_faulty = MailAccount.objects.create( account_faulty = MailAccount.objects.create(
@ -676,7 +682,6 @@ class TestManagementCommand(TestCase):
"paperless_mail.management.commands.mail_fetcher.tasks.process_mail_accounts", "paperless_mail.management.commands.mail_fetcher.tasks.process_mail_accounts",
) )
def test_mail_fetcher(self, m): def test_mail_fetcher(self, m):
call_command("mail_fetcher") call_command("mail_fetcher")
m.assert_called_once() m.assert_called_once()
@ -711,7 +716,6 @@ class TestTasks(TestCase):
@mock.patch("paperless_mail.tasks.MailAccountHandler.handle_mail_account") @mock.patch("paperless_mail.tasks.MailAccountHandler.handle_mail_account")
def test_single_accounts(self, m): def test_single_accounts(self, m):
MailAccount.objects.create( MailAccount.objects.create(
name="A", name="A",
imap_server="A", imap_server="A",