Completed
Push — main ( 9db286...3b2881 )
by Alexander
01:39
created

tests.test_mailbox_cleaner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 49
dl 0
loc 84
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A MyTestCase.setUp() 0 7 1
A MyTestCase.__init__() 0 2 1
A MyTestCase.test_convert_filename() 0 14 1
A MyTestCase.test_detach_attachment() 0 16 2
A MyTestCase.test_get_tags() 0 10 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Module documentation goes here."""
5
6
import email
7
import logging
8
import unittest
9
from src.mailbox_cleaner import MailboxCleaner
10
11
12
class MyTestCase(unittest.TestCase):
13
    """Class documentation goes here."""
14
15
    class _Namespace:  # pylint: disable=R0903
16
        """Helper class for arguments."""
17
18
        def __init__(self, **kwargs):
19
            self.__dict__.update(kwargs)
20
21
    def setUp(self):
22
        logging.basicConfig(level=logging.DEBUG)
23
        self.args = self._Namespace(server='example.org',
24
                                    max_size=20,
25
                                    skip_download=False,
26
                                    detach=False,
27
                                    target='/tmp/mailbox_cleaner')
28
29
    def test_get_tags(self):
30
        """Testing flag extraction."""
31
32
        test_input = [(b'1 (RFC822 {242020}', b'\r\n'),
33
                      b' UID 142377 FLAGS (\\Seen \\Recent NotJunk)']
34
        test_output = MailboxCleaner.get_flags_from_struct(test_input)
35
        print("Result: ", test_output)
36
        self.assertTrue("Seen" in test_output)
37
        self.assertTrue("NotJunk" in test_output)
38
        self.assertFalse("Recent" in test_output)
39
40
    # def test_convert_date(self):
41
    #     """Testing date conversion."""
42
43
    #     test_input = "Thu, 22 Oct 2020 12:38:26 +0200"
44
    #     test_output = MailboxCleaner.convert_date(test_input)
45
    #     test_expectation = '"22-Oct-2020 12:38:26 +0200"'
46
    #     print("Result: ", test_output)
47
    #     self.assertEqual(test_output, test_expectation)
48
49
    def test_convert_filename(self):
50
        """Testing decoding filenames."""
51
52
        test_input = "=?iso-8859-1?Q?2=5FB=FCrg=5F_Br?= =?iso-8859-1?Q?l.p?="
53
        test_output = MailboxCleaner.convert_filename(test_input)
54
        test_expectation = '2_Bürg_ Brl.p'
55
        print("Result: ", test_output)
56
        self.assertEqual(test_output, test_expectation)
57
58
        test_input = "file/with/slash.pdf"
59
        test_output = MailboxCleaner.convert_filename(test_input)
60
        test_expectation = 'file_with_slash.pdf'
61
        print("Result: ", test_output)
62
        self.assertEqual(test_output, test_expectation)
63
64
    def test_detach_attachment(self):
65
        """Testing detaching attachments."""
66
67
        test_input = 'tests/test.eml'
68
        test_expectation = 'removed-fed00c051c9f991a8a2d19dcadcf5ff3.jpg.txt'
69
70
        with open(test_input) as filepointer:
71
            msg = email.message_from_file(filepointer)
72
73
        self.assertFalse(test_expectation in msg.as_string())
74
        cleaner = MailboxCleaner(self.args)
75
        modified = cleaner.download_and_detach_attachments(msg)
76
        self.assertTrue(modified)
77
        self.assertTrue(test_expectation in msg.as_string())
78
        modified = cleaner.download_and_detach_attachments(msg)
79
        self.assertFalse(modified)
80
81
82
if __name__ == '__main__':
83
    unittest.main()
84