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: TestDatabaseLogout.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.RememberManagerException;
021: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
022: import com.uwyn.rife.authentication.remembermanagers.DatabaseRemember;
023: import com.uwyn.rife.authentication.remembermanagers.DatabaseRememberFactory;
024: import com.uwyn.rife.authentication.remembermanagers.RememberManagerFactoryFactory;
025: import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessions;
026: import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessionsFactory;
027: import com.uwyn.rife.authentication.sessionmanagers.SessionManagerFactoryFactory;
028: import com.uwyn.rife.database.Datasource;
029: import com.uwyn.rife.database.Datasources;
030: import com.uwyn.rife.tools.ExceptionUtils;
031:
032: public class TestDatabaseLogout extends TestsuiteDatabaseAuthenticated {
033: private Datasource mDatasource = null;
034:
035: public TestDatabaseLogout(String datasourceName, int siteType,
036: String name) {
037: super (datasourceName, siteType, name);
038:
039: mDatasource = Datasources.getRepInstance().getDatasource(
040: datasourceName);
041: mProperties.put("datasource", mDatasource);
042: mProperties
043: .put(
044: SessionManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
045: DatabaseSessionsFactory.class.getName());
046: mProperties
047: .put(
048: RememberManagerFactoryFactory.PROPERTYNAME_FACTORY_CLASS,
049: DatabaseRememberFactory.class.getName());
050: }
051:
052: public void setUp() throws Exception {
053: super .setUp();
054:
055: DatabaseUsers users = DatabaseUsersFactory
056: .getInstance(mDatasource);
057: try {
058: users.install();
059:
060: users.addRole("admin");
061: users.addRole("maint");
062:
063: users.addUser("guest", new RoleUserAttributes(43,
064: "guestpass"));
065: users.addUser("gbevin", new RoleUserAttributes(432,
066: "yeolpass", new String[] { "admin", "maint" }));
067: users.addUser("johndoe", new RoleUserAttributes(174,
068: "thepassofbass", new String[] { "maint" }));
069: } catch (CredentialsManagerException e) {
070: try {
071: users.remove();
072: } catch (CredentialsManagerException e2) {
073: assertTrue(ExceptionUtils.getExceptionStackTrace(e2),
074: false);
075: }
076: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
077: }
078:
079: DatabaseSessions sessions = (DatabaseSessions) SessionManagerFactoryFactory
080: .getManager(mProperties);
081:
082: try {
083: sessions.install();
084: } catch (SessionManagerException e) {
085: try {
086: sessions.remove();
087: } catch (SessionManagerException e2) {
088: assertTrue(ExceptionUtils.getExceptionStackTrace(e2),
089: false);
090: }
091: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
092: }
093:
094: DatabaseRemember remember = (DatabaseRemember) RememberManagerFactoryFactory
095: .getManager(mProperties);
096:
097: try {
098: remember.install();
099: } catch (RememberManagerException e) {
100: try {
101: remember.remove();
102: } catch (RememberManagerException e2) {
103: assertTrue(ExceptionUtils.getExceptionStackTrace(e2),
104: false);
105: }
106: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
107: }
108: }
109:
110: public void tearDown() throws Exception {
111: DatabaseUsers users = DatabaseUsersFactory
112: .getInstance(mDatasource);
113:
114: try {
115: users.remove();
116: } catch (CredentialsManagerException e) {
117: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
118: }
119:
120: DatabaseSessions sessions = (DatabaseSessions) SessionManagerFactoryFactory
121: .getManager(mProperties);
122:
123: try {
124: sessions.remove();
125: } catch (SessionManagerException e) {
126: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
127: }
128:
129: DatabaseRemember remember = (DatabaseRemember) RememberManagerFactoryFactory
130: .getManager(mProperties);
131:
132: try {
133: remember.remove();
134: } catch (RememberManagerException e) {
135: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
136: }
137:
138: super .tearDown();
139: }
140:
141: public void testDatabaseLogoutTemplateBasic() throws Exception {
142: setupSite("site/authentication_database.xml");
143:
144: WebConversation conversation = new WebConversation();
145: WebRequest request = null;
146: WebResponse response = null;
147: WebForm form = null;
148:
149: request = new GetMethodWebRequest(
150: "http://localhost:8181/authentication/database/basic");
151: response = conversation.getResponse(request);
152: form = response.getForms()[0];
153: form.setParameter("login", "gbevin");
154: form.setParameter("password", "yeolpass");
155: response = form.submit();
156: assertEquals(0, response.getForms().length);
157:
158: response = response.getLinkWith("reload").click();
159: assertEquals(0, response.getForms().length);
160:
161: WebResponse response_logout = response.getLinkWith(
162: "logout template").click();
163: assertEquals("logged out database", response_logout.getTitle());
164:
165: response = response.getLinkWith("reload").click();
166: assertEquals(1, response.getForms().length);
167: }
168:
169: public void testDatabaseLogoutPassthroughBasic() throws Exception {
170: setupSite("site/authentication_database.xml");
171:
172: WebConversation conversation = new WebConversation();
173: WebRequest request = null;
174: WebResponse response = null;
175: WebForm form = null;
176:
177: request = new GetMethodWebRequest(
178: "http://localhost:8181/authentication/database/basic");
179: response = conversation.getResponse(request);
180: form = response.getForms()[0];
181: form.setParameter("login", "guest");
182: form.setParameter("password", "guestpass");
183: response = form.submit();
184: assertEquals(0, response.getForms().length);
185:
186: response = response.getLinkWith("reload").click();
187: assertEquals(0, response.getForms().length);
188:
189: response = response.getLinkWith("logout passthrough").click();
190: assertEquals(1, response.getForms().length);
191: form = response.getForms()[0];
192: form.setParameter("login", "guest");
193: form.setParameter("password", "guestpass");
194: response = form.submit();
195: assertEquals(0, response.getForms().length);
196: }
197:
198: public void testDatabaseLogoutPrecedence() throws Exception {
199: setupSite("site/authentication_database.xml");
200:
201: WebConversation conversation = new WebConversation();
202: WebRequest request = null;
203: WebResponse response = null;
204: WebForm form = null;
205:
206: request = new GetMethodWebRequest(
207: "http://localhost:8181/authentication/database/precedence");
208: response = conversation.getResponse(request);
209: form = response.getForms()[0];
210: form.setParameter("login", "guest");
211: form.setParameter("password", "guestpass");
212: response = form.submit();
213: assertEquals(0, response.getForms().length);
214:
215: response = response.getLinkWith("reload").click();
216: assertEquals(0, response.getForms().length);
217:
218: response = response.getLinkWith("logout").click();
219: assertEquals(1, response.getForms().length);
220: form = response.getForms()[0];
221: form.setParameter("login", "guest");
222: form.setParameter("password", "guestpass");
223: response = form.submit();
224: assertEquals(0, response.getForms().length);
225: }
226:
227: public void testDatabaseLogoutTemplateRemember() throws Exception {
228: setupSite("site/authentication_database.xml");
229:
230: WebConversation conversation = new WebConversation();
231: WebRequest request = null;
232: WebResponse response = null;
233: WebForm form = null;
234:
235: request = new GetMethodWebRequest(
236: "http://localhost:8181/authentication/database/remember");
237: response = conversation.getResponse(request);
238: form = response.getForms()[0];
239: form.setParameter("login", "gbevin");
240: form.setParameter("password", "yeolpass");
241: form.setCheckbox("remember", true);
242: assertNull(conversation.getCookieValue("rememberid"));
243: response = form.submit();
244: assertEquals(0, response.getForms().length);
245: assertNotNull(conversation.getCookieValue("rememberid"));
246: String rememberid = conversation.getCookieValue("rememberid");
247:
248: response = response.getLinkWith("reload").click();
249: assertEquals(0, response.getForms().length);
250:
251: request = new GetMethodWebRequest(
252: "http://localhost:8181/authentication/database/remember");
253: response = conversation.getResponse(request);
254: assertEquals(0, response.getForms().length);
255:
256: WebResponse response_logout = response.getLinkWith(
257: "logout template").click();
258: assertEquals("logged out database", response_logout.getTitle());
259: assertEquals("", conversation.getCookieValue("rememberid"));
260:
261: response = response.getLinkWith("reload").click();
262: assertEquals(1, response.getForms().length);
263:
264: // ensure that the rememberid has also been erased from the backend
265: conversation.addCookie("rememberid", rememberid);
266:
267: request = new GetMethodWebRequest(
268: "http://localhost:8181/authentication/database/remember");
269: response = conversation.getResponse(request);
270: assertEquals(1, response.getForms().length);
271: }
272:
273: public void testDatabaseLogoutPassthroughRemember()
274: throws Exception {
275: setupSite("site/authentication_database.xml");
276:
277: WebConversation conversation = new WebConversation();
278: WebRequest request = null;
279: WebResponse response = null;
280: WebForm form = null;
281:
282: request = new GetMethodWebRequest(
283: "http://localhost:8181/authentication/database/remember");
284: response = conversation.getResponse(request);
285: form = response.getForms()[0];
286: form.setParameter("login", "gbevin");
287: form.setParameter("password", "yeolpass");
288: form.setCheckbox("remember", true);
289: assertNull(conversation.getCookieValue("rememberid"));
290: response = form.submit();
291: assertEquals(0, response.getForms().length);
292: assertNotNull(conversation.getCookieValue("rememberid"));
293: String rememberid = conversation.getCookieValue("rememberid");
294:
295: response = response.getLinkWith("reload").click();
296: assertEquals(0, response.getForms().length);
297:
298: request = new GetMethodWebRequest(
299: "http://localhost:8181/authentication/database/remember");
300: response = conversation.getResponse(request);
301: assertEquals(0, response.getForms().length);
302:
303: response = response.getLinkWith("logout passthrough").click();
304: assertEquals(1, response.getForms().length);
305: assertEquals("", conversation.getCookieValue("rememberid"));
306:
307: request = new GetMethodWebRequest(
308: "http://localhost:8181/authentication/database/remember");
309: response = conversation.getResponse(request);
310: assertEquals(1, response.getForms().length);
311:
312: // ensure that the rememberid has also been erased from the backend
313: conversation.addCookie("rememberid", rememberid);
314:
315: request = new GetMethodWebRequest(
316: "http://localhost:8181/authentication/database/remember");
317: response = conversation.getResponse(request);
318: assertEquals(1, response.getForms().length);
319: }
320: }
|