diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/mailman/model/docs/usermanager.rst | 22 | ||||
| -rw-r--r-- | src/mailman/model/tests/test_usermanager.py | 42 |
2 files changed, 59 insertions, 5 deletions
diff --git a/src/mailman/model/docs/usermanager.rst b/src/mailman/model/docs/usermanager.rst index ba328b54b..8e40b621e 100644 --- a/src/mailman/model/docs/usermanager.rst +++ b/src/mailman/model/docs/usermanager.rst @@ -179,3 +179,25 @@ There are now four members in the system. Sort them by address then role. test.example.com bperson@example.com MemberRole.owner test.example.com eperson@example.com MemberRole.member test.example.com fperson@example.com MemberRole.member + + +Creating a new user +=================== + +A common situation (especially during the subscription life cycle) is to +create a user linked to an address, with a preferred address. Say for +example, we are asked to subscribe a new address we have never seen before. + + >>> cris = user_manager.make_user('cris@example.com', 'Cris Person') + +Since we've never seen ``cris@example.com`` before, this call creates a new +user with the given email and display name. + + >>> cris + <User "Cris Person" (5) at ...> + +The user has a single unverified address object. + + >>> for address in cris.addresses: + ... print(repr(address)) + <Address: Cris Person <cris@example.com> [not verified] at ...> diff --git a/src/mailman/model/tests/test_usermanager.py b/src/mailman/model/tests/test_usermanager.py index 90fcdac0c..31f1a7275 100644 --- a/src/mailman/model/tests/test_usermanager.py +++ b/src/mailman/model/tests/test_usermanager.py @@ -33,18 +33,50 @@ from zope.component import getUtility class TestUserManager(unittest.TestCase): layer = ConfigLayer + def setUp(self): + self._usermanager = getUtility(IUserManager) + 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') + self._usermanager.create_address('anne@example.com') # There are no users. - self.assertEqual(len(list(manager.users)), 0) + self.assertEqual(len(list(self._usermanager.users)), 0) # Now create the user with an already existing address. with self.assertRaises(ExistingAddressError) as cm: - manager.create_user('anne@example.com') + self._usermanager.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) + self.assertEqual(len(list(self._usermanager.users)), 0) + + def test_make_new_user(self): + # Neither the user nor address objects exist yet. + self.assertIsNone(self._usermanager.get_user('anne@example.com')) + self.assertIsNone(self._usermanager.get_address('anne@example.com')) + user = self._usermanager.make_user('anne@example.com', 'Anne Person') + self.assertIn('anne@example.com', + [address.email for address in user.addresses]) + addresses = list(user.addresses) + self.assertEqual(len(addresses), 1) + address = addresses[0] + self.assertEqual(address.email, 'anne@example.com') + self.assertEqual(address.display_name, 'Anne Person') + self.assertEqual(address.user.display_name, 'Anne Person') + self.assertIs(address.user, user) + + def test_make_linked_user(self): + # The address exists, but there is no linked user. + self.assertIsNone(self._usermanager.get_user('anne@example.com')) + address = self._usermanager.create_address('anne@example.com') + user = self._usermanager.make_user('anne@example.com', 'Anne Person') + self.assertIsNotNone(address.user) + self.assertIs(user, address.user) + self.assertIn(address, user.addresses) + self.assertEqual(user.display_name, 'Anne Person') + + def test_make_user_exists(self): + user = self._usermanager.create_user('anne@example.com', 'Anne Person') + other_user = self._usermanager.make_user('anne@example.com') + self.assertIs(user, other_user) |
