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