1
# Copyright (C) 2011 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 the ListManager."""
20
from __future__ import absolute_import, unicode_literals
30
from zope.component import getUtility
32
from mailman.app.lifecycle import create_list
33
from mailman.app.moderator import hold_message
34
from mailman.interfaces.listmanager import (
35
IListManager, ListCreatedEvent, ListCreatingEvent, ListDeletedEvent,
37
from mailman.interfaces.messages import IMessageStore
38
from mailman.interfaces.requests import IRequests
39
from mailman.interfaces.subscriptions import ISubscriptionService
40
from mailman.interfaces.usermanager import IUserManager
41
from mailman.testing.helpers import (
42
event_subscribers, specialized_message_from_string)
43
from mailman.testing.layers import ConfigLayer
47
class TestListManager(unittest.TestCase):
53
def _record_event(self, event):
54
self._events.append(event)
56
def test_create_list_event(self):
57
# Test that creating a list in the list manager propagates the
59
with event_subscribers(self._record_event):
60
mlist = getUtility(IListManager).create('test@example.com')
61
self.assertEqual(len(self._events), 2)
62
self.assertTrue(isinstance(self._events[0], ListCreatingEvent))
63
self.assertEqual(self._events[0].fqdn_listname, 'test@example.com')
64
self.assertTrue(isinstance(self._events[1], ListCreatedEvent))
65
self.assertEqual(self._events[1].mailing_list, mlist)
67
def test_delete_list_event(self):
68
# Test that deleting a list in the list manager propagates the
70
mlist = create_list('another@example.com')
71
with event_subscribers(self._record_event):
72
getUtility(IListManager).delete(mlist)
73
self.assertEqual(len(self._events), 2)
74
self.assertTrue(isinstance(self._events[0], ListDeletingEvent))
75
self.assertEqual(self._events[0].mailing_list, mlist)
76
self.assertTrue(isinstance(self._events[1], ListDeletedEvent))
77
self.assertEqual(self._events[1].fqdn_listname, 'another@example.com')
81
class TestListLifecycleEvents(unittest.TestCase):
85
self._ant = create_list('ant@example.com')
86
self._bee = create_list('bee@example.com')
87
self._usermanager = getUtility(IUserManager)
89
def test_members_are_deleted_when_mailing_list_is_deleted(self):
90
# When a mailing list with members is deleted, all the Member records
92
anne = self._usermanager.create_address('anne@example.com')
93
bart = self._usermanager.create_address('bart@example.com')
94
anne_ant = self._ant.subscribe(anne)
95
anne_bee = self._bee.subscribe(anne)
96
bart_ant = self._ant.subscribe(bart)
97
anne_ant_id = anne_ant.member_id
98
anne_bee_id = anne_bee.member_id
99
bart_ant_id = bart_ant.member_id
100
getUtility(IListManager).delete(self._ant)
101
service = getUtility(ISubscriptionService)
102
# We deleted the ant@example.com mailing list. Anne's and Bart's
103
# membership in this list should now be removed, but Anne's membership
104
# in bee@example.com should still exist.
105
self.assertEqual(service.get_member(anne_ant_id), None)
106
self.assertEqual(service.get_member(bart_ant_id), None)
107
self.assertEqual(service.get_member(anne_bee_id), anne_bee)
109
def test_requests_are_deleted_when_mailing_list_is_deleted(self):
110
# When a mailing list is deleted, its requests database is deleted
111
# too, e.g. all its message hold requests (but not the messages
113
msg = specialized_message_from_string("""\
114
From: anne@example.com
120
request_id = hold_message(self._ant, msg)
121
getUtility(IListManager).delete(self._ant)
122
# This is a hack. ListRequests don't access self._mailinglist in
123
# their get_request() method.
124
requestsdb = getUtility(IRequests).get_list_requests(None)
125
request = requestsdb.get_request(request_id)
126
self.assertEqual(request, None)
127
saved_message = getUtility(IMessageStore).get_message_by_id('<argon>')
128
self.assertEqual(saved_message.as_string(), msg.as_string())
133
suite = unittest.TestSuite()
134
suite.addTest(unittest.makeSuite(TestListManager))
135
suite.addTest(unittest.makeSuite(TestListLifecycleEvents))