示例#1
0
 def test_get_title(self):
     message = namedtuple('MailMessage', [])
     message.subject = "the message title"
     att = namedtuple('Attachment', [])
     att.filename = "this_is_the_file.pdf"
     rule = MailRule(name="a",
                     assign_title_from=MailRule.TITLE_FROM_FILENAME)
     self.assertEqual(get_title(message, att, rule), "this_is_the_file")
     rule = MailRule(name="b",
                     assign_title_from=MailRule.TITLE_FROM_SUBJECT)
     self.assertEqual(get_title(message, att, rule), "the message title")
示例#2
0
    def test_get_correspondent(self):
        message = namedtuple('MailMessage', [])
        message.from_ = "*****@*****.**"
        message.from_values = {
            'name': "Someone!",
            'email': "*****@*****.**"
        }

        message2 = namedtuple('MailMessage', [])
        message2.from_ = "*****@*****.**"
        message2.from_values = {'name': "", 'email': "*****@*****.**"}

        me_localhost = Correspondent.objects.create(name=message2.from_)
        someone_else = Correspondent.objects.create(name="someone else")

        handler = MailAccountHandler()

        rule = MailRule(
            name="a",
            assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NOTHING)
        self.assertIsNone(handler.get_correspondent(message, rule))

        rule = MailRule(
            name="b",
            assign_correspondent_from=MailRule.CORRESPONDENT_FROM_EMAIL)
        c = handler.get_correspondent(message, rule)
        self.assertIsNotNone(c)
        self.assertEqual(c.name, "*****@*****.**")
        c = handler.get_correspondent(message2, rule)
        self.assertIsNotNone(c)
        self.assertEqual(c.name, "*****@*****.**")
        self.assertEqual(c.id, me_localhost.id)

        rule = MailRule(
            name="c",
            assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NAME)
        c = handler.get_correspondent(message, rule)
        self.assertIsNotNone(c)
        self.assertEqual(c.name, "Someone!")
        c = handler.get_correspondent(message2, rule)
        self.assertIsNotNone(c)
        self.assertEqual(c.id, me_localhost.id)

        rule = MailRule(
            name="d",
            assign_correspondent_from=MailRule.CORRESPONDENT_FROM_CUSTOM,
            assign_correspondent=someone_else)
        c = handler.get_correspondent(message, rule)
        self.assertEqual(c, someone_else)
示例#3
0
    def test_filename_filter(self):
        message = create_message()
        message.attachments = [
            create_attachment(filename="f1.pdf"),
            create_attachment(filename="f2.pdf"),
            create_attachment(filename="f3.pdf"),
            create_attachment(filename="f2.png"),
        ]

        tests = [
            ("*.pdf", ["f1.pdf", "f2.pdf", "f3.pdf"]),
            ("f1.pdf", ["f1.pdf"]),
            ("f1", []),
            ("*", ["f1.pdf", "f2.pdf", "f3.pdf", "f2.png"]),
            ("*.png", ["f2.png"]),
        ]

        for (pattern, matches) in tests:
            self.async_task.reset_mock()
            account = MailAccount()
            rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME,
                            account=account,
                            filter_attachment_filename=pattern)

            result = self.mail_account_handler.handle_message(message, rule)

            self.assertEqual(result, len(matches))
            filenames = [
                a[1]['override_filename']
                for a in self.async_task.call_args_list
            ]
            self.assertCountEqual(filenames, matches)
示例#4
0
    def test_handle_message(self):
        message = create_message(subject="the message title",
                                 from_="Myself",
                                 num_attachments=2)

        account = MailAccount()
        rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME,
                        account=account)

        result = self.mail_account_handler.handle_message(message, rule)

        self.assertEqual(result, 2)

        self.assertEqual(len(self.async_task.call_args_list), 2)

        args1, kwargs1 = self.async_task.call_args_list[0]
        args2, kwargs2 = self.async_task.call_args_list[1]

        self.assertTrue(os.path.isfile(kwargs1['path']), kwargs1['path'])

        self.assertEqual(kwargs1['override_title'], "file_0")
        self.assertEqual(kwargs1['override_filename'], "file_0.pdf")

        self.assertTrue(os.path.isfile(kwargs2['path']), kwargs1['path'])

        self.assertEqual(kwargs2['override_title'], "file_1")
        self.assertEqual(kwargs2['override_filename'], "file_1.pdf")
示例#5
0
    def test_handle_empty_message(self):
        message = namedtuple('MailMessage', [])

        message.attachments = []
        rule = MailRule()

        result = self.mail_account_handler.handle_message(message, rule)

        self.assertFalse(self.async_task.called)
        self.assertEqual(result, 0)
示例#6
0
    def test_handle_inline_files(self):
        message = create_message()
        message.attachments = [
            create_attachment(filename="f1.pdf", content_disposition='inline'),
            create_attachment(filename="f2.pdf",
                              content_disposition='attachment')
        ]

        account = MailAccount()
        rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME,
                        account=account,
                        attachment_type=MailRule.ATTACHMENT_TYPE_EVERYTHING)

        result = self.mail_account_handler.handle_message(message, rule)

        self.assertEqual(result, 2)
        self.assertEqual(self.async_task.call_count, 2)
示例#7
0
    def test_handle_disposition(self):
        message = create_message()
        message.attachments = [
            create_attachment(filename="f1.pdf", content_disposition='inline'),
            create_attachment(filename="f2.pdf",
                              content_disposition='attachment')
        ]

        account = MailAccount()
        rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME,
                        account=account)

        result = self.mail_account_handler.handle_message(message, rule)

        self.assertEqual(result, 1)
        self.assertEqual(self.async_task.call_count, 1)

        args, kwargs = self.async_task.call_args
        self.assertEqual(kwargs['override_filename'], "f2.pdf")
示例#8
0
    def test_handle_unknown_mime_type(self):
        message = create_message()
        message.attachments = [
            create_attachment(filename="f1.pdf"),
            create_attachment(
                filename="f2.json",
                payload=b"{'much': 'payload.', 'so': 'json', 'wow': true}")
        ]

        account = MailAccount()
        rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME,
                        account=account)

        result = self.mail_account_handler.handle_message(message, rule)

        self.assertEqual(result, 1)
        self.assertEqual(self.async_task.call_count, 1)

        args, kwargs = self.async_task.call_args
        self.assertEqual(kwargs['override_filename'], "f1.pdf")