001: package org.apache.turbine.services.security;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.List;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.turbine.om.security.User;
028: import org.apache.turbine.test.BaseTurbineHsqlTest;
029: import org.apache.turbine.util.security.DataBackendException;
030: import org.apache.turbine.util.security.EntityExistsException;
031: import org.apache.turbine.util.security.PasswordMismatchException;
032: import org.apache.turbine.util.security.UnknownEntityException;
033:
034: public class TestSecurityUserManager extends BaseTurbineHsqlTest {
035: public TestSecurityUserManager(String name) throws Exception {
036: super (name, "conf/test/TurbineResources.properties");
037: }
038:
039: public static Test suite() {
040: return new TestSuite(TestSecurityUserManager.class);
041: }
042:
043: private void checkUserList() throws Exception {
044: SecurityService ss = TurbineSecurity.getService();
045: UserManager um = ss.getUserManager();
046: assertEquals("User added to storage!", um.retrieveList(
047: new org.apache.torque.util.Criteria()).size(), 2);
048: }
049:
050: public void testUserManager1() throws Exception {
051: SecurityService ss = TurbineSecurity.getService();
052: UserManager um = ss.getUserManager();
053:
054: assertTrue(um.accountExists("admin"));
055: assertFalse(um.accountExists("does-not-exist"));
056:
057: User admin = um.retrieve("admin");
058: assertTrue(um.accountExists(admin));
059:
060: User doesNotExist = TurbineSecurity.getUserInstance();
061: assertFalse(um.accountExists(doesNotExist));
062:
063: checkUserList();
064: }
065:
066: public void testUserManager2() throws Exception {
067: SecurityService ss = TurbineSecurity.getService();
068: UserManager um = ss.getUserManager();
069:
070: User admin = um.retrieve("admin");
071:
072: try {
073: User doesNotExist = um.retrieve("does-not-exist");
074: fail("Non existing Account was retrieved");
075: } catch (Exception e) {
076: assertEquals("Wrong Exception thrown: "
077: + e.getClass().getName(), e.getClass(),
078: UnknownEntityException.class);
079: }
080:
081: checkUserList();
082: }
083:
084: public void testUserManager3() throws Exception {
085: SecurityService ss = TurbineSecurity.getService();
086: UserManager um = ss.getUserManager();
087:
088: User admin = um.retrieve("admin", "admin");
089:
090: try {
091: admin = um.retrieve("admin", "no such password");
092: fail("User was authenticated with wrong password");
093: } catch (Exception e) {
094: assertEquals("Wrong Exception thrown: "
095: + e.getClass().getName(), e.getClass(),
096: PasswordMismatchException.class);
097: }
098:
099: checkUserList();
100: }
101:
102: public void testUserManager4() throws Exception {
103: SecurityService ss = TurbineSecurity.getService();
104: UserManager um = ss.getUserManager();
105:
106: User admin = um.retrieve("admin");
107: um.store(admin);
108:
109: try {
110: User newbie = TurbineSecurity.getUserInstance();
111: newbie.setName("newbie");
112:
113: um.store(newbie);
114: fail("Non Existing User could be stored!");
115: } catch (Exception e) {
116: assertEquals("Wrong Exception thrown: "
117: + e.getClass().getName(), e.getClass(),
118: UnknownEntityException.class);
119: }
120:
121: checkUserList();
122: }
123:
124: public void testUserManager5() throws Exception {
125: SecurityService ss = TurbineSecurity.getService();
126: UserManager um = ss.getUserManager();
127:
128: User admin = um.retrieve("admin");
129:
130: um.authenticate(admin, "admin");
131:
132: try {
133: User newbie = TurbineSecurity.getUserInstance();
134: newbie.setName("newbie");
135:
136: um.authenticate(newbie, "somePw");
137: fail("User was authenticated with wrong password");
138: } catch (Exception e) {
139: assertEquals("Wrong Exception thrown: "
140: + e.getClass().getName(), e.getClass(),
141: UnknownEntityException.class);
142: }
143:
144: checkUserList();
145: }
146:
147: public void testUserManager6() throws Exception {
148: SecurityService ss = TurbineSecurity.getService();
149: UserManager um = ss.getUserManager();
150:
151: User[] users = um
152: .retrieve(new org.apache.torque.util.Criteria());
153: assertNotNull(users);
154: assertEquals("Wrong number of users retrieved!", users.length,
155: 2);
156:
157: List userList = um
158: .retrieveList(new org.apache.torque.util.Criteria());
159: assertNotNull(userList);
160: assertEquals("Wrong number of userList retrieved!", userList
161: .size(), 2);
162:
163: assertEquals("Array and List have different sizes!",
164: users.length, userList.size());
165:
166: checkUserList();
167: }
168:
169: public void testUserManager7() throws Exception {
170: SecurityService ss = TurbineSecurity.getService();
171: UserManager um = ss.getUserManager();
172:
173: User admin = um.retrieveById(new Integer(1));
174:
175: try {
176: User doesNotExist = um.retrieveById(new Integer(667));
177: fail("Non existing Account was retrieved");
178: } catch (Exception e) {
179: assertEquals("Wrong Exception thrown: "
180: + e.getClass().getName(), e.getClass(),
181: UnknownEntityException.class);
182: }
183:
184: checkUserList();
185: }
186:
187: public void testAddUser() throws Exception {
188: SecurityService ss = TurbineSecurity.getService();
189: UserManager um = ss.getUserManager();
190:
191: User newbie = TurbineSecurity.getUserInstance();
192: newbie.setName("newbie");
193:
194: newbie.setFirstName("John");
195: newbie.setLastName("Doe");
196:
197: um.createAccount(newbie, "newbie");
198:
199: List users = um
200: .retrieveList(new org.apache.torque.util.Criteria());
201: assertEquals("User was not added", users.size(), 3);
202:
203: try {
204: User admin = um.retrieve("admin");
205:
206: um.createAccount(admin, "admin");
207: fail("Existing User could be added!");
208: } catch (Exception e) {
209: assertEquals("Wrong Exception thrown: "
210: + e.getClass().getName(), e.getClass(),
211: EntityExistsException.class);
212: }
213:
214: try {
215: User empty = TurbineSecurity.getUserInstance();
216:
217: um.createAccount(empty, "empty");
218: fail("User with empty Username could be added!");
219: } catch (Exception e) {
220: assertEquals("Wrong Exception thrown: "
221: + e.getClass().getName(), e.getClass(),
222: DataBackendException.class);
223: }
224:
225: assertEquals("User was not added", users.size(), 3);
226: }
227:
228: public void testRemoveUser() throws Exception {
229: SecurityService ss = TurbineSecurity.getService();
230: UserManager um = ss.getUserManager();
231:
232: User newbie = um.retrieve("newbie");
233: assertNotNull(newbie);
234:
235: um.removeAccount(newbie);
236:
237: try {
238: User foo = TurbineSecurity.getUserInstance();
239: foo.setName("foo");
240:
241: um.removeAccount(foo);
242: fail("Non Existing User could be deleted!");
243: } catch (Exception e) {
244: assertEquals("Wrong Exception thrown: "
245: + e.getClass().getName(), e.getClass(),
246: UnknownEntityException.class);
247: }
248:
249: checkUserList();
250: }
251:
252: public void testChangePassword() throws Exception {
253: SecurityService ss = TurbineSecurity.getService();
254: UserManager um = ss.getUserManager();
255:
256: User admin = um.retrieve("admin");
257: assertNotNull(admin);
258:
259: um.changePassword(admin, admin.getPassword(), "foobar");
260:
261: User admin2 = um.retrieve("admin");
262: assertEquals("Password was not changed!", "foobar", admin2
263: .getPassword());
264:
265: try {
266: admin = um.retrieve("admin");
267: um.changePassword(admin, "admin", "foobar");
268: fail("Password could be changed without old password!");
269: } catch (Exception e) {
270: assertEquals("Wrong Exception thrown: "
271: + e.getClass().getName(), e.getClass(),
272: PasswordMismatchException.class);
273: }
274:
275: admin2 = um.retrieve("admin");
276: assertEquals("Password was changed!", "foobar", admin2
277: .getPassword());
278:
279: checkUserList();
280: }
281:
282: public void testForcePassword() throws Exception {
283: SecurityService ss = TurbineSecurity.getService();
284: UserManager um = ss.getUserManager();
285:
286: User admin = um.retrieve("admin");
287: assertNotNull(admin);
288:
289: um.forcePassword(admin, "barbaz");
290:
291: User admin2 = um.retrieve("admin");
292: assertEquals("Password was not changed!", "barbaz", admin2
293: .getPassword());
294:
295: um.forcePassword(admin, "admin");
296:
297: admin2 = um.retrieve("admin");
298: assertEquals("Password was not reset!", "admin", admin2
299: .getPassword());
300:
301: checkUserList();
302: }
303: }
|