|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
"""Module documentation goes here.""" |
|
5
|
|
|
|
|
6
|
|
|
import email |
|
7
|
|
|
import unittest |
|
8
|
|
|
from src.mailbox_message import MailboxCleanerMessage |
|
9
|
|
|
from tests.test_mailbox_abstract import TestMailboxAbstract |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestMailboxMessage(TestMailboxAbstract, unittest.TestCase): |
|
13
|
|
|
"""Class documentation goes here.""" |
|
14
|
|
|
|
|
15
|
|
|
def test_convert_filename(self): |
|
16
|
|
|
"""Testing decoding filenames.""" |
|
17
|
|
|
|
|
18
|
|
|
test_input = "=?iso-8859-1?Q?2=5FB=FCrg=5F_Br?= =?iso-8859-1?Q?l.p?=" |
|
19
|
|
|
test_output = MailboxCleanerMessage.convert_filename(test_input) |
|
20
|
|
|
test_expectation = '2_Bürg_ Brl.p' |
|
21
|
|
|
print("Result: ", test_output) |
|
22
|
|
|
self.assertEqual(test_output, test_expectation) |
|
23
|
|
|
|
|
24
|
|
|
test_input = "file/with/slash.pdf" |
|
25
|
|
|
test_output = MailboxCleanerMessage.convert_filename(test_input) |
|
26
|
|
|
test_expectation = 'file_with_slash.pdf' |
|
27
|
|
|
print("Result: ", test_output) |
|
28
|
|
|
self.assertEqual(test_output, test_expectation) |
|
29
|
|
|
|
|
30
|
|
|
def test_download_attachment(self): |
|
31
|
|
|
"""Testing downloading attachments.""" |
|
32
|
|
|
|
|
33
|
|
|
test_input = 'tests/test.eml' |
|
34
|
|
|
|
|
35
|
|
|
with open(test_input) as filepointer: |
|
36
|
|
|
msg = email.message_from_file(filepointer) |
|
37
|
|
|
|
|
38
|
|
|
message = MailboxCleanerMessage(self.args) |
|
39
|
|
|
uid = MailboxCleanerMessage.get_uid(msg) |
|
40
|
|
|
expected = '[email protected]' |
|
41
|
|
|
self.assertEqual(uid, expected) |
|
42
|
|
|
|
|
43
|
|
|
message.download_attachment(msg) |
|
44
|
|
|
|
|
45
|
|
|
self.args.skip_download = True |
|
46
|
|
|
message.download_attachment(msg) |
|
47
|
|
|
|
|
48
|
|
|
def test_detach_attachment(self): |
|
49
|
|
|
"""Testing detaching attachments.""" |
|
50
|
|
|
|
|
51
|
|
|
test_input = 'tests/test.eml' |
|
52
|
|
|
test_expectation = 'removed-fed00c051c9f991a8a2d19dcadcf5ff3.jpg.txt' |
|
53
|
|
|
|
|
54
|
|
|
with open(test_input) as filepointer: |
|
55
|
|
|
msg = email.message_from_file(filepointer) |
|
56
|
|
|
|
|
57
|
|
|
self.assertFalse(test_expectation in msg.as_string()) |
|
58
|
|
|
message = MailboxCleanerMessage(self.args) |
|
59
|
|
|
modified = message.download_and_detach_attachments(msg) |
|
60
|
|
|
self.assertTrue(modified) |
|
61
|
|
|
self.assertTrue(test_expectation in msg.as_string()) |
|
62
|
|
|
modified = message.download_and_detach_attachments(msg) |
|
63
|
|
|
self.assertFalse(modified) |
|
64
|
|
|
|
|
65
|
|
|
def test_process_folder_locally(self): |
|
66
|
|
|
"""Testing local eml file processing.""" |
|
67
|
|
|
|
|
68
|
|
|
cleaner = MailboxCleanerMessage(self.args) |
|
69
|
|
|
cleaner.process_directory(self._void) |
|
70
|
|
|
|
|
71
|
|
|
def _void(self, param): |
|
72
|
|
|
"""Helper function for testing.""" |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
if __name__ == '__main__': |
|
76
|
|
|
unittest.main() |
|
77
|
|
|
|