From a7f742825fb4f76eb32428eee664fa6555def680 Mon Sep 17 00:00:00 2001 From: Jonas Winkler Date: Tue, 17 Nov 2020 17:00:26 +0100 Subject: [PATCH] more test --- src/paperless_mail/tests/test_mail.py | 45 ++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/paperless_mail/tests/test_mail.py b/src/paperless_mail/tests/test_mail.py index 10ad36336..37740bb9b 100644 --- a/src/paperless_mail/tests/test_mail.py +++ b/src/paperless_mail/tests/test_mail.py @@ -56,16 +56,53 @@ class TestMail(TestCase): rule = MailRule(assign_title_from=MailRule.TITLE_FROM_SUBJECT) self.assertEqual(get_title(message, att, rule), "the message title") - @mock.patch("django_q.tasks.async_task") + @mock.patch("paperless_mail.mail.async_task") def test_handle_message(self, m): message = namedtuple('MailMessage', []) message.subject = "the message title" + att = namedtuple('Attachment', []) - att.filename = "this_is_the_file.pdf" + att.filename = "test1.pdf" att.content_type = 'application/pdf' att.payload = b"attachment contents" - message.attachments = [att] + + att2 = namedtuple('Attachment', []) + att2.filename = "test2.pdf" + att2.content_type = 'application/pdf' + att2.payload = b"attachment contents" + + att3 = namedtuple('Attachment', []) + att3.filename = "test3.pdf" + att3.content_type = 'application/invalid' + att3.payload = b"attachment contents" + + message.attachments = [att, att2, att3] rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME) - #handle_message(message, rule) + result = handle_message(message, rule) + + self.assertEqual(result, 2) + + self.assertEqual(len(m.call_args_list), 2) + + args1, kwargs1 = m.call_args_list[0] + args2, kwargs2 = m.call_args_list[1] + + self.assertEqual(kwargs1['force_title'], "test1") + self.assertEqual(kwargs1['original_filename'], "test1.pdf") + + self.assertEqual(kwargs2['force_title'], "test2") + self.assertEqual(kwargs2['original_filename'], "test2.pdf") + + @mock.patch("paperless_mail.mail.async_task") + def test_handle_empty_message(self, m): + message = namedtuple('MailMessage', []) + + message.attachments = [] + rule = MailRule() + + result = handle_message(message, rule) + + self.assertFalse(m.called) + self.assertEqual(result, 0)