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.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,v 1.9 2005/11/25 00:26:29 benalex Exp $
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      public void testCorrectOperation() throws Exception {
55          HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
56          PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
57                  "someone", "password",
58                  new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")},
59                  null);
60  
61          MockHttpServletRequest request = new MockHttpServletRequest();
62          request.setUserPrincipal(principal);
63  
64          MockHttpServletResponse response = new MockHttpServletResponse();
65          MockFilterChain chain = new MockFilterChain(true);
66  
67          filter.doFilter(request, response, chain);
68  
69          if (!(SecurityContextHolder.getContext().getAuthentication() instanceof PrincipalAcegiUserToken)) {
70              System.out.println(SecurityContextHolder.getContext()
71                                                      .getAuthentication());
72              fail("Should have returned PrincipalAcegiUserToken");
73          }
74  
75          PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) SecurityContextHolder.getContext()
76                                                                                              .getAuthentication();
77          assertEquals(principal, castResult);
78      }
79  
80      public void testHandlesIfHttpRequestIsNullForSomeReason()
81          throws Exception {
82          HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
83  
84          try {
85              filter.doFilter(null, null, null);
86              fail("Should have thrown IllegalArgumentException");
87          } catch (IllegalArgumentException expected) {
88              assertTrue(true);
89          }
90      }
91  
92      public void testHandlesIfThereIsNoPrincipal() throws Exception {
93          HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
94          MockHttpServletRequest request = new MockHttpServletRequest();
95          MockHttpServletResponse response = new MockHttpServletResponse();
96          MockFilterChain chain = new MockFilterChain(true);
97  
98          assertNull(SecurityContextHolder.getContext().getAuthentication());
99          filter.doFilter(request, response, chain);
100         assertNull(SecurityContextHolder.getContext().getAuthentication());
101     }
102 
103     protected void setUp() throws Exception {
104         super.setUp();
105         SecurityContextHolder.getContext().setAuthentication(null);
106     }
107 
108     protected void tearDown() throws Exception {
109         super.tearDown();
110         SecurityContextHolder.getContext().setAuthentication(null);
111     }
112 }