Also converts the live mail account over (probably)

This commit is contained in:
Trenton H 2024-06-26 11:04:07 -07:00
parent 4d53dff978
commit be2c55af38
2 changed files with 47 additions and 30 deletions

View File

@ -1,7 +1,11 @@
import os
from collections.abc import Generator
from pathlib import Path from pathlib import Path
import pytest import pytest
from paperless_mail.mail import MailAccountHandler
from paperless_mail.models import MailAccount
from paperless_mail.parsers import MailDocumentParser from paperless_mail.parsers import MailDocumentParser
@ -63,3 +67,23 @@ def merged_pdf_second(sample_dir: Path) -> Path:
@pytest.fixture() @pytest.fixture()
def mail_parser() -> MailDocumentParser: def mail_parser() -> MailDocumentParser:
return MailDocumentParser(logging_group=None) return MailDocumentParser(logging_group=None)
@pytest.fixture()
def live_mail_account() -> Generator[MailAccount, None, None]:
try:
account = MailAccount.objects.create(
name="test",
imap_server=os.environ["PAPERLESS_MAIL_TEST_HOST"],
username=os.environ["PAPERLESS_MAIL_TEST_USER"],
password=os.environ["PAPERLESS_MAIL_TEST_PASSWD"],
imap_port=993,
)
yield account
finally:
account.delete()
@pytest.fixture()
def mail_account_handler() -> MailAccountHandler:
return MailAccountHandler()

View File

@ -1,7 +1,7 @@
import os import os
import warnings
import pytest import pytest
from django.test import TestCase
from paperless_mail.mail import MailAccountHandler from paperless_mail.mail import MailAccountHandler
from paperless_mail.mail import MailError from paperless_mail.mail import MailError
@ -16,53 +16,46 @@ from paperless_mail.models import MailRule
or not len(os.environ["PAPERLESS_MAIL_TEST_HOST"]), or not len(os.environ["PAPERLESS_MAIL_TEST_HOST"]),
reason="Live server testing not enabled", reason="Live server testing not enabled",
) )
class TestMailLiveServer(TestCase): @pytest.mark.django_db()
def setUp(self) -> None: class TestMailLiveServer:
self.mail_account_handler = MailAccountHandler() def test_process_non_gmail_server_flag(
self.account = MailAccount.objects.create( self,
name="test", mail_account_handler: MailAccountHandler,
imap_server=os.environ["PAPERLESS_MAIL_TEST_HOST"], live_mail_account: MailAccount,
username=os.environ["PAPERLESS_MAIL_TEST_USER"], ):
password=os.environ["PAPERLESS_MAIL_TEST_PASSWD"],
imap_port=993,
)
return super().setUp()
def tearDown(self) -> None:
self.account.delete()
return super().tearDown()
def test_process_non_gmail_server_flag(self):
try: try:
rule1 = MailRule.objects.create( rule1 = MailRule.objects.create(
name="testrule", name="testrule",
account=self.account, account=live_mail_account,
action=MailRule.MailAction.FLAG, action=MailRule.MailAction.FLAG,
) )
self.mail_account_handler.handle_mail_account(self.account) mail_account_handler.handle_mail_account(live_mail_account)
rule1.delete() rule1.delete()
except MailError as e: except MailError as e:
self.fail(f"Failure: {e}") pytest.fail(f"Failure: {e}")
except Exception: except Exception as e:
pass warnings.warn(f"Unhandled exception: {e}")
def test_process_non_gmail_server_tag(self): def test_process_non_gmail_server_tag(
self,
mail_account_handler: MailAccountHandler,
live_mail_account: MailAccount,
):
try: try:
rule2 = MailRule.objects.create( rule2 = MailRule.objects.create(
name="testrule", name="testrule",
account=self.account, account=live_mail_account,
action=MailRule.MailAction.TAG, action=MailRule.MailAction.TAG,
) )
self.mail_account_handler.handle_mail_account(self.account) mail_account_handler.handle_mail_account(live_mail_account)
rule2.delete() rule2.delete()
except MailError as e: except MailError as e:
self.fail(f"Failure: {e}") pytest.fail(f"Failure: {e}")
except Exception: except Exception as e:
pass warnings.warn(f"Unhandled exception: {e}")