~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/rest/tests/test_membership.py

  • Committer: Barry Warsaw
  • Date: 2012-04-22 21:33:33 UTC
  • mfrom: (7150.1.11 transactions)
  • Revision ID: barry@list.org-20120422213333-3skjqsjktooesgsl
Several non-functional improvements to the code base.

Reduce the explicit use of the config.db global by introducing two new
helpers:
 - A new transaction() context manager which will commit the transaction on
   successful exit, otherwise it will abort the transaction
 - A new dbconnection decorator which calls the decorated method with the
   Storm store object as the first argument (after self).  This can be used
   instead of config.db.store.

By reducing the explicit use of this global, we have a better chance of
refactoring it away in the future.  Still TODO: runner.py and lmtp.py.

Be explicit about the `store` attribute on the IDatabase interface.

More consistent use of __future__ imports.

Remove an obsolete command line script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""REST membership tests."""
19
19
 
20
 
from __future__ import absolute_import, unicode_literals
 
20
from __future__ import absolute_import, print_function, unicode_literals
21
21
 
22
22
__metaclass__ = type
23
23
__all__ = [
 
24
    'TestMembership',
24
25
    ]
25
26
 
26
27
 
31
32
 
32
33
from mailman.app.lifecycle import create_list
33
34
from mailman.config import config
 
35
from mailman.database.transaction import transaction
34
36
from mailman.interfaces.usermanager import IUserManager
35
37
from mailman.testing.helpers import call_api
36
38
from mailman.testing.layers import RESTLayer
42
44
    layer = RESTLayer
43
45
 
44
46
    def setUp(self):
45
 
        self._mlist = create_list('test@example.com')
46
 
        config.db.commit()
 
47
        with transaction():
 
48
            self._mlist = create_list('test@example.com')
47
49
        self._usermanager = getUtility(IUserManager)
48
50
 
49
51
    def test_try_to_join_missing_list(self):
85
87
            raise AssertionError('Expected HTTPError')
86
88
 
87
89
    def test_try_to_leave_a_list_twice(self):
88
 
        anne = self._usermanager.create_address('anne@example.com')
89
 
        self._mlist.subscribe(anne)
90
 
        config.db.commit()
 
90
        with transaction():
 
91
            anne = self._usermanager.create_address('anne@example.com')
 
92
            self._mlist.subscribe(anne)
91
93
        url = 'http://localhost:9001/3.0/members/1'
92
94
        content, response = call_api(url, method='DELETE')
93
95
        # For a successful DELETE, the response code is 204 and there is no
104
106
            raise AssertionError('Expected HTTPError')
105
107
 
106
108
    def test_try_to_join_a_list_twice(self):
107
 
        anne = self._usermanager.create_address('anne@example.com')
108
 
        self._mlist.subscribe(anne)
109
 
        config.db.commit()
 
109
        with transaction():
 
110
            anne = self._usermanager.create_address('anne@example.com')
 
111
            self._mlist.subscribe(anne)
110
112
        try:
111
113
            # For Python 2.6.
112
114
            call_api('http://localhost:9001/3.0/members', {
151
153
        self.assertEqual(members[0].address.email, 'hugh/person@example.com')
152
154
 
153
155
    def test_join_as_user_with_preferred_address(self):
154
 
        anne = self._usermanager.create_user('anne@example.com')
155
 
        preferred = list(anne.addresses)[0]
156
 
        preferred.verified_on = now()
157
 
        anne.preferred_address = preferred
158
 
        self._mlist.subscribe(anne)
159
 
        config.db.commit()
 
156
        with transaction():
 
157
            anne = self._usermanager.create_user('anne@example.com')
 
158
            preferred = list(anne.addresses)[0]
 
159
            preferred.verified_on = now()
 
160
            anne.preferred_address = preferred
 
161
            self._mlist.subscribe(anne)
160
162
        content, response = call_api('http://localhost:9001/3.0/members')
161
163
        self.assertEqual(response.status, 200)
162
164
        self.assertEqual(int(content['total_size']), 1)
169
171
        self.assertEqual(entry_0['fqdn_listname'], 'test@example.com')
170
172
 
171
173
    def test_member_changes_preferred_address(self):
172
 
        anne = self._usermanager.create_user('anne@example.com')
173
 
        preferred = list(anne.addresses)[0]
174
 
        preferred.verified_on = now()
175
 
        anne.preferred_address = preferred
176
 
        self._mlist.subscribe(anne)
177
 
        config.db.commit()
 
174
        with transaction():
 
175
            anne = self._usermanager.create_user('anne@example.com')
 
176
            preferred = list(anne.addresses)[0]
 
177
            preferred.verified_on = now()
 
178
            anne.preferred_address = preferred
 
179
            self._mlist.subscribe(anne)
178
180
        # Take a look at Anne's current membership.
179
181
        content, response = call_api('http://localhost:9001/3.0/members')
180
182
        self.assertEqual(int(content['total_size']), 1)
182
184
        self.assertEqual(entry_0['address'], 'anne@example.com')
183
185
        # Anne registers a new address and makes it her preferred address.
184
186
        # There are no changes to her membership.
185
 
        new_preferred = anne.register('aperson@example.com')
186
 
        new_preferred.verified_on = now()
187
 
        anne.preferred_address = new_preferred
188
 
        config.db.commit()
 
187
        with transaction():
 
188
            new_preferred = anne.register('aperson@example.com')
 
189
            new_preferred.verified_on = now()
 
190
            anne.preferred_address = new_preferred
189
191
        # Take another look at Anne's current membership.
190
192
        content, response = call_api('http://localhost:9001/3.0/members')
191
193
        self.assertEqual(int(content['total_size']), 1)
214
216
 
215
217
    def test_patch_member_bogus_attribute(self):
216
218
        # /members/<id> PATCH 'bogus' returns 400
217
 
        anne = self._usermanager.create_address('anne@example.com')
218
 
        self._mlist.subscribe(anne)
219
 
        config.db.commit()
 
219
        with transaction():
 
220
            anne = self._usermanager.create_address('anne@example.com')
 
221
            self._mlist.subscribe(anne)
220
222
        try:
221
223
            # For Python 2.6
222
224
            call_api('http://localhost:9001/3.0/members/1', {