001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.concurrent;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021:
022: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
023:
024: import org.acegisecurity.ui.WebAuthenticationDetails;
025:
026: import org.springframework.mock.web.MockHttpServletRequest;
027: import org.springframework.mock.web.MockHttpSession;
028:
029: /**
030: * Tests {@link ConcurrentSessionControllerImpl}.
031: *
032: * @author Ben Alex
033: * @version $Id: ConcurrentSessionControllerImplTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class ConcurrentSessionControllerImplTests extends TestCase {
036: //~ Methods ========================================================================================================
037:
038: private Authentication createAuthentication(String user,
039: String password) {
040: UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
041: user, password);
042: auth.setDetails(createWebDetails(auth));
043:
044: return auth;
045: }
046:
047: private WebAuthenticationDetails createWebDetails(
048: Authentication auth) {
049: MockHttpSession session = new MockHttpSession();
050: MockHttpServletRequest request = new MockHttpServletRequest();
051: request.setSession(session);
052: request.setUserPrincipal(auth);
053:
054: return new WebAuthenticationDetails(request);
055: }
056:
057: public void testLifecycle() throws Exception {
058: // Build a test fixture
059: ConcurrentSessionControllerImpl sc = new ConcurrentSessionControllerImpl();
060: SessionRegistry registry = new SessionRegistryImpl();
061: sc.setSessionRegistry(registry);
062:
063: // Attempt to authenticate - it should be successful
064: Authentication auth = createAuthentication("bob", "1212");
065: sc.checkAuthenticationAllowed(auth);
066: sc.registerSuccessfulAuthentication(auth);
067:
068: String sessionId1 = ((WebAuthenticationDetails) auth
069: .getDetails()).getSessionId();
070: assertFalse(registry.getSessionInformation(sessionId1)
071: .isExpired());
072:
073: // Attempt to authenticate again - it should still be successful
074: sc.checkAuthenticationAllowed(auth);
075: sc.registerSuccessfulAuthentication(auth);
076:
077: // Attempt to authenticate with a different session for same principal - should fail
078: sc.setExceptionIfMaximumExceeded(true);
079:
080: Authentication auth2 = createAuthentication("bob", "1212");
081: assertFalse(registry.getSessionInformation(sessionId1)
082: .isExpired());
083:
084: try {
085: sc.checkAuthenticationAllowed(auth2);
086: fail("Should have thrown ConcurrentLoginException");
087: } catch (ConcurrentLoginException expected) {
088: assertTrue(true);
089: }
090:
091: // Attempt to authenticate with a different session for same principal - should expire first session
092: sc.setExceptionIfMaximumExceeded(false);
093:
094: Authentication auth3 = createAuthentication("bob", "1212");
095: sc.checkAuthenticationAllowed(auth3);
096: sc.registerSuccessfulAuthentication(auth3);
097:
098: String sessionId3 = ((WebAuthenticationDetails) auth3
099: .getDetails()).getSessionId();
100: assertTrue(registry.getSessionInformation(sessionId1)
101: .isExpired());
102: assertFalse(registry.getSessionInformation(sessionId3)
103: .isExpired());
104: }
105:
106: public void testStartupDetectsInvalidMaximumSessions()
107: throws Exception {
108: ConcurrentSessionControllerImpl sc = new ConcurrentSessionControllerImpl();
109: sc.setMaximumSessions(0);
110:
111: try {
112: sc.afterPropertiesSet();
113: fail("Should have thrown IAE");
114: } catch (IllegalArgumentException expected) {
115: assertTrue(true);
116: }
117: }
118:
119: public void testStartupDetectsInvalidSessionRegistry()
120: throws Exception {
121: ConcurrentSessionControllerImpl sc = new ConcurrentSessionControllerImpl();
122: sc.setSessionRegistry(null);
123:
124: try {
125: sc.afterPropertiesSet();
126: fail("Should have thrown IAE");
127: } catch (IllegalArgumentException expected) {
128: assertTrue(true);
129: }
130: }
131: }
|