add basic tests and fix error

This commit is contained in:
Clemens Rieder 2023-01-11 18:00:51 +01:00 committed by Trenton H
parent 195f3a5dbf
commit 6024a862d6
2 changed files with 43 additions and 2 deletions

View File

@ -101,13 +101,15 @@ class TagMailAction(BaseMailAction):
raise MailError("Not a valid AppleMail tag color.")
except Exception as e:
raise MailError(
"""Could not parse the parameters.
Make sure they look like this: apple:<color>""",
"""Could not parse parameters.
Make sure they look like this: apple:<color> and
only use allowed colors.""",
) from e
self.keyword = None
else:
self.keyword = parameter
self.color = None
def get_criteria(self):

View File

@ -24,6 +24,7 @@ from imap_tools import NOT
from paperless_mail import tasks
from paperless_mail.mail import MailAccountHandler
from paperless_mail.mail import MailError
from paperless_mail.mail import TagMailAction
from paperless_mail.models import MailAccount
from paperless_mail.models import MailRule
@ -674,6 +675,44 @@ class TestMail(DirectoriesMixin, TestCase):
self.assertEqual(len(self.bogus_mailbox.fetch(criteria, False)), 0)
self.assertEqual(len(self.bogus_mailbox.messages), 3)
def test_tag_mail_action_applemail_wrong_input(self):
self.assertRaises(
MailError,
TagMailAction,
"apple:black",
)
self.assertRaises(
MailError,
TagMailAction,
"applegreen",
)
def test_handle_mail_account_tag_applemail(self):
# all mails will be FLAGGED afterwards
account = MailAccount.objects.create(
name="test",
imap_server="",
username="admin",
password="secret",
)
_ = MailRule.objects.create(
name="testrule",
account=account,
action=MailRule.MailAction.TAG,
action_parameter="apple:green",
)
self.assertEqual(len(self.bogus_mailbox.messages), 3)
self.assertEqual(self.async_task.call_count, 0)
self.assertEqual(len(self.bogus_mailbox.fetch("UNFLAGGED", False)), 2)
self.mail_account_handler.handle_mail_account(account)
self.assertEqual(self.async_task.call_count, 2)
self.assertEqual(len(self.bogus_mailbox.fetch("UNFLAGGED", False)), 0)
self.assertEqual(len(self.bogus_mailbox.messages), 3)
def test_error_login(self):
account = MailAccount.objects.create(
name="test",