1 # Copyright (C) 2015 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Test the IUserManager implementation."""
27 from mailman
.interfaces
.address
import ExistingAddressError
28 from mailman
.interfaces
.usermanager
import IUserManager
29 from mailman
.testing
.layers
import ConfigLayer
30 from zope
.component
import getUtility
34 class TestUserManager(unittest
.TestCase
):
38 self
._usermanager
= getUtility(IUserManager
)
40 def test_create_user_with_existing_address(self
):
41 # LP: #1418280. If a user is created when an email address is passed
42 # in, and that address already exists, the user object should not get
44 # Create the address we're going to try to duplicate.
45 self
._usermanager
.create_address('anne@example.com')
47 self
.assertEqual(len(list(self
._usermanager
.users
)), 0)
48 # Now create the user with an already existing address.
49 with self
.assertRaises(ExistingAddressError
) as cm
:
50 self
._usermanager
.create_user('anne@example.com')
51 self
.assertEqual(cm
.exception
.address
, 'anne@example.com')
52 # There are still no users.
53 self
.assertEqual(len(list(self
._usermanager
.users
)), 0)
55 def test_make_new_user(self
):
56 # Neither the user nor address objects exist yet.
57 self
.assertIsNone(self
._usermanager
.get_user('anne@example.com'))
58 self
.assertIsNone(self
._usermanager
.get_address('anne@example.com'))
59 user
= self
._usermanager
.make_user('anne@example.com', 'Anne Person')
60 self
.assertIn('anne@example.com',
61 [address
.email
for address
in user
.addresses
])
62 addresses
= list(user
.addresses
)
63 self
.assertEqual(len(addresses
), 1)
64 address
= addresses
[0]
65 self
.assertEqual(address
.email
, 'anne@example.com')
66 self
.assertEqual(address
.display_name
, 'Anne Person')
67 self
.assertEqual(address
.user
.display_name
, 'Anne Person')
68 self
.assertIs(address
.user
, user
)
70 def test_make_linked_user(self
):
71 # The address exists, but there is no linked user.
72 self
.assertIsNone(self
._usermanager
.get_user('anne@example.com'))
73 address
= self
._usermanager
.create_address('anne@example.com')
74 user
= self
._usermanager
.make_user('anne@example.com', 'Anne Person')
75 self
.assertIsNotNone(address
.user
)
76 self
.assertIs(user
, address
.user
)
77 self
.assertIn(address
, user
.addresses
)
78 self
.assertEqual(user
.display_name
, 'Anne Person')
80 def test_make_user_exists(self
):
81 user
= self
._usermanager
.create_user('anne@example.com', 'Anne Person')
82 other_user
= self
._usermanager
.make_user('anne@example.com')
83 self
.assertIs(user
, other_user
)
85 def test_get_user_by_id(self
):
86 original
= self
._usermanager
.make_user('anne@example.com')
87 copy
= self
._usermanager
.get_user_by_id(original
.user_id
)
88 self
.assertEqual(original
, copy
)