Passed
Push — main ( 95ff57...81b3cd )
by Alexander
01:41
created

pt()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""Module documentation goes here."""
5
6
7
import email
8
import imaplib
9
import unittest
10
from src.mailbox_imap import MailboxCleanerIMAP
11
from tests.test_mailbox_abstract import TestMailboxAbstract
12
13
14
class TestMailboxCleanerIMAP(TestMailboxAbstract, unittest.TestCase):
15
    """Class documentation goes here."""
16
    class _ImapMockup():
17
        def __init__(self):
18
            self.store = []
19
            test_input = 'tests/input/test.eml'
20
            with open(test_input) as filepointer:
21
                self.msg = email.message_from_file(filepointer)
22
23
        @staticmethod
24
        def socket():
25
            """Mocking socket."""
26
            return type("_", (__class__, object), {"setsockopt": lambda _a, _b, _c: None })
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable __class__ does not seem to be defined.
Loading history...
27
28
        @staticmethod
29
        def login(user=None, _password=None):
30
            """Mocking login."""
31
            if user != 'test':
32
                raise imaplib.IMAP4.error('wrong username')
33
34
        @staticmethod
35
        def logout():
36
            """Mocking logout."""
37
            return ('OK', None)
38
39
        @staticmethod
40
        def close():
41
            """Mocking close."""
42
            return ('OK', None)
43
44
        @staticmethod
45
        def expunge():
46
            """Mocking expunge."""
47
            return ('OK', None)
48
49
        @staticmethod
50
        def select(_folder, readonly=True):
51
            """Mocking select."""
52
            return ('OK', readonly)
53
54
        def append(self, _folder, _flags, _date, msg):
55
            """Mocking append."""
56
            self.store.append(msg)
57
            return ('OK', None)
58
59
        @staticmethod
60
        def list():
61
            """Mocking list."""
62
            folders = [b'(\\Marked \\HasNoChildren) "/" Inbox',
63
                       b'(\\Marked \\HasChildren) "/" Archive',
64
                       b'(\\HasNoChildren) "/" "Archive/Test"']
65
            return ('OK',
66
                    folders)
67
68
        def uid(self, command, _a=None, query=None, _c=None):
69
            """Mocking uid."""
70
            result = 'OK'
71
            if command.lower() == 'search':
72
                if len(self.store) > 0 or query == "ALL":
73
                    data = [b'142627 142632 142633 142640 142641']
74
                else:
75
                    data = [b'']
76
            elif command.lower() == 'fetch':
77
                data = [(b'10 (UID 142684 BODY[] {2617453}',
78
                         self.msg.as_bytes()),
79
                        b' FLAGS (\\Seen \\Recent))']
80
            else:
81
                data = None
82
            return (result, data)
83
84
    def test_basics(self):
85
        """Testing basic class."""
86
87
        imap = MailboxCleanerIMAP(self.args)
88
        with self.assertRaises((ConnectionRefusedError,
89
                                SystemExit)):
90
            imap.login()
91
92
        imap._load_cache()  # pylint: disable=W0212
93
        imap.cleanup()
94
95
    def test_with_mockup(self):
96
        """Testing with mockup class."""
97
98
        mockup = self._ImapMockup()
99
        imap = MailboxCleanerIMAP(self.args, mockup)
100
        imap.process_folders()
101
        imap.login()
102
        imap.logout()
103
104
        self.args.user = 'invalid'
105
        with self.assertRaises(SystemExit):
106
            imap = MailboxCleanerIMAP(self.args, mockup)
107
            imap.login()
108
109
    def test_get_tags(self):
110
        """Testing flag extraction."""
111
112
        test_input = [(b'1 (RFC822 {242020}', b'\r\n'),
113
                      b' UID 142377 FLAGS (\\Seen \\Recent NotJunk)']
114
        test_output = MailboxCleanerIMAP.get_flags_from_struct(test_input)
115
        print("Result: ", test_output)
116
        self.assertTrue("Seen" in test_output)
117
        self.assertTrue("NotJunk" in test_output)
118
        self.assertFalse("Recent" in test_output)
119
120
    def test_convert_date(self):
121
        """Testing date conversion."""
122
123
        test_input = "Thu, 22 Oct 2020 12:38:26 +0200"
124
        test_output = MailboxCleanerIMAP.convert_date(test_input)
125
        test_expectation = '22-Oct-2020'
126
        print("Result: ", test_output)
127
        self.assertTrue(test_expectation in test_output)
128
129
    def test_struct(self):
130
        """Testing struct parser."""
131
132
        test_input = [(b'1 (RFC822 {242020}', b'\x80abc'),
133
                      b' UID 142377 FLAGS (\\Seen \\Recent NotJunk)']
134
        test_output = MailboxCleanerIMAP.get_msg_from_struct(test_input)
135
        test_output = test_output.as_string()
136
        test_expectation = 'abc'
137
        print("Result: '%s'" % test_output)
138
        self.assertTrue(test_expectation in test_output)
139
140
141
if __name__ == '__main__':
142
    unittest.main()
143