    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.jboss;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.GrantedAuthority;
    21  import org.acegisecurity.GrantedAuthorityImpl;
    22
    23  import org.acegisecurity.adapters.PrincipalAcegiUserToken;
    24
    25  import org.acegisecurity.context.SecurityContextHolder;
    26  import org.acegisecurity.context.SecurityContextImpl;
    27
    28  import org.springframework.mock.web.MockHttpServletRequest;
    29
    30  import java.io.IOException;
    31
    32  import java.security.Principal;
    33
    34  import java.util.HashSet;
    35  import java.util.Set;
    36
    37  import javax.naming.Context;
    38
    39  import javax.security.auth.Subject;
    40
    41  import javax.servlet.Filter;
    42  import javax.servlet.FilterChain;
    43  import javax.servlet.FilterConfig;
    44  import javax.servlet.ServletException;
    45  import javax.servlet.ServletRequest;
    46  import javax.servlet.ServletResponse;
    47
    48
    49  /***
    50   * Tests {@link JbossIntegrationFilter}.
    51   *
    52   * @author Ben Alex
    53   * @version $Id: JbossIntegrationFilterTests.java,v 1.10 2005/11/25 00:26:30 benalex Exp $
    54   */
    55  public class JbossIntegrationFilterTests extends TestCase {
    56      //~ Constructors ===========================================================
    57
    58      public JbossIntegrationFilterTests() {
    59          super();
    60      }
    61
    62      public JbossIntegrationFilterTests(String arg0) {
    63          super(arg0);
    64      }
    65
    66      //~ Methods ================================================================
    67
    68      public static void main(String[] args) {
    69          junit.textui.TestRunner.run(JbossIntegrationFilterTests.class);
    70      }
    71
    72      public void testCorrectOperation() throws Exception {
    73          PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
    74                  "someone", "password",
    75                  new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")},
    76                  null);
    77
    78          JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext(
    79                      makeIntoSubject(principal)));
    80
    81          MockHttpServletRequest request = new MockHttpServletRequest();
    82          MockFilterChain chain = new MockFilterChain();
    83
    84          filter.doFilter(request, null, chain);
    85
    86          assertEquals(principal,
    87              SecurityContextHolder.getContext().getAuthentication());
    88          SecurityContextHolder.setContext(new SecurityContextImpl());
    89      }
    90
    91      public void testReturnsNullIfContextReturnsSomethingOtherThanASubject()
    92          throws Exception {
    93          JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext(
    94                      "THIS_IS_NOT_A_SUBJECT"));
    95
    96          MockHttpServletRequest request = new MockHttpServletRequest();
    97          MockFilterChain chain = new MockFilterChain();
    98
    99          filter.doFilter(request, null, chain);
    100         assertNull(SecurityContextHolder.getContext().getAuthentication());
    101     }
    102
    103     public void testReturnsNullIfInitialContextHasNullPrincipal()
    104         throws Exception {
    105         JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext(
    106                     makeIntoSubject(null)));
    107
    108         MockHttpServletRequest request = new MockHttpServletRequest();
    109         MockFilterChain chain = new MockFilterChain();
    110
    111         filter.doFilter(request, null, chain);
    112         assertNull(SecurityContextHolder.getContext().getAuthentication());
    113     }
    114
    115     public void testReturnsNullIfInitialContextHasNullSubject()
    116         throws Exception {
    117         JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext(
    118                     null));
    119
    120         MockHttpServletRequest request = new MockHttpServletRequest();
    121         MockFilterChain chain = new MockFilterChain();
    122
    123         filter.doFilter(request, null, chain);
    124         assertNull(SecurityContextHolder.getContext().getAuthentication());
    125     }
    126
    127     public void testReturnsNullIfInitialContextIsNull()
    128         throws Exception {
    129         JbossIntegrationFilter filter = new MockJbossIntegrationFilter(null);
    130
    131         MockHttpServletRequest request = new MockHttpServletRequest();
    132         MockFilterChain chain = new MockFilterChain();
    133
    134         filter.doFilter(request, null, chain);
    135         assertNull(SecurityContextHolder.getContext().getAuthentication());
    136     }
    137
    138     public void testReturnsNullIfPrincipalNotAnAuthenticationImplementation()
    139         throws Exception {
    140         JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext(
    141                     makeIntoSubject(new Principal() {
    142                     public String getName() {
    143                         return "MockPrincipal";
    144                     }
    145                 })));
    146
    147         MockHttpServletRequest request = new MockHttpServletRequest();
    148         MockFilterChain chain = new MockFilterChain();
    149
    150         filter.doFilter(request, null, chain);
    151         assertNull(SecurityContextHolder.getContext().getAuthentication());
    152     }
    153
    154     public void testTestingObjectReturnsInitialContext()
    155         throws Exception {
    156         JbossIntegrationFilter filter = new JbossIntegrationFilter();
    157         assertTrue(filter.getLookupContext() instanceof Context);
    158     }
    159
    160     protected void setUp() throws Exception {
    161         super.setUp();
    162         SecurityContextHolder.setContext(new SecurityContextImpl());
    163     }
    164
    165     protected void tearDown() throws Exception {
    166         super.tearDown();
    167         SecurityContextHolder.setContext(new SecurityContextImpl());
    168     }
    169
    170     private void executeFilterInContainerSimulator(FilterConfig filterConfig,
    171         Filter filter, ServletRequest request, ServletResponse response,
    172         FilterChain filterChain) throws ServletException, IOException {
    173         filter.init(filterConfig);
    174         filter.doFilter(request, response, filterChain);
    175         filter.destroy();
    176     }
    177
    178     private Subject makeIntoSubject(Principal principal) {
    179         Set principals = new HashSet();
    180         principals.add(principal);
    181
    182         return new Subject(false, principals, new HashSet(), new HashSet());
    183     }
    184
    185     //~ Inner Classes ==========================================================
    186
    187     private class MockFilterChain implements FilterChain {
    188         public void doFilter(ServletRequest arg0, ServletResponse arg1)
    189             throws IOException, ServletException {}
    190     }
    191 }