summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mailman/docs/NEWS.rst3
-rw-r--r--src/mailman/model/tests/test_usermanager.py50
-rw-r--r--src/mailman/model/usermanager.py3
-rw-r--r--src/mailman/rest/tests/test_users.py22
4 files changed, 77 insertions, 1 deletions
diff --git a/src/mailman/docs/NEWS.rst b/src/mailman/docs/NEWS.rst
index ba8fa61bd..a235a98a3 100644
--- a/src/mailman/docs/NEWS.rst
+++ b/src/mailman/docs/NEWS.rst
@@ -16,6 +16,9 @@ Bugs
----
* Fix calculation of default configuration file to use when the ``$var_dir``
is created by ``mailman start``. (LP: #1411435)
+ * When creating a user with an email address, do not create the user record
+ if the email address already exists. Given by Andrew Stuart.
+ (LP: #1418280)
Configuration
-------------
diff --git a/src/mailman/model/tests/test_usermanager.py b/src/mailman/model/tests/test_usermanager.py
new file mode 100644
index 000000000..90fcdac0c
--- /dev/null
+++ b/src/mailman/model/tests/test_usermanager.py
@@ -0,0 +1,50 @@
+# Copyright (C) 2015 by the Free Software Foundation, Inc.
+#
+# This file is part of GNU Mailman.
+#
+# GNU Mailman is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
+
+"""Test the IUserManager implementation."""
+
+__all__ = [
+ 'TestUserManager',
+ ]
+
+
+import unittest
+
+from mailman.interfaces.address import ExistingAddressError
+from mailman.interfaces.usermanager import IUserManager
+from mailman.testing.layers import ConfigLayer
+from zope.component import getUtility
+
+
+class TestUserManager(unittest.TestCase):
+ layer = ConfigLayer
+
+ def test_create_user_with_existing_address(self):
+ # LP: #1418280. If a user is created when an email address is passed
+ # in, and that address already exists, the user object should not get
+ # created.
+ manager = getUtility(IUserManager)
+ # Create the address we're going to try to duplicate.
+ manager.create_address('anne@example.com')
+ # There are no users.
+ self.assertEqual(len(list(manager.users)), 0)
+ # Now create the user with an already existing address.
+ with self.assertRaises(ExistingAddressError) as cm:
+ manager.create_user('anne@example.com')
+ self.assertEqual(cm.exception.address, 'anne@example.com')
+ # There are still no users.
+ self.assertEqual(len(list(manager.users)), 0)
diff --git a/src/mailman/model/usermanager.py b/src/mailman/model/usermanager.py
index 2ad259693..11557bc25 100644
--- a/src/mailman/model/usermanager.py
+++ b/src/mailman/model/usermanager.py
@@ -39,9 +39,10 @@ class UserManager:
def create_user(self, email=None, display_name=None):
"""See `IUserManager`."""
- user = User(display_name, Preferences())
if email:
address = self.create_address(email, display_name)
+ user = User(display_name, Preferences())
+ if email:
user.link(address)
return user
diff --git a/src/mailman/rest/tests/test_users.py b/src/mailman/rest/tests/test_users.py
index e009f63c1..2c729711f 100644
--- a/src/mailman/rest/tests/test_users.py
+++ b/src/mailman/rest/tests/test_users.py
@@ -188,6 +188,28 @@ class TestUsers(unittest.TestCase):
})
self.assertEqual(cm.exception.code, 404)
+ def test_create_user_twice(self):
+ # LP: #1418280. No additional users should be created when an address
+ # that already exists is given.
+ content, response = call_api('http://localhost:9001/3.0/users')
+ self.assertEqual(content['total_size'], 0)
+ # Create the user.
+ call_api('http://localhost:9001/3.0/users', dict(
+ email='anne@example.com'))
+ # There is now one user.
+ content, response = call_api('http://localhost:9001/3.0/users')
+ self.assertEqual(content['total_size'], 1)
+ # Trying to create the user with the same address results in an error.
+ with self.assertRaises(HTTPError) as cm:
+ call_api('http://localhost:9001/3.0/users', dict(
+ email='anne@example.com'))
+ self.assertEqual(cm.exception.code, 400)
+ self.assertEqual(cm.exception.reason,
+ b'Address already exists: anne@example.com')
+ # But at least no new users was created.
+ content, response = call_api('http://localhost:9001/3.0/users')
+ self.assertEqual(content['total_size'], 1)
+
class TestLP1074374(unittest.TestCase):