Barry Warsaw pushed to branch master at mailman / Mailman

Commits:
c76660e4 by Abhilash Raj at 2015-07-30T15:25:22Z
Add an unlinked address to a user via REST

When adding an existing address through REST API, a 400 error was
raised even if the address was not linked to any user. Fix that.

- - - - -
d42dd4a7 by Barry Warsaw at 2015-07-31T20:29:19Z
Merge branch 'maxking/mailman-add-alternate' into mr30

By POSTing to a user resource with an existing unlinked address, you can link
the address to the user.  Given by Abhilash Raj.

- - - - -


3 changed files:

- src/mailman/docs/NEWS.rst
- src/mailman/rest/addresses.py
- src/mailman/rest/tests/test_addresses.py


Changes:

=====================================
src/mailman/docs/NEWS.rst
=====================================
--- a/src/mailman/docs/NEWS.rst
+++ b/src/mailman/docs/NEWS.rst
@@ -57,6 +57,8 @@ REST
    Bompard.
  * The REST API incorrectly parsed `is_server_owner` values when given
    explicitly in the POST that creates a user.  (Closes #136)
+ * By POSTing to a user resource with an existing unlinked address, you can
+   link the address to the user.  Given by Abhilash Raj.
 
 Other
 -----


=====================================
src/mailman/rest/addresses.py
=====================================
--- a/src/mailman/rest/addresses.py
+++ b/src/mailman/rest/addresses.py
@@ -211,11 +211,20 @@ class UserAddresses(_AddressBase):
         except InvalidEmailAddressError:
             bad_request(response, b'Invalid email address')
         except ExistingAddressError:
-            bad_request(response, b'Address already exists')
+            # Check if the address is not linked to any user, link it to the
+            # current user and return it.  Since we're linking to an existing
+            # address, ignore any given display_name attribute.
+            address = user_manager.get_address(validator(request)['email'])
+            if address.user is None:
+                address.user = self._user
+                location = self.path_to('addresses/{}'.format(address.email))
+                created(response, location)
+            else:
+                bad_request(response, 'Address belongs to other user.')
         else:
             # Link the address to the current user and return it.
             address.user = self._user
-            location = self.path_to('addresses/{0}'.format(address.email))
+            location = self.path_to('addresses/{}'.format(address.email))
             created(response, location)
 
 


=====================================
src/mailman/rest/tests/test_addresses.py
=====================================
--- a/src/mailman/rest/tests/test_addresses.py
+++ b/src/mailman/rest/tests/test_addresses.py
@@ -168,7 +168,48 @@ class TestAddresses(unittest.TestCase):
                      'email': 'a...@example.com',
                      })
         self.assertEqual(cm.exception.code, 400)
-        self.assertEqual(cm.exception.reason, b'Address already exists')
+        self.assertEqual(cm.exception.reason,
+                         b'Address belongs to other user.')
+
+    def test_add_unlinked_address_to_user(self):
+        user_manager = getUtility(IUserManager)
+        with transaction():
+            anne = user_manager.create_user('anne.per...@example.com')
+            user_manager.create_address('a...@example.com')
+        response, content = call_api(
+         'http://localhost:9001/3.0/users/anne.per...@example.com/addresses', {
+            'email': 'a...@example.com',
+            })
+        self.assertIn('a...@example.com',
+                      [address.email for address in anne.addresses])
+        self.assertEqual(content['status'], '201')
+        self.assertEqual(
+            content['location'],
+            'http://localhost:9001/3.0/addresses/a...@example.com')
+        # The address has no display name.
+        anne_person = user_manager.get_address('a...@example.com')
+        self.assertEqual(anne_person.display_name, '')
+
+    def test_add_unlinked_address_to_user_with_ignored_display_name(self):
+        user_manager = getUtility(IUserManager)
+        with transaction():
+            anne = user_manager.create_user('anne.per...@example.com')
+            user_manager.create_address('a...@example.com')
+        response, content = call_api(
+         'http://localhost:9001/3.0/users/anne.per...@example.com/addresses', {
+            'email': 'a...@example.com',
+            'display_name': 'Anne Person',
+            })
+        self.assertIn('a...@example.com',
+                      [address.email for address in anne.addresses])
+        self.assertEqual(content['status'], '201')
+        self.assertEqual(
+            content['location'],
+            'http://localhost:9001/3.0/addresses/a...@example.com')
+        # Even though a display_name was given in the POST data, because the
+        # address already existed, it still has no display name.
+        anne_person = user_manager.get_address('a...@example.com')
+        self.assertEqual(anne_person.display_name, '')
 
     def test_invalid_address_bad_request(self):
         # Trying to add an invalid address string returns 400.



View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/39e55cff34f2556f5fca7c50fb05196fbec03aa6...d42dd4a73228eab1309e517695866ac7debbba23
_______________________________________________
Mailman-checkins mailing list
Mailman-checkins@python.org
Unsubscribe: 
https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to