summaryrefslogtreecommitdiff
path: root/src/mailman/mta/tests/test_aliases.py
blob: b1a60bc958c38b1a97f7028f8549b4c815584038 (plain) (blame)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Copyright (C) 2011-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 template generating utility."""

from __future__ import absolute_import, unicode_literals

__metaclass__ = type
__all__ = [
    ]


import unittest

from cStringIO import StringIO
from zope.component import getUtility

from mailman.app.lifecycle import create_list
from mailman.interfaces.mta import IMailTransportAgentAliases
from mailman.mta.postfix import LMTP
from mailman.testing.layers import ConfigLayer

NL = '\n'



class TestAliases(unittest.TestCase):

    layer = ConfigLayer

    def setUp(self):
        self.utility = getUtility(IMailTransportAgentAliases)
        self.mlist = create_list('test@example.com')

    def test_posting_address_first(self):
        # The posting address is always first.
        aliases = list(self.utility.aliases(self.mlist))
        self.assertEqual(aliases[0], self.mlist.posting_address)

    def test_aliases(self):
        # The aliases are the fully qualified email addresses.
        aliases = list(self.utility.aliases(self.mlist))
        self.assertEqual(aliases, [
            'test@example.com',
            'test-bounces@example.com',
            'test-confirm@example.com',
            'test-join@example.com',
            'test-leave@example.com',
            'test-owner@example.com',
            'test-request@example.com',
            'test-subscribe@example.com',
            'test-unsubscribe@example.com',
            ])

    def test_destinations(self):
        # The destinations are just the local part.
        destinations = list(self.utility.destinations(self.mlist))
        self.assertEqual(destinations, [
            'test',
            'test-bounces',
            'test-confirm',
            'test-join',
            'test-leave',
            'test-owner',
            'test-request',
            'test-subscribe',
            'test-unsubscribe',
            ])

    def test_duck_typed_aliases(self):
        # Test the .aliases() method with duck typed arguments.
        class Duck:
            def __init__(self, list_name, mail_host):
                self.list_name = list_name
                self.mail_host = mail_host
                self.posting_address = '{0}@{1}'.format(list_name, mail_host)
        duck_list = Duck('sample', 'example.net')
        aliases = list(self.utility.aliases(duck_list))
        self.assertEqual(aliases, [
            'sample@example.net',
            'sample-bounces@example.net',
            'sample-confirm@example.net',
            'sample-join@example.net',
            'sample-leave@example.net',
            'sample-owner@example.net',
            'sample-request@example.net',
            'sample-subscribe@example.net',
            'sample-unsubscribe@example.net',
            ])

    def test_duck_typed_destinations(self):
        # Test the .destinations() method with duck typed arguments.
        class Duck:
            def __init__(self, list_name):
                self.list_name = list_name
        duck_list = Duck('sample')
        destinations = list(self.utility.destinations(duck_list))
        self.assertEqual(destinations, [
            'sample',
            'sample-bounces',
            'sample-confirm',
            'sample-join',
            'sample-leave',
            'sample-owner',
            'sample-request',
            'sample-subscribe',
            'sample-unsubscribe',
            ])



class TestPostfix(unittest.TestCase):
    """Test the Postfix LMTP alias generator."""

    layer = ConfigLayer

    def setUp(self):
        self.utility = getUtility(IMailTransportAgentAliases)
        self.mlist = create_list('test@example.com')
        self.output = StringIO()
        self.postfix = LMTP()
        # Python 2.7 has assertMultiLineEqual.  Let this work without bounds.
        self.maxDiff = None
        self.eq = getattr(self, 'assertMultiLineEqual', self.assertEqual)

    def test_aliases(self):
        # Test the format of the Postfix alias generator.
        self.postfix.regenerate(self.output)
        # Strip out the variable and unimportant bits of the output.
        lines = self.output.getvalue().splitlines()
        output = NL.join(lines[7:])
        self.eq(output, """\
# Aliases which are visible only in the @example.com domain.
test@example.com               lmtp:[127.0.0.1]:9024
test-bounces@example.com       lmtp:[127.0.0.1]:9024
test-confirm@example.com       lmtp:[127.0.0.1]:9024
test-join@example.com          lmtp:[127.0.0.1]:9024
test-leave@example.com         lmtp:[127.0.0.1]:9024
test-owner@example.com         lmtp:[127.0.0.1]:9024
test-request@example.com       lmtp:[127.0.0.1]:9024
test-subscribe@example.com     lmtp:[127.0.0.1]:9024
test-unsubscribe@example.com   lmtp:[127.0.0.1]:9024
""")

    def test_two_lists(self):
        # Both lists need to show up in the aliases file.  LP: #874929.
        # Create a second list.
        create_list('other@example.com')
        self.postfix.regenerate(self.output)
        # Strip out the variable and unimportant bits of the output.
        lines = self.output.getvalue().splitlines()
        output = NL.join(lines[7:])
        self.eq(output, """\
# Aliases which are visible only in the @example.com domain.
other@example.com               lmtp:[127.0.0.1]:9024
other-bounces@example.com       lmtp:[127.0.0.1]:9024
other-confirm@example.com       lmtp:[127.0.0.1]:9024
other-join@example.com          lmtp:[127.0.0.1]:9024
other-leave@example.com         lmtp:[127.0.0.1]:9024
other-owner@example.com         lmtp:[127.0.0.1]:9024
other-request@example.com       lmtp:[127.0.0.1]:9024
other-subscribe@example.com     lmtp:[127.0.0.1]:9024
other-unsubscribe@example.com   lmtp:[127.0.0.1]:9024

test@example.com               lmtp:[127.0.0.1]:9024
test-bounces@example.com       lmtp:[127.0.0.1]:9024
test-confirm@example.com       lmtp:[127.0.0.1]:9024
test-join@example.com          lmtp:[127.0.0.1]:9024
test-leave@example.com         lmtp:[127.0.0.1]:9024
test-owner@example.com         lmtp:[127.0.0.1]:9024
test-request@example.com       lmtp:[127.0.0.1]:9024
test-subscribe@example.com     lmtp:[127.0.0.1]:9024
test-unsubscribe@example.com   lmtp:[127.0.0.1]:9024
""")