Barry Warsaw pushed to branch release-3.0 at mailman / Mailman
Commits: 6cfe2ecc by Barry Warsaw at 2015-07-31T21:22:05Z 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 @@ -28,6 +28,8 @@ Bugs ``list_url`` or permalink. Given by Aurélien 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. 3.0.0 -- "Show Don't Tell" ===================================== 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 = 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 - created(response, path_to('addresses/{0}'.format(address.email))) + created(response, path_to('addresses/{}'.format(address.email))) ===================================== 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/commit/6cfe2eccc5e4655df51c776c02887b72d0ff37c3
_______________________________________________ Mailman-checkins mailing list Mailman-checkins@python.org Unsubscribe: https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org