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_detach_attachment(self): |
31
|
|
|
"""Testing detaching attachments.""" |
32
|
|
|
|
33
|
|
|
test_input = 'tests/test.eml' |
34
|
|
|
test_expectation = 'removed-fed00c051c9f991a8a2d19dcadcf5ff3.jpg.txt' |
35
|
|
|
|
36
|
|
|
with open(test_input) as filepointer: |
37
|
|
|
msg = email.message_from_file(filepointer) |
38
|
|
|
|
39
|
|
|
self.assertFalse(test_expectation in msg.as_string()) |
40
|
|
|
message = MailboxCleanerMessage(self.args) |
41
|
|
|
modified = message.download_and_detach_attachments(msg) |
42
|
|
|
self.assertTrue(modified) |
43
|
|
|
self.assertTrue(test_expectation in msg.as_string()) |
44
|
|
|
modified = message.download_and_detach_attachments(msg) |
45
|
|
|
self.assertFalse(modified) |
46
|
|
|
|
47
|
|
|
def test_process_folder_locally(self): |
48
|
|
|
"""Testing local eml file processing.""" |
49
|
|
|
|
50
|
|
|
cleaner = MailboxCleanerMessage(self.args) |
51
|
|
|
cleaner.process_directory(self._void) |
52
|
|
|
|
53
|
|
|
def _void(self, param): |
54
|
|
|
"""Helper function for testing.""" |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
if __name__ == '__main__': |
58
|
|
|
unittest.main() |
59
|
|
|
|