001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.reg;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.aikp.*;
030: import com.methodhead.test.*;
031: import com.methodhead.sitecontext.*;
032: import com.methodhead.*;
033: import com.methodhead.transfer.*;
034:
035: public class UserTest extends TestCase {
036:
037: private User user = null;
038: private Contact contact = null;
039: private ResultSet rs = null;
040: private List list = null;
041: private SiteContext siteContext = null;
042: private Role role = null;
043:
044: static {
045: TestUtils.initLogger();
046: TestUtils.initDb();
047: }
048:
049: public UserTest(String name) {
050: super (name);
051: }
052:
053: protected void setUp() {
054: try {
055: ConnectionSingleton.runBatchUpdate(new FileReader(
056: "webapp/WEB-INF/db/transfer-reset.sql"));
057: } catch (Exception e) {
058: fail(e.getMessage());
059: }
060: }
061:
062: protected void tearDown() {
063: }
064:
065: public void testProperties() {
066: try {
067: user = new User();
068:
069: assertNotNull(user.getRoles());
070: assertEquals(0, user.getRoles().size());
071:
072: assertNotNull(user.getContact());
073: } catch (Exception e) {
074: e.printStackTrace();
075: fail();
076: }
077: }
078:
079: public void testCompareTo() {
080: TestData.createUsers();
081:
082: assertEquals(-1, TestData.user1.compareTo(TestData.user2));
083: assertEquals(0, TestData.user1.compareTo(TestData.user1));
084: assertEquals(1, TestData.user2.compareTo(TestData.user1));
085: }
086:
087: public void testGetPublicSecret() {
088:
089: //
090: // without password encryption
091: //
092: user = new User();
093: user.setString("password", "password");
094: assertEquals("X03MO1qnZdYdgyfeuILPmQ==", user.getPublicSecret());
095:
096: //
097: // with password encryption
098: //
099: user = new User();
100: user.setPasswordEncrypted(true);
101: user.setString("password", "password");
102: assertEquals("dgx+mS8hYD3yKR2nr1/AwA==", user.getPublicSecret());
103: }
104:
105: public void testHasRole() {
106: TestData.createRoles();
107:
108: user = new User();
109: user.getRoles().add(TestData.role1);
110: user.getRoles().add(TestData.role2);
111:
112: assertTrue(user.hasRole(TestData.siteContext1,
113: DefaultTransferPolicy.ROLE_SYSADMIN));
114: assertTrue(!user.hasRole(TestData.siteContext1,
115: DefaultTransferPolicy.ROLE_WEBMASTER));
116: }
117:
118: public void testSaveNew() {
119: try {
120: TestData.createRoles();
121:
122: user = new User();
123: user.getRoles().add(TestData.role1);
124: user.getContact().setString("firstname", "firstname");
125: user.saveNew();
126:
127: rs = ConnectionSingleton
128: .runQuery("SELECT sitecontext_id,name FROM reg_role WHERE user_id="
129: + user.getInt("id"));
130: assertTrue(rs.next());
131: assertEquals(DefaultTransferPolicy.ROLE_SYSADMIN, rs
132: .getString("name"));
133: assertEquals(1, rs.getInt("sitecontext_id"));
134: assertTrue(!rs.next());
135: ConnectionSingleton.close(rs);
136:
137: contact = new Contact();
138: contact.load(new IntKey(user.getInt("contact_id")));
139: assertEquals("firstname", contact.getString("firstname"));
140: } catch (Exception e) {
141: e.printStackTrace();
142: fail();
143: }
144: }
145:
146: public void testLoad() {
147: try {
148: TestData.createUsers();
149:
150: user = new User();
151: user.load(new IntKey(1));
152:
153: contact = user.getContact();
154: assertEquals("User1", contact.getString("firstname"));
155:
156: list = user.getRoles();
157: assertEquals(2, list.size());
158:
159: role = (Role) list.get(0);
160: assertEquals(DefaultTransferPolicy.ROLE_SYSADMIN, role
161: .getName());
162: assertEquals(TestData.siteContext1, role.getSiteContext());
163:
164: role = (Role) list.get(1);
165: assertEquals(DefaultTransferPolicy.ROLE_SYSADMIN, role
166: .getName());
167: assertEquals(SiteContext.getDefaultContext(), role
168: .getSiteContext());
169: } catch (Exception e) {
170: e.printStackTrace();
171: fail();
172: }
173: }
174:
175: public void testSave() {
176: try {
177: TestData.createUsers();
178:
179: user = new User();
180: user.load(new IntKey(1));
181: user.getContact().setString("firstname", "User1Updated");
182: user.getRoles().add(TestData.role2);
183: user.save();
184:
185: user = new User();
186: user.load(new IntKey(1));
187:
188: contact = user.getContact();
189: assertEquals("User1Updated", contact.getString("firstname"));
190:
191: list = user.getRoles();
192: assertEquals(3, list.size());
193:
194: role = (Role) list.get(0);
195: assertEquals(DefaultTransferPolicy.ROLE_SYSADMIN, role
196: .getName());
197: assertEquals(TestData.siteContext1, role.getSiteContext());
198:
199: role = (Role) list.get(1);
200: assertEquals(DefaultTransferPolicy.ROLE_SYSADMIN, role
201: .getName());
202: assertEquals(SiteContext.getDefaultContext(), role
203: .getSiteContext());
204:
205: role = (Role) list.get(2);
206: assertEquals(DefaultTransferPolicy.ROLE_SITEADMIN, role
207: .getName());
208: assertEquals(TestData.siteContext1, role.getSiteContext());
209: } catch (Exception e) {
210: e.printStackTrace();
211: fail();
212: }
213: }
214:
215: public void testDelete() {
216: try {
217: TestData.createUsers();
218:
219: user = new User();
220: user.load(new IntKey(1));
221: user.delete();
222:
223: rs = ConnectionSingleton
224: .runQuery("SELECT name FROM reg_role WHERE user_id="
225: + user.getInt("id"));
226: assertTrue(!rs.next());
227: ConnectionSingleton.close(rs);
228:
229: try {
230: contact = new Contact();
231: contact.load(new IntKey(user.getInt("contact_id")));
232: fail();
233: } catch (PersistableException e) {
234: }
235: } catch (Exception e) {
236: e.printStackTrace();
237: fail();
238: }
239: }
240:
241: public void testLoadForLogin() {
242: try {
243: TestData.createUsers();
244:
245: user = new User();
246:
247: assertTrue(!user.loadForLogin("invalidlogin"));
248: assertTrue(user.loadForLogin("test1@methodhead.com"));
249:
250: assertEquals(TestData.user1.getInt("id"), user.getInt("id"));
251: } catch (Exception e) {
252: e.printStackTrace();
253: fail();
254: }
255: }
256:
257: public void testLoadAllForSiteContext() {
258: try {
259: TestData.createUsers();
260:
261: user = new User();
262:
263: list = user.loadAllForSiteContext(TestData.siteContext1);
264: assertNotNull(list);
265: assertEquals(2, list.size());
266:
267: user = (User) list.get(0);
268: assertEquals(1, user.getInt("id"));
269:
270: user = (User) list.get(1);
271: assertEquals(2, user.getInt("id"));
272: } catch (Exception e) {
273: e.printStackTrace();
274: fail();
275: }
276: }
277:
278: public void testEncryptPassword() {
279: try {
280: TestData.createUsers();
281:
282: //
283: // password should be encrypted
284: //
285: user = new User();
286: assertEquals("X03MO1qnZdYdgyfeuILPmQ==", user
287: .encryptPassword("password"));
288:
289: //
290: // result should be different for a different password
291: //
292: assertEquals("bLdfZSqbUnmOts8iAQV8cw==", user
293: .encryptPassword("password2"));
294:
295: //
296: // result should be less than 128 characters even for a long password
297: //
298: assertEquals(
299: "f3v9NIcJ3uqs4Z4/U1+MVA==",
300: user
301: .encryptPassword("0123456789012345678901234567890123456789012345678901234567890123"));
302: } catch (Exception e) {
303: e.printStackTrace();
304: fail();
305: }
306: }
307:
308: public void testSetPasswordEncrypted() {
309: try {
310: user = new User();
311:
312: //
313: // password should not encrypt by default
314: //
315: user.setString("password", "password");
316: assertEquals("password", user.getString("password"));
317:
318: //
319: // authentication should work with password
320: //
321: assertEquals(true, user.authenticate("password"));
322: assertEquals(false, user.authenticate("invalidpassword"));
323:
324: //
325: // set the encrypt flag
326: //
327: user.setPasswordEncrypted(true);
328:
329: //
330: // password should be encrypted
331: //
332: user.setString("password", "password");
333: assertEquals("X03MO1qnZdYdgyfeuILPmQ==", user
334: .getString("password"));
335:
336: //
337: // authentication should work with password
338: //
339: assertEquals(true, user.authenticate("password"));
340: assertEquals(false, user.authenticate("invalidpassword"));
341:
342: //
343: // should be able save and load the user
344: //
345: user.getContact().setString("email", "test");
346: user.saveNew();
347: user = new User();
348: user.setPasswordEncrypted(true);
349: user.load(new IntKey(1));
350: assertEquals("X03MO1qnZdYdgyfeuILPmQ==", user
351: .getString("password"));
352:
353: user = new User();
354: user.load(new IntKey(1));
355: user.setPasswordEncrypted(true);
356: assertEquals("X03MO1qnZdYdgyfeuILPmQ==", user
357: .getString("password"));
358:
359: //
360: //
361: //
362: user = new User();
363: user.setPasswordEncrypted(true);
364: user.loadForLogin("test");
365: assertEquals(true, user.authenticate("password"));
366: } catch (Exception e) {
367: e.printStackTrace();
368: fail();
369: }
370: }
371: }
|