summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/mailman/rest/addresses.py26
-rw-r--r--src/mailman/rest/tests/test_addresses.py17
2 files changed, 20 insertions, 23 deletions
diff --git a/src/mailman/rest/addresses.py b/src/mailman/rest/addresses.py
index 8fc6871b5..f0cb8edef 100644
--- a/src/mailman/rest/addresses.py
+++ b/src/mailman/rest/addresses.py
@@ -192,23 +192,23 @@ class UserAddresses(_AddressBase):
user_manager = getUtility(IUserManager)
validator = Validator(email=str,
display_name=str,
- force_existing=bool,
- _optional=('display_name', 'force_existing'))
+ absorb_existing=bool,
+ _optional=('display_name', 'absorb_existing'))
try:
data = validator(request)
except ValueError as error:
bad_request(response, str(error))
return
- force_existing = data.pop('force_existing', False)
+ absorb_existing = data.pop('absorb_existing', False)
try:
address = user_manager.create_address(**data)
except InvalidEmailAddressError:
bad_request(response, b'Invalid email address')
return
except ExistingAddressError:
- # 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.
+ # 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(data['email'])
if address.user is None:
address.user = self._user
@@ -216,15 +216,13 @@ class UserAddresses(_AddressBase):
'addresses/{}'.format(address.email))
created(response, location)
return
+ elif not absorb_existing:
+ bad_request(response, 'Address belongs to other user')
+ return
else:
- if not force_existing:
- bad_request(response, 'Address belongs to other user.')
- return
- else:
- # The address exists and is linked but we can merge the
- # users.
- address = user_manager.get_address(data['email'])
- self._user.absorb(address.user)
+ # The address exists and is linked but we can merge the users.
+ address = user_manager.get_address(data['email'])
+ self._user.absorb(address.user)
else:
# Link the address to the current user and return it.
address.user = self._user
diff --git a/src/mailman/rest/tests/test_addresses.py b/src/mailman/rest/tests/test_addresses.py
index 85bd117ef..c20ab1993 100644
--- a/src/mailman/rest/tests/test_addresses.py
+++ b/src/mailman/rest/tests/test_addresses.py
@@ -169,8 +169,7 @@ class TestAddresses(unittest.TestCase):
'email': 'anne@example.com',
})
self.assertEqual(cm.exception.code, 400)
- self.assertEqual(cm.exception.reason,
- b'Address belongs to other user.')
+ self.assertEqual(cm.exception.reason, b'Address belongs to other user')
def test_add_unlinked_address_to_user(self):
user_manager = getUtility(IUserManager)
@@ -214,22 +213,22 @@ class TestAddresses(unittest.TestCase):
def test_existing_address_absorb(self):
# Trying to add an existing address causes a merge if the
- # 'force_existing' flag is present.
+ # 'absorb_existing' flag is present.
user_manager = getUtility(IUserManager)
with transaction():
anne = user_manager.create_user('anne@example.com')
- user_manager.create_user('bill@example.com')
+ user_manager.create_user('bart@example.com')
response, content = call_api(
'http://localhost:9001/3.0/users/anne@example.com/addresses', {
- 'email': 'bill@example.com',
- 'force_existing': '1',
+ 'email': 'bart@example.com',
+ 'absorb_existing': True,
})
- self.assertIn('bill@example.com',
- [addr.email for addr in anne.addresses])
+ self.assertIn('bart@example.com',
+ [address.email for address in anne.addresses])
self.assertEqual(content['status'], '201')
self.assertEqual(
content['location'],
- 'http://localhost:9001/3.0/addresses/bill@example.com')
+ 'http://localhost:9001/3.0/addresses/bart@example.com')
def test_invalid_address_bad_request(self):
# Trying to add an invalid address string returns 400.