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
|
# Copyright (C) 2016-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 `banned-address` rule."""
import unittest
from mailman.app.lifecycle import create_list
from mailman.interfaces.bans import IBanManager
from mailman.interfaces.usermanager import IUserManager
from mailman.rules import banned_address
from mailman.testing.helpers import (
set_preferred, specialized_message_from_string as mfs)
from mailman.testing.layers import ConfigLayer
from zope.component import getUtility
class TestBannedAddress(unittest.TestCase):
"""Test the banned-address rule."""
layer = ConfigLayer
def setUp(self):
self._mlist = create_list('test@example.com')
def test_no_banned_sender(self):
# Simple case where the sender is not banned.
user_manager = getUtility(IUserManager)
anne = user_manager.create_user('anne@example.com')
set_preferred(anne)
msg = mfs("""\
From: anne@example.com
To: test@example.com
Subject: A test message
Message-ID: <ant>
MIME-Version: 1.0
A message body.
""")
rule = banned_address.BannedAddress()
result = rule.check(self._mlist, msg, {})
self.assertFalse(result)
def test_simple_banned_sender(self):
# Simple case where the sender is banned.
user_manager = getUtility(IUserManager)
anne = user_manager.create_user('anne@example.com')
set_preferred(anne)
IBanManager(self._mlist).ban('anne@example.com')
msg = mfs("""\
From: anne@example.com
To: test@example.com
Subject: A test message
Message-ID: <ant>
MIME-Version: 1.0
A message body.
""")
rule = banned_address.BannedAddress()
result = rule.check(self._mlist, msg, {})
self.assertTrue(result)
def test_rule_returns_reason(self):
# Ensure a reason is returned.
user_manager = getUtility(IUserManager)
anne = user_manager.create_user('anne@example.com')
set_preferred(anne)
IBanManager(self._mlist).ban('anne@example.com')
msg = mfs("""\
From: anne@example.com
To: test@example.com
Subject: A test message
Message-ID: <ant>
MIME-Version: 1.0
A message body.
""")
rule = banned_address.BannedAddress()
msgdata = {}
result = rule.check(self._mlist, msg, msgdata)
self.assertTrue(result)
self.assertEqual(
msgdata['moderation_reasons'],
[('Message sender {} is banned from this list',
'anne@example.com')])
def test_banned_address_linked_to_user(self):
# Anne is subscribed to a mailing list as a user with her preferred
# address. She also has a secondary address which is banned and which
# she uses to post to the mailing list. The rule matches because the
# posting address is banned.
user_manager = getUtility(IUserManager)
anne = user_manager.create_user('anne@example.com')
set_preferred(anne)
anne.link(user_manager.create_address('anne.person@example.com'))
IBanManager(self._mlist).ban('anne.person@example.com')
msg = mfs("""\
From: anne.person@example.com
To: test@example.com
Subject: A test message
Message-ID: <ant>
MIME-Version: 1.0
A message body.
""")
rule = banned_address.BannedAddress()
result = rule.check(self._mlist, msg, {})
self.assertTrue(result)
def test_banned_sender_among_multiple_senders(self):
# Two addresses are created, one of which is banned. The rule matches
# because all senders are checked.
user_manager = getUtility(IUserManager)
user_manager.create_address('anne@example.com')
user_manager.create_address('bart@example.com')
IBanManager(self._mlist).ban('bart@example.com')
msg = mfs("""\
From: anne@example.com
Sender: bart@example.com
To: test@example.com
Subject: A test message
Message-ID: <ant>
MIME-Version: 1.0
A message body.
""")
rule = banned_address.BannedAddress()
result = rule.check(self._mlist, msg, {})
self.assertTrue(result)
|