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.captcha;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.ConfigAttributeDefinition;
021: import org.acegisecurity.MockFilterChain;
022: import org.acegisecurity.SecurityConfig;
023:
024: import org.acegisecurity.context.SecurityContextHolder;
025:
026: import org.acegisecurity.intercept.web.FilterInvocation;
027:
028: import org.springframework.mock.web.MockHttpServletRequest;
029: import org.springframework.mock.web.MockHttpServletResponse;
030:
031: import java.io.IOException;
032:
033: import javax.servlet.ServletException;
034:
035: /**
036: * Tests {@link org.acegisecurity.captcha.CaptchaChannelProcessorTemplate}
037: *
038: * @author marc antoine Garrigue
039: * @version $Id: CaptchaChannelProcessorTemplateTests.java 1496 2006-05-23 13:38:33Z benalex $
040: */
041: public class CaptchaChannelProcessorTemplateTests extends TestCase {
042: //~ Methods ========================================================================================================
043:
044: private MockHttpServletResponse decideWithNewResponse(
045: ConfigAttributeDefinition cad,
046: CaptchaChannelProcessorTemplate processor,
047: MockHttpServletRequest request) throws IOException,
048: ServletException {
049: MockHttpServletResponse response;
050: MockFilterChain chain;
051: FilterInvocation fi;
052: response = new MockHttpServletResponse();
053: chain = new MockFilterChain();
054: fi = new FilterInvocation(request, response, chain);
055: processor.decide(fi, cad);
056:
057: return response;
058: }
059:
060: public void setUp() {
061: SecurityContextHolder.clearContext();
062: }
063:
064: public void tearDown() {
065: SecurityContextHolder.clearContext();
066: }
067:
068: public void testContextRedirect() throws Exception {
069: CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
070: processor.setKeyword("X");
071:
072: ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
073: cad.addConfigAttribute(new SecurityConfig("Y"));
074:
075: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
076: SecurityContextHolder.setContext(context);
077:
078: CaptchaEntryPoint epoint = new CaptchaEntryPoint();
079: epoint.setCaptchaFormUrl("/jcaptcha.do");
080: epoint.setIncludeOriginalRequest(false);
081:
082: processor.setEntryPoint(epoint);
083:
084: MockHttpServletRequest request = new MockHttpServletRequest();
085: request.setQueryString("info=true");
086: request.setServerName("localhost");
087: request.setContextPath("/demo");
088: request.setServletPath("/restricted");
089: request.setScheme("http");
090: request.setServerPort(8000);
091:
092: MockHttpServletResponse response = new MockHttpServletResponse();
093: MockFilterChain chain = new MockFilterChain();
094: FilterInvocation fi = new FilterInvocation(request, response,
095: chain);
096:
097: processor.decide(fi, cad);
098: assertEquals(null, response.getRedirectedUrl());
099: processor.setKeyword("Y");
100: response = decideWithNewResponse(cad, processor, request);
101: assertEquals("http://localhost:8000/demo/jcaptcha.do", response
102: .getRedirectedUrl());
103: context.setHuman();
104: response = decideWithNewResponse(cad, processor, request);
105: assertEquals(null, response.getRedirectedUrl());
106: }
107:
108: public void testDecideRejectsNulls() throws Exception {
109: CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
110: processor.setEntryPoint(new CaptchaEntryPoint());
111: processor.setKeyword("X");
112: processor.afterPropertiesSet();
113:
114: try {
115: processor.decide(null, null);
116: fail("Should have thrown IllegalArgumentException");
117: } catch (IllegalArgumentException expected) {
118: assertTrue(true);
119: }
120: }
121:
122: public void testGettersSetters() {
123: CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
124: assertEquals(null, processor.getKeyword());
125: processor.setKeyword("X");
126: assertEquals("X", processor.getKeyword());
127:
128: assertEquals(0, processor.getThresold());
129: processor.setThresold(1);
130: assertEquals(1, processor.getThresold());
131:
132: assertTrue(processor.getEntryPoint() == null);
133: processor.setEntryPoint(new CaptchaEntryPoint());
134: assertTrue(processor.getEntryPoint() != null);
135: }
136:
137: public void testIncrementRequestCount() throws Exception {
138: CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
139: processor.setKeyword("X");
140:
141: ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
142: cad.addConfigAttribute(new SecurityConfig("X"));
143:
144: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
145: SecurityContextHolder.setContext(context);
146:
147: CaptchaEntryPoint epoint = new CaptchaEntryPoint();
148: epoint.setCaptchaFormUrl("/jcaptcha.do");
149: processor.setEntryPoint(epoint);
150:
151: MockHttpServletRequest request = new MockHttpServletRequest();
152: request.setQueryString("info=true");
153: request.setServerName("localhost");
154: request.setContextPath("/demo");
155: request.setServletPath("/restricted");
156: request.setScheme("http");
157: request.setServerPort(8000);
158:
159: MockHttpServletResponse response = new MockHttpServletResponse();
160: MockFilterChain chain = new MockFilterChain();
161: FilterInvocation fi = new FilterInvocation(request, response,
162: chain);
163:
164: processor.decide(fi, cad);
165: assertEquals(0, context
166: .getHumanRestrictedResourcesRequestsCount());
167: context.setHuman();
168: decideWithNewResponse(cad, processor, request);
169: assertEquals(1, context
170: .getHumanRestrictedResourcesRequestsCount());
171: decideWithNewResponse(cad, processor, request);
172: assertEquals(2, context
173: .getHumanRestrictedResourcesRequestsCount());
174: processor.setKeyword("Y");
175: decideWithNewResponse(cad, processor, request);
176: assertEquals(2, context
177: .getHumanRestrictedResourcesRequestsCount());
178: context = new CaptchaSecurityContextImpl();
179: decideWithNewResponse(cad, processor, request);
180: assertEquals(0, context
181: .getHumanRestrictedResourcesRequestsCount());
182: }
183:
184: public void testMissingEntryPoint() throws Exception {
185: CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
186: processor.setEntryPoint(null);
187:
188: try {
189: processor.afterPropertiesSet();
190: fail("Should have thrown IllegalArgumentException");
191: } catch (IllegalArgumentException expected) {
192: assertEquals("entryPoint required", expected.getMessage());
193: }
194: }
195:
196: public void testMissingKeyword() throws Exception {
197: CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
198: processor.setKeyword(null);
199:
200: try {
201: processor.afterPropertiesSet();
202: fail("Should have thrown IllegalArgumentException");
203: } catch (IllegalArgumentException expected) {
204: }
205:
206: processor.setKeyword("");
207:
208: try {
209: processor.afterPropertiesSet();
210: fail("Should have thrown IllegalArgumentException");
211: } catch (IllegalArgumentException expected) {
212: }
213: }
214:
215: public void testSupports() {
216: CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
217: processor.setKeyword("X");
218: assertTrue(processor.supports(new SecurityConfig(processor
219: .getKeyword())));
220:
221: assertTrue(processor.supports(new SecurityConfig("X")));
222:
223: assertFalse(processor.supports(null));
224:
225: assertFalse(processor.supports(new SecurityConfig(
226: "NOT_SUPPORTED")));
227: }
228:
229: //~ Inner Classes ==================================================================================================
230:
231: private class TestHumanityCaptchaChannelProcessor extends
232: CaptchaChannelProcessorTemplate {
233: boolean isContextValidConcerningHumanity(
234: CaptchaSecurityContext context) {
235: return context.isHuman();
236: }
237: }
238: }
|