~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/mta/tests/test_delivery.py

  • Committer: klm
  • Date: 1998-01-07 21:21:35 UTC
  • Revision ID: vcs-imports@canonical.com-19980107212135-sv0y521ps0xye37r
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2012 by the Free Software Foundation, Inc.
2
 
#
3
 
# This file is part of GNU Mailman.
4
 
#
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)
8
 
# any later version.
9
 
#
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
13
 
# more details.
14
 
#
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/>.
17
 
 
18
 
"""Test various aspects of email delivery."""
19
 
 
20
 
from __future__ import absolute_import, print_function, unicode_literals
21
 
 
22
 
__metaclass__ = type
23
 
__all__ = [
24
 
    'TestIndividualDelivery',
25
 
    ]
26
 
 
27
 
 
28
 
import os
29
 
import shutil
30
 
import tempfile
31
 
import unittest
32
 
 
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
42
 
 
43
 
 
44
 
 
45
 
# Global test capture.
46
 
_deliveries = []
47
 
 
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
51
 
# inspection.
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.
56
 
        return []
57
 
 
58
 
 
59
 
 
60
 
class TestIndividualDelivery(unittest.TestCase):
61
 
    """Test personalized delivery details."""
62
 
 
63
 
    layer = ConfigLayer
64
 
 
65
 
    def setUp(self):
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.
73
 
        del _deliveries[:]
74
 
        self._msg = mfs("""\
75
 
From: anne@example.org
76
 
To: test@example.com
77
 
Subject: test
78
 
 
79
 
""")
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:
86
 
            print("""\
87
 
address  : $user_address
88
 
delivered: $user_delivered_to
89
 
language : $user_language
90
 
name     : $user_name
91
 
options  : $user_optionsurl
92
 
""", file=fp)
93
 
        config.push('templates', """
94
 
        [paths.testing]
95
 
        template_dir: {0}
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.
99
 
        self.maxDiff = None
100
 
        self.eq = getattr(self, 'assertMultiLineEqual', self.assertEqual)
101
 
 
102
 
    def tearDown(self):
103
 
        # Free references.
104
 
        del _deliveries[:]
105
 
        shutil.rmtree(self._template_dir)
106
 
        config.pop('templates')
107
 
 
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)
119
 
 
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
129
 
To: test@example.com
130
 
Subject: test
131
 
MIME-Version: 1.0
132
 
Content-Type: text/plain; charset="us-ascii"
133
 
Content-Transfer-Encoding: 7bit
134
 
 
135
 
 
136
 
address  : anne@example.org
137
 
delivered: anne@example.org
138
 
language : English (USA)
139
 
name     : Anne Person
140
 
options  : http://example.com/anne@example.org
141
 
 
142
 
""")