~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/model/tests/test_listmanager.py

  • Committer: Barry Warsaw
  • Date: 2011-08-18 00:39:11 UTC
  • mfrom: (7036.1.4 bug-827036)
  • Revision ID: barry@list.org-20110818003911-9gef1p84g2pg4p10
 * Four new events are created, and notifications are sent during mailing list
   lifecycle changes:
   - ListCreatingEvent - sent before the mailing list is created
   - ListCreatedEvent  - sent after the mailing list is created
   - ListDeletingEvent - sent before the mailing list is deleted
   - ListDeletedEvent  - sent after the mailing list is deleted
 * Using the above events, when a mailing list is deleted, all its members are
   deleted, as well as all held message requests (but not the held messages
   themselves).  (LP: 827036)

Also: relax the find_member() argument constraints so that even the subscriber
email address is optional.  This is mirrored in the REST API's
.../members/find resource.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 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 the ListManager."""
 
19
 
 
20
from __future__ import absolute_import, unicode_literals
 
21
 
 
22
__metaclass__ = type
 
23
__all__ = [
 
24
    'test_suite',
 
25
    ]
 
26
 
 
27
 
 
28
import unittest
 
29
 
 
30
from zope.component import getUtility
 
31
 
 
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,
 
36
    ListDeletingEvent)
 
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
 
44
 
 
45
 
 
46
 
 
47
class TestListManager(unittest.TestCase):
 
48
    layer = ConfigLayer
 
49
 
 
50
    def setUp(self):
 
51
        self._events = []
 
52
 
 
53
    def _record_event(self, event):
 
54
        self._events.append(event)
 
55
 
 
56
    def test_create_list_event(self):
 
57
        # Test that creating a list in the list manager propagates the
 
58
        # expected events.
 
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)
 
66
 
 
67
    def test_delete_list_event(self):
 
68
        # Test that deleting a list in the list manager propagates the
 
69
        # expected event.
 
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')
 
78
 
 
79
 
 
80
 
 
81
class TestListLifecycleEvents(unittest.TestCase):
 
82
    layer = ConfigLayer
 
83
 
 
84
    def setUp(self):
 
85
        self._ant = create_list('ant@example.com')
 
86
        self._bee = create_list('bee@example.com')
 
87
        self._usermanager = getUtility(IUserManager)
 
88
 
 
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
 
91
        # are also deleted.
 
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)
 
108
 
 
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
 
112
        # themselves).
 
113
        msg = specialized_message_from_string("""\
 
114
From: anne@example.com
 
115
To: ant@example.com
 
116
Subject: Hold me
 
117
Message-ID: <argon>
 
118
 
 
119
""")
 
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())
 
129
 
 
130
 
 
131
 
 
132
def test_suite():
 
133
    suite = unittest.TestSuite()
 
134
    suite.addTest(unittest.makeSuite(TestListManager))
 
135
    suite.addTest(unittest.makeSuite(TestListLifecycleEvents))
 
136
    return suite