1   /* Copyright 2004, 2005 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  import org.acegisecurity.ui.WebAuthenticationDetails;
23  
24  import org.springframework.mock.web.MockHttpServletRequest;
25  
26  
27  /***
28   * Tests {@link AuthenticationProcessingFilter}.
29   *
30   * @author Ben Alex
31   * @version $Id: AuthenticationProcessingFilterTests.java,v 1.9 2005/11/17 00:56:10 benalex Exp $
32   */
33  public class AuthenticationProcessingFilterTests extends TestCase {
34      //~ Constructors ===========================================================
35  
36      public AuthenticationProcessingFilterTests() {
37          super();
38      }
39  
40      public AuthenticationProcessingFilterTests(String arg0) {
41          super(arg0);
42      }
43  
44      //~ Methods ================================================================
45  
46      public final void setUp() throws Exception {
47          super.setUp();
48      }
49  
50      public static void main(String[] args) {
51          junit.textui.TestRunner.run(AuthenticationProcessingFilterTests.class);
52      }
53  
54      public void testGetters() {
55          AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
56          assertEquals("/j_acegi_security_check",
57              filter.getDefaultFilterProcessesUrl());
58      }
59  
60      public void testNormalOperation() throws Exception {
61          MockHttpServletRequest request = new MockHttpServletRequest();
62          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_USERNAME_KEY,
63              "marissa");
64          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_PASSWORD_KEY,
65              "koala");
66  
67          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
68  
69          AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
70          filter.setAuthenticationManager(authMgr);
71          filter.init(null);
72  
73          Authentication result = filter.attemptAuthentication(request);
74          assertTrue(result != null);
75          assertEquals("127.0.0.1",
76              ((WebAuthenticationDetails) result.getDetails()).getRemoteAddress());
77      }
78  
79      public void testNullPasswordHandledGracefully() throws Exception {
80          MockHttpServletRequest request = new MockHttpServletRequest();
81          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_USERNAME_KEY,
82              "marissa");
83  
84          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
85  
86          AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
87          filter.setAuthenticationManager(authMgr);
88          filter.init(null);
89  
90          Authentication result = filter.attemptAuthentication(request);
91          assertTrue(result != null);
92      }
93  
94      public void testNullUsernameHandledGracefully() throws Exception {
95          MockHttpServletRequest request = new MockHttpServletRequest();
96          request.addParameter(AuthenticationProcessingFilter.ACEGI_SECURITY_FORM_PASSWORD_KEY,
97              "koala");
98  
99          MockAuthenticationManager authMgr = new MockAuthenticationManager(true);
100 
101         AuthenticationProcessingFilter filter = new AuthenticationProcessingFilter();
102         filter.setAuthenticationManager(authMgr);
103         filter.init(null);
104 
105         Authentication result = filter.attemptAuthentication(request);
106         assertTrue(result != null);
107     }
108 }