1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# Copyright (C) 2012-2017 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
"""Test the mime_delete handler."""
import os
import sys
import shutil
import tempfile
import unittest
from contextlib import ExitStack, contextmanager
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.handlers import mime_delete
from mailman.interfaces.action import FilterAction
from mailman.interfaces.member import MemberRole
from mailman.interfaces.pipeline import DiscardMessage, RejectMessage
from mailman.interfaces.usermanager import IUserManager
from mailman.testing.helpers import (
LogFileMark, configuration, get_queue_messages,
specialized_message_from_string as mfs)
from mailman.testing.layers import ConfigLayer
from zope.component import getUtility
@contextmanager
def dummy_script():
with ExitStack() as resources:
tempdir = tempfile.mkdtemp()
resources.callback(shutil.rmtree, tempdir)
filter_path = os.path.join(tempdir, 'filter.py')
with open(filter_path, 'w', encoding='utf-8') as fp:
print("""\
import sys
print('Converted text/html to text/plain')
print('Filename:', sys.argv[1])
""", file=fp)
config.push('dummy script', """\
[mailman]
html_to_plain_text_command = {exe} {script} $filename
""".format(exe=sys.executable, script=filter_path))
resources.callback(config.pop, 'dummy script')
yield
class TestDispose(unittest.TestCase):
"""Test the mime_delete handler."""
layer = ConfigLayer
maxxDiff = None
def setUp(self):
self._mlist = create_list('test@example.com')
self._msg = mfs("""\
From: anne@example.com
To: test@example.com
Subject: A disposable message
Message-ID: <ant>
""")
config.push('dispose', """
[mailman]
site_owner: noreply@example.com
""")
self.addCleanup(config.pop, 'dispose')
def test_dispose_discard(self):
self._mlist.filter_action = FilterAction.discard
with self.assertRaises(DiscardMessage) as cm:
mime_delete.dispose(self._mlist, self._msg, {}, 'discarding')
self.assertEqual(cm.exception.message, 'discarding')
# There should be no messages in the 'bad' queue.
get_queue_messages('bad', expected_count=0)
def test_dispose_bounce(self):
self._mlist.filter_action = FilterAction.reject
with self.assertRaises(RejectMessage) as cm:
mime_delete.dispose(self._mlist, self._msg, {}, 'rejecting')
self.assertEqual(cm.exception.message, 'rejecting')
# There should be no messages in the 'bad' queue.
get_queue_messages('bad', expected_count=0)
def test_dispose_forward(self):
# The disposed message gets forwarded to the list moderators. So
# first add some moderators.
user_manager = getUtility(IUserManager)
anne = user_manager.create_address('anne@example.com')
bart = user_manager.create_address('bart@example.com')
self._mlist.subscribe(anne, MemberRole.moderator)
self._mlist.subscribe(bart, MemberRole.moderator)
# Now set the filter action and dispose the message.
self._mlist.filter_action = FilterAction.forward
with self.assertRaises(DiscardMessage) as cm:
mime_delete.dispose(self._mlist, self._msg, {}, 'forwarding')
self.assertEqual(cm.exception.message, 'forwarding')
# There should now be a multipart message in the virgin queue destined
# for the mailing list owners.
items = get_queue_messages('virgin', expected_count=1)
message = items[0].msg
self.assertEqual(message.get_content_type(), 'multipart/mixed')
# Anne and Bart should be recipients of the message, but it will look
# like the message is going to the list owners.
self.assertEqual(message['to'], 'test-owner@example.com')
self.assertEqual(message.recipients,
set(['anne@example.com', 'bart@example.com']))
# The list owner should be the sender.
self.assertEqual(message['from'], 'noreply@example.com')
self.assertEqual(message['subject'],
'Content filter message notification')
# The body of the first part provides the moderators some details.
part0 = message.get_payload(0)
self.assertEqual(part0.get_content_type(), 'text/plain')
self.assertMultiLineEqual(part0.get_payload(), """\
The attached message matched the Test mailing list's content
filtering rules and was prevented from being forwarded on to the list
membership. You are receiving the only remaining copy of the discarded
message.
""")
# The second part is the container for the original message.
part1 = message.get_payload(1)
self.assertEqual(part1.get_content_type(), 'message/rfc822')
# And the first part of *that* message will be the original message.
original = part1.get_payload(0)
self.assertEqual(original['subject'], 'A disposable message')
self.assertEqual(original['message-id'], '<ant>')
@configuration('mailman', filtered_messages_are_preservable='no')
def test_dispose_non_preservable(self):
# Two actions can happen here, depending on a site-wide setting. If
# the site owner has indicated that filtered messages cannot be
# preserved, then this is the same as discarding them.
self._mlist.filter_action = FilterAction.preserve
with self.assertRaises(DiscardMessage) as cm:
mime_delete.dispose(self._mlist, self._msg, {}, 'not preserved')
self.assertEqual(cm.exception.message, 'not preserved')
# There should be no messages in the 'bad' queue.
get_queue_messages('bad', expected_count=0)
@configuration('mailman', filtered_messages_are_preservable='yes')
def test_dispose_preservable(self):
# Two actions can happen here, depending on a site-wide setting. If
# the site owner has indicated that filtered messages can be
# preserved, then this is similar to discarding the message except
# that a copy is preserved in the 'bad' queue.
self._mlist.filter_action = FilterAction.preserve
with self.assertRaises(DiscardMessage) as cm:
mime_delete.dispose(self._mlist, self._msg, {}, 'preserved')
self.assertEqual(cm.exception.message, 'preserved')
# There should be no messages in the 'bad' queue.
items = get_queue_messages('bad', expected_count=1)
message = items[0].msg
self.assertEqual(message['subject'], 'A disposable message')
self.assertEqual(message['message-id'], '<ant>')
def test_bad_action(self):
# This should never happen, but what if it does?
# FilterAction.accept, FilterAction.hold, and FilterAction.defer are
# not valid. They are treated as discard actions, but the problem is
# also logged.
for action in (FilterAction.accept,
FilterAction.hold,
FilterAction.defer):
self._mlist.filter_action = action
mark = LogFileMark('mailman.error')
with self.assertRaises(DiscardMessage) as cm:
mime_delete.dispose(self._mlist, self._msg, {}, 'bad action')
self.assertEqual(cm.exception.message, 'bad action')
line = mark.readline()[:-1]
self.assertTrue(line.endswith(
'test@example.com invalid FilterAction: {}. '
'Treating as discard'.format(action.name)))
class TestHTMLFilter(unittest.TestCase):
"""Test the conversion of HTML to plaintext."""
layer = ConfigLayer
def setUp(self):
self._mlist = create_list('test@example.com')
self._mlist.convert_html_to_plaintext = True
self._mlist.filter_content = True
def test_convert_html_to_plaintext(self):
# Converting to plain text calls a command line script.
msg = mfs("""\
From: aperson@example.com
Content-Type: text/html
MIME-Version: 1.0
<html><head></head>
<body></body></html>
""")
process = config.handlers['mime-delete'].process
with dummy_script():
process(self._mlist, msg, {})
self.assertEqual(msg.get_content_type(), 'text/plain')
self.assertTrue(
msg['x-content-filtered-by'].startswith('Mailman/MimeDel'))
payload_lines = msg.get_payload().splitlines()
self.assertEqual(payload_lines[0], 'Converted text/html to text/plain')
|