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