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
=== modified file 'src/mailman/rest/tests/test_users.py'
--- src/mailman/rest/tests/test_users.py 2014-04-15 21:54:35 +0000
+++ src/mailman/rest/tests/test_users.py 2014-07-01 08:25:55 +0000
@@ -120,7 +120,20 @@
120 })120 })
121 self.assertEqual(cm.exception.code, 400)121 self.assertEqual(cm.exception.code, 400)
122 self.assertEqual(cm.exception.reason,122 self.assertEqual(cm.exception.reason,
123 'Address already exists: anne@example.com')123 'User already exists: anne@example.com')
124
125 def test_existing_address_link(self):
126 # Creating a user with an existing address links them
127 user_manager = getUtility(IUserManager)
128 with transaction():
129 user_manager.create_address('anne@example.com')
130 call_api('http://localhost:9001/3.0/users', {
131 'email': 'anne@example.com',
132 })
133 anne = user_manager.get_user('anne@example.com')
134 self.assertNotEqual(anne, None)
135 self.assertTrue('anne@example.com' in
136 [ a.email for a in anne.addresses ])
124137
125 def test_addresses_of_missing_user_id(self):138 def test_addresses_of_missing_user_id(self):
126 # Trying to get the /addresses of a missing user id results in error.139 # Trying to get the /addresses of a missing user id results in error.
127140
=== modified file 'src/mailman/rest/users.py'
--- src/mailman/rest/users.py 2014-04-28 15:23:35 +0000
+++ src/mailman/rest/users.py 2014-07-01 08:25:55 +0000
@@ -117,11 +117,21 @@
117 # so strip that out (if it exists), then create the user, adding the117 # so strip that out (if it exists), then create the user, adding the
118 # password after the fact if successful.118 # password after the fact if successful.
119 password = arguments.pop('password', None)119 password = arguments.pop('password', None)
120 user_manager = getUtility(IUserManager)
120 try:121 try:
121 user = getUtility(IUserManager).create_user(**arguments)122 user = user_manager.create_user(**arguments)
122 except ExistingAddressError as error:123 except ExistingAddressError as error:
123 return http.bad_request(124 user = user_manager.get_user(arguments["email"])
124 [], b'Address already exists: {0}'.format(error.address))125 if user is None:
126 # existing address but no user: link it
127 address = user_manager.get_address(arguments["email"])
128 args_no_email = arguments.copy()
129 del args_no_email["email"]
130 user = user_manager.create_user(**args_no_email)
131 user.link(address)
132 else:
133 return http.bad_request(
134 [], b'User already exists: {0}'.format(error.address))
125 if password is None:135 if password is None:
126 # This will have to be reset since it cannot be retrieved.136 # This will have to be reset since it cannot be retrieved.
127 password = generate(int(config.passwords.password_length))137 password = generate(int(config.passwords.password_length))