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
|
# Copyright (C) 2012 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 core modification pipelines."""
from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
]
import unittest
from zope.interface import implements
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.core.errors import DiscardMessage, RejectMessage
from mailman.core.pipelines import process
from mailman.interfaces.handler import IHandler
from mailman.interfaces.pipeline import IPipeline
from mailman.testing.helpers import (
LogFileMark,
get_queue_messages,
reset_the_world,
specialized_message_from_string as mfs)
from mailman.testing.layers import ConfigLayer
class DiscardingHandler:
implements(IHandler)
name = 'discarding'
def process(self, mlist, msg, msgdata):
raise DiscardMessage('by test handler')
class RejectHandler:
implements(IHandler)
name = 'rejecting'
def process(self, mlist, msg, msgdata):
raise RejectMessage('by test handler')
class DiscardingPipeline:
implements(IPipeline)
name = 'test-discarding'
description = 'Discarding test pipeline'
def __iter__(self):
yield DiscardingHandler()
class RejectingPipeline:
implements(IPipeline)
name = 'test-rejecting'
description = 'Rejectinging test pipeline'
def __iter__(self):
yield RejectHandler()
class TestBuiltinPipeline(unittest.TestCase):
"""Test various aspects of the built-in postings pipeline."""
layer = ConfigLayer
def setUp(self):
self._mlist = create_list('test@example.com')
config.pipelines['test-discarding'] = DiscardingPipeline()
config.pipelines['test-rejecting'] = RejectingPipeline()
self._msg = mfs("""\
From: Anne Person <anne@example.org>
To: test@example.com
Subject: a test
Message-ID: <ant>
testing
""")
def tearDown(self):
reset_the_world()
del config.pipelines['test-discarding']
del config.pipelines['test-rejecting']
def test_rfc2369_headers(self):
# Ensure that RFC 2369 List-* headers are added.
msgdata = {}
process(self._mlist, self._msg, msgdata,
pipeline_name='default-posting-pipeline')
self.assertEqual(self._msg['list-id'], '<test.example.com>')
self.assertEqual(self._msg['list-post'], '<mailto:test@example.com>')
def test_discarding_pipeline(self):
# If a handler in the pipeline raises DiscardMessage, the message will
# be thrown away, but with a log message.
mark = LogFileMark('mailman.vette')
process(self._mlist, self._msg, {}, 'test-discarding')
line = mark.readline()[:-1]
self.assertTrue(line.endswith(
'<ant> discarded by "test-discarding" pipeline handler '
'"discarding": by test handler'))
def test_rejecting_pipeline(self):
# If a handler in the pipeline raises DiscardMessage, the message will
# be thrown away, but with a log message.
mark = LogFileMark('mailman.vette')
process(self._mlist, self._msg, {}, 'test-rejecting')
line = mark.readline()[:-1]
self.assertTrue(line.endswith(
'<ant> rejected by "test-rejecting" pipeline handler '
'"rejecting": by test handler'))
# In the rejection case, the original message will also be in the
# virgin queue.
messages = get_queue_messages('virgin')
self.assertEqual(len(messages), 1)
self.assertEqual(str(messages[0].msg['subject']), 'a test')
|