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.adapters;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.GrantedAuthority;
21  import org.acegisecurity.GrantedAuthorityImpl;
22  
23  import org.acegisecurity.context.SecurityContextHolder;
24  
25  import org.acegisecurity.util.MockFilterChain;
26  
27  import org.springframework.mock.web.MockHttpServletRequest;
28  import org.springframework.mock.web.MockHttpServletResponse;
29  
30  
31  /**
32   * Tests {@link HttpRequestIntegrationFilter}.
33   *
34   * @author Ben Alex
35   * @version $Id: HttpRequestIntegrationFilterTests.java 1496 2006-05-23 13:38:33Z benalex $
36   */
37  public class HttpRequestIntegrationFilterTests extends TestCase {
38      //~ Constructors ===================================================================================================
39  
40      public HttpRequestIntegrationFilterTests() {
41          super();
42      }
43  
44      public HttpRequestIntegrationFilterTests(String arg0) {
45          super(arg0);
46      }
47  
48      //~ Methods ========================================================================================================
49  
50      public static void main(String[] args) {
51          junit.textui.TestRunner.run(HttpRequestIntegrationFilterTests.class);
52      }
53  
54      protected void setUp() throws Exception {
55          super.setUp();
56          SecurityContextHolder.getContext().setAuthentication(null);
57      }
58  
59      protected void tearDown() throws Exception {
60          super.tearDown();
61          SecurityContextHolder.getContext().setAuthentication(null);
62      }
63  
64      public void testCorrectOperation() throws Exception {
65          HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
66          PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", "someone", "password",
67                  new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}, null);
68  
69          MockHttpServletRequest request = new MockHttpServletRequest();
70          request.setUserPrincipal(principal);
71  
72          MockHttpServletResponse response = new MockHttpServletResponse();
73          MockFilterChain chain = new MockFilterChain(true);
74  
75          filter.doFilter(request, response, chain);
76  
77          if (!(SecurityContextHolder.getContext().getAuthentication() instanceof PrincipalAcegiUserToken)) {
78              System.out.println(SecurityContextHolder.getContext().getAuthentication());
79              fail("Should have returned PrincipalAcegiUserToken");
80          }
81  
82          PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) SecurityContextHolder.getContext()
83                                                                                              .getAuthentication();
84          assertEquals(principal, castResult);
85      }
86  
87      public void testHandlesIfHttpRequestIsNullForSomeReason()
88          throws Exception {
89          HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
90  
91          try {
92              filter.doFilter(null, null, null);
93              fail("Should have thrown IllegalArgumentException");
94          } catch (IllegalArgumentException expected) {
95              assertTrue(true);
96          }
97      }
98  
99      public void testHandlesIfThereIsNoPrincipal() throws Exception {
100         HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
101         MockHttpServletRequest request = new MockHttpServletRequest();
102         MockHttpServletResponse response = new MockHttpServletResponse();
103         MockFilterChain chain = new MockFilterChain(true);
104 
105         assertNull(SecurityContextHolder.getContext().getAuthentication());
106         filter.doFilter(request, response, chain);
107         assertNull(SecurityContextHolder.getContext().getAuthentication());
108     }
109 }