1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.ui.webapp;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.MockAuthenticationManager;
22  
23  import org.acegisecurity.ui.WebAuthenticationDetails;
24  
25  import org.springframework.mock.web.MockHttpServletRequest;
26  
27  
28  /**
29   * Tests {@link AuthenticationProcessingFilter}.
30   *
31   * @author Ben Alex
32   * @version $Id: AuthenticationProcessingFilterTests.java 1496 2006-05-23 13:38:33Z benalex $
33   */
34  public class AuthenticationProcessingFilterTests extends TestCase {
35      //~ Constructors ===================================================================================================
36  
37      public AuthenticationProcessingFilterTests() {
38          super();
39      }
40  
41      public AuthenticationProcessingFilterTests(String arg0) {
42          super(arg0);
43      }
44  
45      //~ Methods ========================================================================================================
46  
47      public static void main(String[] args) {
48          junit.textui.TestRunner.run(AuthenticationProcessingFilterTests.class);
49      }
50  
51      public final void setUp() throws Exception {
52          super.setUp();
53      }
54  
55      public void testGetters() {
56          AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
57          assertEquals("/j_acegi_security_check", filter.getDefaultFilterProcessesUrl());
58      }
59  
60      public void testNormalOperation() throws Exception {
61          MockHttpServletRequest request = new MockHttpServletRequest();
62          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_USERNAME_KEY, "marissa");
63          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_PASSWORD_KEY, "koala");
64  
65          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
66  
67          AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
68          filter.setAuthenticationManager(authMgr);
69          filter.init(null);
70  
71          Authentication result = filter.attemptAuthentication(request);
72          assertTrue(result != null);
73          assertEquals("127.0.0.1", ((WebAuthenticationDetails) result.getDetails()).getRemoteAddress());
74      }
75  
76      public void testNullPasswordHandledGracefully() throws Exception {
77          MockHttpServletRequest request = new MockHttpServletRequest();
78          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_USERNAME_KEY, "marissa");
79  
80          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
81  
82          AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
83          filter.setAuthenticationManager(authMgr);
84          filter.init(null);
85  
86          Authentication result = filter.attemptAuthentication(request);
87          assertTrue(result != null);
88      }
89  
90      public void testNullUsernameHandledGracefully() throws Exception {
91          MockHttpServletRequest request = new MockHttpServletRequest();
92          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_PASSWORD_KEY, "koala");
93  
94          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
95  
96          AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
97          filter.setAuthenticationManager(authMgr);
98          filter.init(null);
99  
100         Authentication result = filter.attemptAuthentication(request);
101         assertTrue(result != null);
102     }
103 }