1
# Copyright (C) 2012 by the Free Software Foundation, Inc.
3
# This file is part of GNU Mailman.
5
# GNU Mailman is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free
7
# Software Foundation, either version 3 of the License, or (at your option)
10
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15
# You should have received a copy of the GNU General Public License along with
16
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18
"""Test various aspects of email delivery."""
20
from __future__ import absolute_import, print_function, unicode_literals
24
'TestIndividualDelivery',
33
from mailman.app.lifecycle import create_list
34
from mailman.app.membership import add_member
35
from mailman.config import config
36
from mailman.interfaces.mailinglist import Personalization
37
from mailman.interfaces.member import DeliveryMode
38
from mailman.mta.deliver import Deliver
39
from mailman.testing.helpers import (
40
specialized_message_from_string as mfs)
41
from mailman.testing.layers import ConfigLayer
45
# Global test capture.
48
# Derive this from the default individual delivery class. The point being
49
# that we don't want to *actually* attempt delivery of the message to the MTA,
50
# we just want to capture the messages and metadata dictionaries for
52
class DeliverTester(Deliver):
53
def _deliver_to_recipients(self, mlist, msg, msgdata, recipients):
54
_deliveries.append((mlist, msg, msgdata, recipients))
55
# Nothing gets refused.
60
class TestIndividualDelivery(unittest.TestCase):
61
"""Test personalized delivery details."""
66
self._mlist = create_list('test@example.com')
67
self._mlist.personalize = Personalization.individual
68
# Make Anne a member of this mailing list.
69
self._anne = add_member(self._mlist,
70
'anne@example.org', 'Anne Person',
71
'xyz', DeliveryMode.regular, 'en')
72
# Clear out any results from the previous test.
75
From: anne@example.org
80
# Set up a personalized footer for decoration.
81
self._template_dir = tempfile.mkdtemp()
82
path = os.path.join(self._template_dir,
83
'site', 'en', 'member-footer.txt')
84
os.makedirs(os.path.dirname(path))
85
with open(path, 'w') as fp:
87
address : $user_address
88
delivered: $user_delivered_to
89
language : $user_language
91
options : $user_optionsurl
93
config.push('templates', """
96
""".format(self._template_dir))
97
self._mlist.footer_uri = 'mailman:///member-footer.txt'
98
# Python 2.7 has assertMultiLineEqual. Let this work without bounds.
100
self.eq = getattr(self, 'assertMultiLineEqual', self.assertEqual)
105
shutil.rmtree(self._template_dir)
106
config.pop('templates')
108
def test_member_key(self):
109
# 'personalize' should end up in the metadata dictionary so that
110
# $user_* keys in headers and footers get filled in correctly.
111
msgdata = dict(recipients=['anne@example.org'])
112
agent = DeliverTester()
113
refused = agent.deliver(self._mlist, self._msg, msgdata)
114
self.assertEqual(len(refused), 0)
115
self.assertEqual(len(_deliveries), 1)
116
_mlist, _msg, _msgdata, _recipients = _deliveries[0]
117
member = _msgdata.get('member')
118
self.assertEqual(member, self._anne)
120
def test_decoration(self):
121
msgdata = dict(recipients=['anne@example.org'])
122
agent = DeliverTester()
123
refused = agent.deliver(self._mlist, self._msg, msgdata)
124
self.assertEqual(len(refused), 0)
125
self.assertEqual(len(_deliveries), 1)
126
_mlist, _msg, _msgdata, _recipients = _deliveries[0]
127
self.eq(_msg.as_string(), """\
128
From: anne@example.org
132
Content-Type: text/plain; charset="us-ascii"
133
Content-Transfer-Encoding: 7bit
136
address : anne@example.org
137
delivered: anne@example.org
138
language : English (USA)
140
options : http://example.com/anne@example.org