Merge lp:~abompard/mailman/link_on_creation into lp:mailman

Proposed by Aurélien Bompard
Status: Merged
Merge reported by: Barry Warsaw
Merged at revision: not available
Proposed branch: lp:~abompard/mailman/link_on_creation
Merge into: lp:mailman
Diff against target: 54 lines (+27/-4)
2 files modified
src/mailman/rest/tests/test_users.py (+14/-1)
src/mailman/rest/users.py (+13/-3)
To merge this branch: bzr merge lp:~abompard/mailman/link_on_creation
Reviewer Review Type Date Requested Status
Barry Warsaw Approve
Review via email: mp+225118@code.launchpad.net

Description of the change

In the REST interface, when creating a user with an existing address, the server currently replies with a Bad Request error. This changes creates the user and links the existing address to it.
A unit test is provided.

To post a comment you must log in.
Revision history for this message
Barry Warsaw (barry) wrote :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/mailman/rest/tests/test_users.py'
2--- src/mailman/rest/tests/test_users.py 2014-04-15 21:54:35 +0000
3+++ src/mailman/rest/tests/test_users.py 2014-07-01 08:25:55 +0000
4@@ -120,7 +120,20 @@
5 })
6 self.assertEqual(cm.exception.code, 400)
7 self.assertEqual(cm.exception.reason,
8- 'Address already exists: anne@example.com')
9+ 'User already exists: anne@example.com')
10+
11+ def test_existing_address_link(self):
12+ # Creating a user with an existing address links them
13+ user_manager = getUtility(IUserManager)
14+ with transaction():
15+ user_manager.create_address('anne@example.com')
16+ call_api('http://localhost:9001/3.0/users', {
17+ 'email': 'anne@example.com',
18+ })
19+ anne = user_manager.get_user('anne@example.com')
20+ self.assertNotEqual(anne, None)
21+ self.assertTrue('anne@example.com' in
22+ [ a.email for a in anne.addresses ])
23
24 def test_addresses_of_missing_user_id(self):
25 # Trying to get the /addresses of a missing user id results in error.
26
27=== modified file 'src/mailman/rest/users.py'
28--- src/mailman/rest/users.py 2014-04-28 15:23:35 +0000
29+++ src/mailman/rest/users.py 2014-07-01 08:25:55 +0000
30@@ -117,11 +117,21 @@
31 # so strip that out (if it exists), then create the user, adding the
32 # password after the fact if successful.
33 password = arguments.pop('password', None)
34+ user_manager = getUtility(IUserManager)
35 try:
36- user = getUtility(IUserManager).create_user(**arguments)
37+ user = user_manager.create_user(**arguments)
38 except ExistingAddressError as error:
39- return http.bad_request(
40- [], b'Address already exists: {0}'.format(error.address))
41+ user = user_manager.get_user(arguments["email"])
42+ if user is None:
43+ # existing address but no user: link it
44+ address = user_manager.get_address(arguments["email"])
45+ args_no_email = arguments.copy()
46+ del args_no_email["email"]
47+ user = user_manager.create_user(**args_no_email)
48+ user.link(address)
49+ else:
50+ return http.bad_request(
51+ [], b'User already exists: {0}'.format(error.address))
52 if password is None:
53 # This will have to be reset since it cannot be retrieved.
54 password = generate(int(config.passwords.password_length))