2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.usermgt;
21 import junit.framework.TestCase;
23 import org.apache.log4j.Logger;
24 import org.wamblee.security.encryption.Md5HexMessageDigester;
25 import org.wamblee.usermgt.UserMgtException.Reason;
28 * Test of user administration implementation.
30 * @author Erik Brakkee
32 public class UserAdministrationImplTest extends TestCase {
34 private static final Logger LOGGER = Logger
35 .getLogger(UserAdministrationImplTest.class);
37 private static final String USER1 = "piet";
39 private static final String PASS1 = "passpiet";
41 private static final String USER2 = "kees";
43 private static final String PASS2 = "passkees";
45 private static final String GROUP1 = "cyclists";
47 private static final String GROUP2 = "runners";
49 private UserAdministration _admin;
55 * @see junit.framework.TestCase#setUp()
58 protected void setUp() throws Exception {
60 _admin = createAdmin();
63 protected UserAdministration createAdmin() {
64 UserSet users = new InMemoryUserSet( new RegexpNameValidator(RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD, "Password must contain at least 6 characters"),
65 new Md5HexMessageDigester());
66 GroupSet groups = new InMemoryGroupSet();
67 return new UserAdministrationImpl(users, groups,
68 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
69 Reason.INVALID_USERNAME, "Invalid user"),
70 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
71 Reason.INVALID_GROUPNAME, "Invalid group"));
74 protected User createUser(String aName, String aPassword, Group aGroup) throws UserMgtException {
75 return UsermgtTestUtils.createUser(aName, aPassword, aGroup);
79 * Constructs the admin, verify it contains no users and no groups.
81 public void testConstruct() {
82 assertEquals(0, _admin.getUsers().size());
83 assertEquals(0, _admin.getGroups().size());
84 assertEquals(0, _admin.getUserCount());
85 assertEquals(0, _admin.getGroupCount());
89 * Creates a new group. Verifies the group is created correctly and that the
93 public void testCreateGroup() throws UserMgtException {
94 Group group = _admin.createGroup(GROUP1);
96 assertEquals(GROUP1, group.getName());
98 Set<Group> groups = _admin.getGroups();
99 assertEquals(1, groups.size());
100 assertEquals(1, _admin.getGroupCount());
101 assertTrue(groups.contains(group));
104 private void createInvalidGroup(String aUsername) {
106 _admin.createGroup(aUsername);
108 } catch (UserMgtException e) {
109 assertEquals(UserMgtException.Reason.INVALID_GROUPNAME, e
111 assertEquals(0, _admin.getGroupCount());
116 * Creates a new group with an invalid name. Verifies that the appropriate
117 * exception is thrown.
119 * @throws UserMgtException
121 public void testCreateInvalidGroupName() throws UserMgtException {
122 createInvalidGroup("");
123 createInvalidGroup("0abc"); // should not start with digits
124 createInvalidGroup("a b"); // should not contain spaces
125 createInvalidGroup(" aa");
126 createInvalidGroup("aa ");
130 * Creates a new group which conflicts with an existing one. Verifies that
131 * the UserMgtException is thrown and that no group is added.
134 public void testCreateDuplicateGroup() throws UserMgtException {
135 _admin.createGroup(GROUP1);
137 _admin.createGroup(GROUP1);
138 } catch (UserMgtException e) {
139 assertEquals(UserMgtException.Reason.DUPLICATE_GROUP, e.getReason());
140 assertEquals(1, _admin.getGroupCount());
147 * Creates a new user. Verifies the user is created correctly and that the
151 public void testCreateUser() throws UserMgtException {
152 Group group = _admin.createGroup(GROUP1);
153 User user = _admin.createUser(USER1, PASS1, group);
155 assertEquals(USER1, user.getName());
156 user.checkPassword(PASS1);
158 Set<User> users = _admin.getUsers();
159 assertEquals(1, users.size());
160 assertEquals(1, _admin.getUserCount());
161 assertTrue(users.contains(user));
164 private void createInvalidUser(String aUsername, Group aGroup) {
166 _admin.createUser(aUsername, "pass", aGroup);
168 } catch (UserMgtException e) {
169 assertEquals(UserMgtException.Reason.INVALID_USERNAME, e
171 assertEquals(0, _admin.getUserCount());
176 * Constructs users with invalid names. Verifies that the appropriate
177 * exception is thrown.
180 public void testCreateInvalidUserName() throws UserMgtException {
181 Group group = _admin.createGroup(GROUP1);
182 createInvalidUser("", group);
183 createInvalidUser("0abc", group); // should not start with digits
184 createInvalidUser("a b", group); // should not contain spaces
185 createInvalidUser(" aa", group);
186 createInvalidUser("aa ", group);
190 * Creates a new user which conflicts with an existing one. Verifies that
191 * the UserMgtException is thrown and that no user is added.
194 public void testCreateDuplicateUser() throws UserMgtException {
195 Group group = _admin.createGroup(GROUP1);
196 _admin.createUser(USER1, PASS1, group);
198 _admin.createUser(USER1, PASS2, group);
200 } catch (UserMgtException e) {
201 assertEquals(UserMgtException.Reason.DUPLICATE_USER, e.getReason());
202 assertEquals(1, _admin.getUserCount());
207 * Gets a known user by name. Verifies the correct user is obtained.
208 * Verifies that null is returned when trying to obtain an unknown user.
211 public void testGetUser() throws UserMgtException {
212 Group group = _admin.createGroup(GROUP1);
213 User user = _admin.createUser(USER1, PASS1, group);
214 User user2 = _admin.getUser(USER1);
215 assertTrue(user.equals(user2));
216 assertNull(_admin.getUser(USER2));
220 * Gets a known group by name. Verifies the correct group is obtained.
221 * Verifies that null is returned when the group is not known.
224 public void testGetGroup() throws UserMgtException {
225 Group group = _admin.createGroup(GROUP1);
226 Group group2 = _admin.getGroup(GROUP1);
227 assertTrue(group.equals(group2));
228 assertNull(_admin.getGroup(GROUP2));
232 * Adds a user to a group. Verifies that the user is added using several API
233 * calls. Verifies that an exception occurs if the user is not already part
237 public void testAddUserToGroup() throws UserMgtException {
239 Group group = _admin.createGroup(GROUP1);
240 User user = _admin.createUser(USER1, PASS1, group);
241 Group group2 = _admin.createGroup(GROUP2);
242 assertTrue(user.isInGroup(group));
243 assertFalse(user.isInGroup(group2));
244 _admin.addUserToGroup(user, group2);
245 assertTrue(user.isInGroup(group));
246 assertTrue(user.isInGroup(group2));
247 Set<User> users = _admin.getUsers(group2);
248 assertNotNull(users);
249 assertEquals(1, users.size());
250 assertTrue(users.contains(user));
253 _admin.addUserToGroup(user, group);
254 } catch (UserMgtException e) {
255 assertEquals(UserMgtException.Reason.USER_ALREADY_IN_GROUP, e
263 * Adds a user to a group where the user does not exist. Verifies that an
267 public void testAddUserToGroupUnknownUser() throws UserMgtException {
268 Group group = _admin.createGroup(GROUP1);
269 User user = createUser(USER1, PASS1, group);
271 _admin.addUserToGroup(user, group);
272 } catch (UserMgtException e) {
273 assertEquals(UserMgtException.Reason.UNKNOWN_USER, e.getReason());
280 * Adds a user to a group where the user does not exist. Verifies that an
284 public void testAddUserToGroupUnknownGroup() throws UserMgtException {
285 Group group = _admin.createGroup(GROUP1);
286 User user = _admin.createUser(USER1, PASS1, group);
287 Group group2 = new Group(GROUP2);
289 _admin.addUserToGroup(user, group2);
290 } catch (UserMgtException e) {
291 assertEquals(UserMgtException.Reason.UNKNOWN_GROUP, e.getReason());
298 * Removes a user from a group. Verifies that the user is removed from the
299 * group using several API calls. Verifies that an exception occurs if the
300 * user not part of the group or if the user is only part of one group.
302 public void testRemoveUserFromGroup() throws UserMgtException {
303 Group group = _admin.createGroup(GROUP1);
305 User user = _admin.createUser(USER1, PASS1, group);
306 Group group2 = _admin.createGroup(GROUP2);
307 _admin.addUserToGroup(user, group2);
308 Set<Group> groups = user.getGroups();
309 assertEquals(2, groups.size());
310 assertTrue(groups.contains(group));
311 assertTrue(groups.contains(group2));
313 _admin.removeUserFromGroup(user, group);
314 groups = user.getGroups();
315 assertEquals(1, groups.size());
316 assertTrue(groups.contains(group2));
317 assertFalse(groups.contains(group));
321 * Removes a user from a group where the user is not known. Verifies that an
322 * exception is thrown.
325 public void testRemoveUserFromGroupUnknownUser() throws UserMgtException {
326 Group group = _admin.createGroup(GROUP1);
327 User user = createUser(USER1, GROUP1, group);
329 _admin.removeUserFromGroup(user, group);
330 } catch (UserMgtException e) {
331 assertEquals(UserMgtException.Reason.UNKNOWN_USER, e.getReason());
336 * Removes a user from a group where the group is not known. Verifies that
337 * an exception is thrown.
340 public void testRemoveUserFromGroupUnknownGroup() throws UserMgtException {
341 Group group = _admin.createGroup(GROUP1);
342 User user = _admin.createUser(USER1, PASS1, group);
343 Group group2 = new Group(GROUP2);
345 _admin.removeUserFromGroup(user, group2);
346 } catch (UserMgtException e) {
347 assertEquals(UserMgtException.Reason.UNKNOWN_GROUP, e.getReason());
352 * Removes a user from a group where the user is only part of one group.
353 * Verifies that an exception is thrown.
355 public void testRemoveUserFromGroupOnlyGroup() throws UserMgtException {
356 Group group = _admin.createGroup(GROUP1);
357 User user = _admin.createUser(USER1, PASS1, group);
359 _admin.removeUserFromGroup(user, group);
360 } catch (UserMgtException e) {
361 assertEquals(UserMgtException.Reason.USER_MUST_BE_IN_A_GROUP, e
367 * Gets the list of users and groups. Verifies that the correct suers and
368 * groups are returned. Verifies also that the relations from user to group
372 public void testGetUsersAndGroups() throws UserMgtException {
373 Group group1 = _admin.createGroup(GROUP1);
374 Group group2 = _admin.createGroup(GROUP2);
376 User user1 = _admin.createUser(USER1, PASS1, group1);
377 _admin.addUserToGroup(user1, group2);
378 User user2 = _admin.createUser(USER2, PASS2, group2);
380 Set<User> users = _admin.getUsers();
381 assertEquals(2, users.size());
382 assertTrue(users.contains(user1));
383 assertTrue(users.contains(user2));
385 Set<Group> groups = _admin.getGroups();
386 assertEquals(2, groups.size());
387 assertTrue(groups.contains(group1));
388 assertTrue(groups.contains(group2));
390 assertTrue(user1.isInGroup(group1));
391 assertTrue(user1.isInGroup(group2));
392 assertFalse(user2.isInGroup(group1));
393 assertTrue(user2.isInGroup(group2));
395 Set<Group> groups1 = user1.getGroups();
396 assertEquals(2, groups1.size());
398 Set<Group> groups2 = user2.getGroups();
399 assertEquals(1, groups2.size());
403 * Renames a user. Verifies that the user is renamed. Verifies that
404 * exceptions are thrown when an attempt is made to rename the user to
405 * itself or to another existing user, or when the group does not exist.
408 public void testRenameUser() throws UserMgtException {
409 Group group = _admin.createGroup(GROUP1);
410 User user1 = _admin.createUser(USER1, PASS1, group);
411 _admin.renameUser(user1, USER2);
412 assertEquals(USER2, user1.getName());
413 assertEquals(user1, _admin.getUser(USER2));
415 _admin.createUser(USER1, PASS1, group);
418 _admin.renameUser(user1, USER1);
419 } catch (UserMgtException e) {
420 assertEquals(UserMgtException.Reason.DUPLICATE_USER, e.getReason());
422 // do a trivial reanem
424 _admin.renameUser(user1, user1.getName());
425 } catch (UserMgtException e2) {
426 assertEquals(UserMgtException.Reason.TRIVIAL_RENAME, e2
436 * Renames a user to a user with an invalid username. Verifies that the
437 * appropriate exception is thrown.
440 public void testRenameUserInvalidUsername() throws UserMgtException {
441 Group group = _admin.createGroup(GROUP1);
442 User user1 = _admin.createUser(USER1, PASS1, group);
444 _admin.renameUser(user1, USER2);
445 } catch (UserMgtException e) {
446 assertEquals(e.getReason(), Reason.INVALID_USERNAME);
451 * Renames a group. Verifies that the group is renamed. Verifies that
452 * exceptions are thrown when an attempt is made to rename the group to
453 * itself or to another existing group or when the group does not exist.
456 public void testRenameGroup() throws UserMgtException {
457 Group group = _admin.createGroup(GROUP1);
458 _admin.renameGroup(group, GROUP2);
459 assertEquals(GROUP2, group.getName());
460 assertEquals(group, _admin.getGroup(GROUP2));
462 _admin.createGroup(GROUP1);
464 _admin.renameGroup(group, GROUP1);
465 } catch (UserMgtException e) {
466 assertEquals(UserMgtException.Reason.DUPLICATE_GROUP, e.getReason());
468 // do a trivial reanem
470 _admin.renameGroup(group, group.getName());
471 } catch (UserMgtException e2) {
472 assertEquals(UserMgtException.Reason.TRIVIAL_RENAME, e2
483 * Renames a group to a group with an invalid name. Verifies that the
484 * appropriate exception is thrown.
487 public void testRenameGroupInvalidGroupname() throws UserMgtException {
488 Group group = _admin.createGroup(GROUP1);
490 _admin.renameGroup(group, "a b");
491 } catch (UserMgtException e) {
492 assertEquals(e.getReason(), Reason.INVALID_GROUPNAME);
497 * Removes a user. Verifies that the user is removed. Verifies that the an
498 * exception is thrown when the user does not exist.
501 public void testRemoveUser() throws UserMgtException {
502 Group group = _admin.createGroup(GROUP1);
503 User user = _admin.createUser(USER1, PASS1, group);
505 assertEquals(1, _admin.getUserCount());
506 _admin.removeUser(user);
507 assertEquals(0, _admin.getUserCount());
509 _admin.createUser(USER1, PASS1, group);
510 assertEquals(1, _admin.getUserCount());
512 User user2 = createUser(USER2, PASS2, group);
515 _admin.removeUser(user2);
516 } catch (UserMgtException e) {
517 assertEquals(UserMgtException.Reason.UNKNOWN_USER, e.getReason());
522 * Removes a group. Verifies that the group is removed. Verifies that the an
523 * exception is thrown when the group does not exist or if there are still
524 * users in the group.
527 public void testRemoveGroup() throws UserMgtException {
528 Group group1 = _admin.createGroup(GROUP1);
529 assertEquals(1, _admin.getGroupCount());
530 _admin.removeGroup(group1);
531 assertEquals(0, _admin.getGroupCount());
532 group1 = _admin.createGroup(GROUP1);
534 _admin.createUser(USER1, PASS1, group1);
536 _admin.removeGroup(group1);
537 } catch (UserMgtException e) {
538 assertEquals(UserMgtException.Reason.GROUP_STILL_OCCUPIED, e
546 * Tries to remove an unknown group. Verifies that an exception is thrown.
549 public void testRemoveGroupUnknownGroup() throws UserMgtException {
550 Group group = _admin.createGroup(GROUP1);
551 Group group2 = new Group(GROUP2);
553 _admin.removeGroup(group2);
554 } catch (UserMgtException e) {
555 assertEquals(UserMgtException.Reason.UNKNOWN_GROUP, e.getReason());
560 * Changes the password, verifies that this succeeds.
562 * @throws UserMgtException
564 public void testChangePassword() throws UserMgtException {
565 Group group = _admin.createGroup(GROUP1);
566 User user = _admin.createUser(USER1, PASS1, group);
567 user.changePassword(PASS1, PASS2);
569 // retrieve the user and verifies the password hasn't changed.
570 User user2 = _admin.getUser(USER1);
572 user2.checkPassword(PASS2);
573 fail(); // password should not have changed already.
574 } catch (UserMgtException e) {
578 // now notify the admin of the change in the user
579 _admin.userModified(user);
581 user2 = _admin.getUser(USER1);
582 user2.checkPassword(PASS2); // this time it should succeed.
587 * Performance test. Finds a user by name.
590 public void testPerformanceFindUserByName() throws UserMgtException {
591 Group group = _admin.createGroup(GROUP1);
592 _admin.createUser(USER1, PASS1, group);
595 long time = System.currentTimeMillis();
596 for (int i = 0; i < n; i++) {
597 _admin.getUser(USER1);
599 LOGGER.info("Looked up a user " + n + " times in "
600 + (float) (System.currentTimeMillis() - time) / 1000.0);