summaryrefslogtreecommitdiff
path: root/src/mailman/handlers/tests/test_file_recips.py
blob: 906530762a1581fe29c4c32dc69f4e5f98ae7f9b (plain)
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
# Copyright (C) 2014 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 file-recips handler."""

__all__ = [
    'TestFileRecips',
    ]


import os
import unittest

from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.testing.helpers import specialized_message_from_string as mfs
from mailman.testing.layers import ConfigLayer



class TestFileRecips(unittest.TestCase):
    layer = ConfigLayer

    def setUp(self):
        self._mlist = create_list('test@example.com')
        self._handler = config.handlers['file-recipients'].process
        self._msg = mfs("""\
From: aperson@example.com

A message.
""")

    def test_file_is_missing(self):
        # It is not an error for the list's the members.txt file to be
        # missing.  The missing file is just ignored.
        msgdata = {}
        self._handler(self._mlist, self._msg, msgdata)
        self.assertEqual(msgdata['recipients'], set())

    def test_file_exists(self):
        # Like above, but the file exists and contains recipients.
        path = os.path.join(self._mlist.data_path, 'members.txt')
        with open(path, 'w', encoding='utf-8') as fp:
            print('bperson@example.com', file=fp)
            print('cperson@example.com', file=fp)
            print('dperson@example.com', file=fp)
            print('eperson@example.com', file=fp)
            print('fperson@example.com', file=fp)
            print('gperson@example.com', file=fp)
        msgdata = {}
        self._handler(self._mlist, self._msg, msgdata)
        self.assertEqual(msgdata['recipients'], set((
            'bperson@example.com',
            'cperson@example.com',
            'dperson@example.com',
            'eperson@example.com',
            'fperson@example.com',
            'gperson@example.com',
            )))