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