(no commit message)
[utils] / security / usermgt / src / test / java / org / wamblee / security / authorization / TestUserAccessor.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.security.authorization;
17
18 import org.wamblee.security.authentication.InMemoryGroupSet;
19 import org.wamblee.security.authentication.InMemoryUserSet;
20 import org.wamblee.security.authentication.Md5HexMessageDigester;
21 import org.wamblee.security.authentication.RegexpNameValidator;
22 import org.wamblee.security.authentication.UserAccessor;
23 import org.wamblee.security.authentication.UserAdministration;
24 import org.wamblee.security.authentication.UserAdministrationImpl;
25 import org.wamblee.security.authentication.UserMgtException.Reason;
26
27 /**
28  * User access that always returns a user that belongs to a fixed group.
29  * 
30  * @author Erik Brakkee
31  */
32 public class TestUserAccessor implements UserAccessor {
33     private static final String USER = "erik";
34
35     private static final String PASSWORD = "abc123";
36
37     private static final String GROUP = "users";
38
39     private UserAdministration userAdmin;
40
41     public TestUserAccessor() {
42         userAdmin = new UserAdministrationImpl(new InMemoryUserSet(
43             new RegexpNameValidator(RegexpNameValidator.PASSWORD_PATTERN,
44                 Reason.INVALID_PASSWORD,
45                 "Password must contain at least 6 characters"),
46             new Md5HexMessageDigester()), new InMemoryGroupSet(),
47             new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
48                 Reason.INVALID_USERNAME, "Invalid user"),
49             new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
50                 Reason.INVALID_GROUPNAME, "Invalid group"));
51
52         userAdmin.createGroup(GROUP);
53         userAdmin.createUser(USER, PASSWORD);
54         userAdmin.addUserToGroup(USER, GROUP);
55     }
56
57     // NOTE: This is only for test. Normally the user accessor would not depend
58     // on user administration. This just ensures that a user administration is
59     // created
60     // that knows about the given user.
61     public UserAdministration getUserAdmin() {
62         return userAdmin;
63     }
64
65     /*
66      * (non-Javadoc)
67      * 
68      * @see org.wamblee.usermgt.UserAccessor#getCurrentUser()
69      */
70     public String getCurrentUser() {
71         return USER;
72     }
73 }