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: TestPurgingMemoryAuthenticated.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.TestCaseServerside;
017: import com.uwyn.rife.authentication.SessionManager;
018: import com.uwyn.rife.engine.Gate;
019: import com.uwyn.rife.tools.ExceptionUtils;
020:
021: public class TestPurgingMemoryAuthenticated extends TestCaseServerside {
022: public TestPurgingMemoryAuthenticated(int siteType, String name) {
023: super (siteType, name);
024: }
025:
026: private SessionManager getSessionManagerInstance(Gate gate,
027: String authElementId) {
028: return ((AuthenticatedDeployer) gate.getSite().resolveId(
029: authElementId).getDeployer()).getSessionValidator()
030: .getSessionManager();
031: }
032:
033: public void testPurgingMemoryAuthenticatedBasicInput()
034: throws Exception {
035: Gate gate = setupSite("site/authentication_memory_input.xml");
036:
037: SessionManager session_manager = getSessionManagerInstance(
038: gate, ".INPUT.PURGING_MEMORY_AUTHENTICATED_BASIC");
039: assertEquals(0, session_manager.countSessions());
040:
041: WebConversation conversation = new WebConversation();
042: WebRequest request = null;
043: WebResponse response = null;
044: WebForm form = null;
045: String auth_id;
046:
047: request = new GetMethodWebRequest(
048: "http://localhost:8181/authentication/purging/memory/basic");
049: response = conversation.getResponse(request);
050: form = response.getForms()[0];
051: form.setParameter("login", "guest");
052: form.setParameter("password", "guestpass");
053: response = form.submit();
054:
055: assertEquals(0, response.getForms().length);
056: auth_id = response.getTitle();
057:
058: assertEquals(1, session_manager.countSessions());
059:
060: request = new GetMethodWebRequest(
061: "http://localhost:8181/authentication/purging/memory/basic");
062: response = conversation.getResponse(request);
063: form = response.getForms()[0];
064: form.setParameter("login", "guest");
065: form.setParameter("password", "guestpass");
066: response = form.submit();
067:
068: assertEquals(0, response.getForms().length);
069: auth_id = response.getTitle();
070:
071: assertEquals(2, session_manager.countSessions());
072:
073: try {
074: Thread.sleep(1000);
075: } catch (InterruptedException e) {
076: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
077: }
078:
079: request = new GetMethodWebRequest(
080: "http://localhost:8181/authentication/purging/memory/basic");
081: request.setParameter("authid", auth_id);
082: response = conversation.getResponse(request);
083:
084: assertEquals(1, response.getForms().length);
085:
086: request = new GetMethodWebRequest(
087: "http://localhost:8181/authentication/purging/memory/basic");
088: response = conversation.getResponse(request);
089: form = response.getForms()[0];
090: form.setParameter("login", "guest");
091: form.setParameter("password", "guestpass");
092: response = form.submit();
093:
094: assertEquals(0, response.getForms().length);
095:
096: assertEquals(1, session_manager.countSessions());
097: session_manager.eraseAllSessions();
098: }
099:
100: public void testPurgingMemoryAuthenticatedBasicCookie()
101: throws Exception {
102: Gate gate = setupSite("site/authentication_memory_cookie.xml");
103:
104: SessionManager session_manager = getSessionManagerInstance(
105: gate, ".COOKIE.PURGING_MEMORY_AUTHENTICATED_BASIC");
106: assertEquals(0, session_manager.countSessions());
107:
108: WebConversation conversation1 = new WebConversation();
109: WebRequest request = null;
110: WebResponse response = null;
111: WebForm form = null;
112:
113: request = new GetMethodWebRequest(
114: "http://localhost:8181/authentication/purging/memory/basic");
115: response = conversation1.getResponse(request);
116: form = response.getForms()[0];
117: form.setParameter("login", "guest");
118: form.setParameter("password", "guestpass");
119: response = form.submit();
120:
121: assertEquals("authid", response.getNewCookieNames()[0]);
122: assertEquals(0, response.getForms().length);
123:
124: assertEquals(1, session_manager.countSessions());
125:
126: WebConversation conversation2 = new WebConversation();
127: request = new GetMethodWebRequest(
128: "http://localhost:8181/authentication/purging/memory/basic");
129: response = conversation2.getResponse(request);
130: form = response.getForms()[0];
131: form.setParameter("login", "guest");
132: form.setParameter("password", "guestpass");
133: response = form.submit();
134:
135: assertEquals("authid", response.getNewCookieNames()[0]);
136: assertEquals(0, response.getForms().length);
137:
138: assertEquals(2, session_manager.countSessions());
139:
140: try {
141: Thread.sleep(1000);
142: } catch (InterruptedException e) {
143: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
144: }
145:
146: request = new GetMethodWebRequest(
147: "http://localhost:8181/authentication/purging/memory/basic");
148: response = conversation1.getResponse(request);
149:
150: assertEquals(0, response.getNewCookieNames().length);
151: assertEquals(1, response.getForms().length);
152:
153: request = new GetMethodWebRequest(
154: "http://localhost:8181/authentication/purging/memory/basic");
155: response = conversation1.getResponse(request);
156: form = response.getForms()[0];
157: form.setParameter("login", "guest");
158: form.setParameter("password", "guestpass");
159: response = form.submit();
160:
161: assertEquals("authid", response.getNewCookieNames()[0]);
162: assertEquals(0, response.getForms().length);
163:
164: assertEquals(1, session_manager.countSessions());
165: session_manager.eraseAllSessions();
166: }
167: }
|