2 * Copyright 2005-2010 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.
16 package org.wamblee.security.authorization;
18 import junit.framework.TestCase;
20 import org.wamblee.security.authentication.Group;
21 import org.wamblee.security.authentication.InMemoryGroupSet;
22 import org.wamblee.security.authentication.InMemoryUserSet;
23 import org.wamblee.security.authentication.RegexpNameValidator;
24 import org.wamblee.security.authentication.User;
25 import org.wamblee.security.authentication.UserAccessor;
26 import org.wamblee.security.authentication.UserAdministration;
27 import org.wamblee.security.authentication.UserAdministrationImpl;
28 import org.wamblee.security.authentication.UserMgtException;
29 import org.wamblee.security.authentication.UserMgtException.Reason;
30 import org.wamblee.security.encryption.Md5HexMessageDigester;
34 * User access that always returns a user that belongs to a fixed group.
36 * @author Erik Brakkee
38 public class TestUserAccessor implements UserAccessor {
39 private static final String USER = "erik";
41 private static final String PASSWORD = "abc123";
43 private static final String GROUP = "users";
45 private UserAdministration userAdmin;
47 public TestUserAccessor() {
48 userAdmin = new UserAdministrationImpl(
49 new InMemoryUserSet(new RegexpNameValidator(
50 RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD,
51 "Password must contain at least 6 characters"),
52 new Md5HexMessageDigester()), new InMemoryGroupSet(),
53 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
54 Reason.INVALID_USERNAME, "Invalid user"),
55 new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
56 Reason.INVALID_GROUPNAME, "Invalid group"));
58 userAdmin.createGroup(GROUP);
59 userAdmin.createUser(USER, PASSWORD);
60 userAdmin.addUserToGroup(USER, GROUP);
63 // NOTE: This is only for test. Normally the user accessor would not depend
64 // on user administration. This just ensures that a user administration is created
65 // that knows about the given user.
66 public UserAdministration getUserAdmin() {
73 * @see org.wamblee.usermgt.UserAccessor#getCurrentUser()
75 public String getCurrentUser() {