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
|
# Copyright (C) 2012-2015 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 various aspects of email delivery."""
__all__ = [
'TestIndividualDelivery',
]
import os
import shutil
import tempfile
import unittest
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.interfaces.mailinglist import Personalization
from mailman.mta.deliver import Deliver
from mailman.testing.helpers import (
specialized_message_from_string as mfs, subscribe)
from mailman.testing.layers import ConfigLayer
# Global test capture.
_deliveries = []
# Derive this from the default individual delivery class. The point being
# that we don't want to *actually* attempt delivery of the message to the MTA,
# we just want to capture the messages and metadata dictionaries for
# inspection.
class DeliverTester(Deliver):
def _deliver_to_recipients(self, mlist, msg, msgdata, recipients):
_deliveries.append((mlist, msg, msgdata, recipients))
# Nothing gets refused.
return []
class TestIndividualDelivery(unittest.TestCase):
"""Test personalized delivery details."""
layer = ConfigLayer
def setUp(self):
self._mlist = create_list('test@example.com')
self._mlist.personalize = Personalization.individual
# Make Anne a member of this mailing list.
self._anne = subscribe(self._mlist, 'Anne', email='anne@example.org')
# Clear out any results from the previous test.
del _deliveries[:]
self._msg = mfs("""\
From: anne@example.org
To: test@example.com
Subject: test
""")
# Set up a personalized footer for decoration.
self._template_dir = tempfile.mkdtemp()
path = os.path.join(self._template_dir,
'site', 'en', 'member-footer.txt')
os.makedirs(os.path.dirname(path))
with open(path, 'w') as fp:
print("""\
address : $user_address
delivered: $user_delivered_to
language : $user_language
name : $user_name
options : $user_optionsurl
""", file=fp)
config.push('templates', """
[paths.testing]
template_dir: {0}
""".format(self._template_dir))
self._mlist.footer_uri = 'mailman:///member-footer.txt'
# Let assertMultiLineEqual work without bounds.
self.maxDiff = None
def tearDown(self):
# Free references.
del _deliveries[:]
shutil.rmtree(self._template_dir)
config.pop('templates')
def test_member_key(self):
# 'personalize' should end up in the metadata dictionary so that
# $user_* keys in headers and footers get filled in correctly.
msgdata = dict(recipients=['anne@example.org'])
agent = DeliverTester()
refused = agent.deliver(self._mlist, self._msg, msgdata)
self.assertEqual(len(refused), 0)
self.assertEqual(len(_deliveries), 1)
_mlist, _msg, _msgdata, _recipients = _deliveries[0]
member = _msgdata.get('member')
self.assertEqual(member, self._anne)
def test_decoration(self):
msgdata = dict(recipients=['anne@example.org'])
agent = DeliverTester()
refused = agent.deliver(self._mlist, self._msg, msgdata)
self.assertEqual(len(refused), 0)
self.assertEqual(len(_deliveries), 1)
_mlist, _msg, _msgdata, _recipients = _deliveries[0]
self.assertMultiLineEqual(_msg.as_string(), """\
From: anne@example.org
To: test@example.com
Subject: test
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
address : anne@example.org
delivered: anne@example.org
language : English (USA)
name : Anne Person
options : http://example.com/anne@example.org
""")
|