001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003: * Steven Grimm <koreth[remove] at midwinter dot com>
004: * Distributed under the terms of either:
005: * - the common development and distribution license (CDDL), v1.0; or
006: * - the GNU Lesser General Public License, v2.1 or later
007: * $Id: TestDatabaseAuthenticatedSessionduration.java 3643 2007-01-12 15:29:45Z gbevin $
008: */
009: package com.uwyn.rife.authentication.elements;
010:
011: import com.meterware.httpunit.GetMethodWebRequest;
012: import com.meterware.httpunit.WebConversation;
013: import com.meterware.httpunit.WebForm;
014: import com.meterware.httpunit.WebRequest;
015: import com.meterware.httpunit.WebResponse;
016: import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsers;
017: import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsersFactory;
018: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
019: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
020: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
021: import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessions;
022: import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessionsFactory;
023: import com.uwyn.rife.authentication.sessionmanagers.SessionManagerFactoryFactory;
024: import com.uwyn.rife.database.Datasource;
025: import com.uwyn.rife.database.Datasources;
026: import com.uwyn.rife.tools.ExceptionUtils;
027:
028: public class TestDatabaseAuthenticatedSessionduration extends
029: TestsuiteDatabaseAuthenticated {
030: private Datasource mDatasource = null;
031:
032: public TestDatabaseAuthenticatedSessionduration(
033: String datasourceName, int siteType, String name) {
034: super (datasourceName, siteType, name);
035:
036: mDatasource = Datasources.getRepInstance().getDatasource(
037: "sessionduration" + datasourceName);
038: mProperties.put("datasource", mDatasource);
039: mProperties
040: .put(
041: SessionManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
042: DatabaseSessionsFactory.class.getName());
043: }
044:
045: public void setUp() throws Exception {
046: super .setUp();
047:
048: DatabaseUsers users = DatabaseUsersFactory
049: .getInstance(mDatasource);
050: try {
051: users.install();
052:
053: users.addRole("admin");
054: users.addRole("maint");
055:
056: users.addUser("guest", new RoleUserAttributes(43,
057: "guestpass"));
058: users.addUser("gbevin", new RoleUserAttributes(432,
059: "yeolpass", new String[] { "admin", "maint" }));
060: users.addUser("johndoe", new RoleUserAttributes(174,
061: "thepassofbass", new String[] { "maint" }));
062: } catch (CredentialsManagerException e) {
063: try {
064: users.remove();
065: } catch (CredentialsManagerException e2) {
066: assertTrue(ExceptionUtils.getExceptionStackTrace(e2),
067: false);
068: }
069: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
070: }
071:
072: DatabaseSessions sessions = (DatabaseSessions) SessionManagerFactoryFactory
073: .getManager(mProperties);
074:
075: try {
076: sessions.install();
077: } catch (SessionManagerException e) {
078: try {
079: sessions.remove();
080: } catch (SessionManagerException e2) {
081: assertTrue(ExceptionUtils.getExceptionStackTrace(e2),
082: false);
083: }
084: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
085: }
086: }
087:
088: public void tearDown() throws Exception {
089: DatabaseUsers users = DatabaseUsersFactory
090: .getInstance(mDatasource);
091:
092: try {
093: users.remove();
094: } catch (CredentialsManagerException e) {
095: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
096: }
097:
098: DatabaseSessions sessions = (DatabaseSessions) SessionManagerFactoryFactory
099: .getManager(mProperties);
100:
101: try {
102: sessions.remove();
103: } catch (SessionManagerException e) {
104: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
105: }
106:
107: super .tearDown();
108: }
109:
110: public void testDatabaseAuthenticatedSessionduration()
111: throws Exception {
112: setupSite("site/authentication_database.xml");
113:
114: WebConversation conversation = new WebConversation();
115: WebRequest request = null;
116: WebResponse response = null;
117: WebForm form = null;
118: String auth_id;
119:
120: request = new GetMethodWebRequest(
121: "http://localhost:8181/authentication/database/sessionduration");
122: response = conversation.getResponse(request);
123: form = response.getForms()[0];
124: form.setParameter("login", "guest");
125: form.setParameter("password", "guestpass");
126: response = form.submit();
127:
128: assertEquals(0, response.getForms().length);
129: auth_id = response.getTitle();
130:
131: request = new GetMethodWebRequest(
132: "http://localhost:8181/authentication/database/sessionduration");
133: request.setParameter("authid", auth_id);
134: response = conversation.getResponse(request);
135:
136: assertEquals(0, response.getForms().length);
137: assertEquals(auth_id, response.getTitle());
138:
139: try {
140: Thread.sleep(2000);
141: } catch (InterruptedException e) {
142: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
143: }
144:
145: request = new GetMethodWebRequest(
146: "http://localhost:8181/authentication/database/sessionduration");
147: request.setParameter("authid", auth_id);
148: response = conversation.getResponse(request);
149:
150: assertEquals(1, response.getForms().length);
151: }
152: }
|