Aurélien Bompard has proposed merging lp:~abompard/mailman/link_on_creation 
into lp:mailman.

Requested reviews:
  Mailman Coders (mailman-coders)

For more details, see:
https://code.launchpad.net/~abompard/mailman/link_on_creation/+merge/225118

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.
-- 
https://code.launchpad.net/~abompard/mailman/link_on_creation/+merge/225118
Your team Mailman Coders is requested to review the proposed merge of 
lp:~abompard/mailman/link_on_creation into lp:mailman.
=== 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 @@
                      })
         self.assertEqual(cm.exception.code, 400)
         self.assertEqual(cm.exception.reason,
-                         'Address already exists: a...@example.com')
+                         'User already exists: a...@example.com')
+
+    def test_existing_address_link(self):
+        # Creating a user with an existing address links them
+        user_manager = getUtility(IUserManager)
+        with transaction():
+            user_manager.create_address('a...@example.com')
+        call_api('http://localhost:9001/3.0/users', {
+                 'email': 'a...@example.com',
+                 })
+        anne = user_manager.get_user('a...@example.com')
+        self.assertNotEqual(anne, None)
+        self.assertTrue('a...@example.com' in
+                        [ a.email for a in anne.addresses ])
 
     def test_addresses_of_missing_user_id(self):
         # Trying to get the /addresses of a missing user id results in error.

=== 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 @@
         # so strip that out (if it exists), then create the user, adding the
         # password after the fact if successful.
         password = arguments.pop('password', None)
+        user_manager = getUtility(IUserManager)
         try:
-            user = getUtility(IUserManager).create_user(**arguments)
+            user = user_manager.create_user(**arguments)
         except ExistingAddressError as error:
-            return http.bad_request(
-                [], b'Address already exists: {0}'.format(error.address))
+            user = user_manager.get_user(arguments["email"])
+            if user is None:
+                # existing address but no user: link it
+                address = user_manager.get_address(arguments["email"])
+                args_no_email = arguments.copy()
+                del args_no_email["email"]
+                user = user_manager.create_user(**args_no_email)
+                user.link(address)
+            else:
+                return http.bad_request(
+                    [], b'User already exists: {0}'.format(error.address))
         if password is None:
             # This will have to be reset since it cannot be retrieved.
             password = generate(int(config.passwords.password_length))

_______________________________________________
Mailman-coders mailing list
Mailman-coders@python.org
https://mail.python.org/mailman/listinfo/mailman-coders

Reply via email to