    1   /* Copyright 2004 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.intercept.web;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.ConfigAttributeDefinition;
    21  import org.acegisecurity.MockFilterChain;
    22
    23
    24  import org.acegisecurity.SecurityConfig;
    25
    26  import java.util.Iterator;
    27
    28  import org.springframework.mock.web.MockHttpServletRequest;
    29  import org.springframework.mock.web.MockHttpServletResponse;
    30
    31
    32  /***
    33   * Tests {@link FilterInvocationDefinitionSourceEditor} and its associated
    34   * {@link PathBasedFilterInvocationDefinitionMap}.
    35   *
    36   * @author Ben Alex
    37   * @version $Id: FilterInvocationDefinitionSourceEditorWithPathsTests.java,v 1.3 2005/11/17 00:55:50 benalex Exp $
    38   */
    39  public class FilterInvocationDefinitionSourceEditorWithPathsTests
    40      extends TestCase {
    41      //~ Constructors ===========================================================
    42
    43      public FilterInvocationDefinitionSourceEditorWithPathsTests() {
    44          super();
    45      }
    46
    47      public FilterInvocationDefinitionSourceEditorWithPathsTests(String arg0) {
    48          super(arg0);
    49      }
    50
    51      //~ Methods ================================================================
    52
    53      public final void setUp() throws Exception {
    54          super.setUp();
    55      }
    56
    57      public static void main(String[] args) {
    58          junit.textui.TestRunner.run(FilterInvocationDefinitionSourceEditorWithPathsTests.class);
    59      }
    60
    61      public void testAntPathDirectiveIsDetected() {
    62          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    63          editor.setAsText(
    64              "PATTERN_TYPE_APACHE_ANT\r\n/secure/super/*=ROLE_WE_DONT_HAVE\r\n/secure/*=ROLE_SUPERVISOR,ROLE_TELLER");
    65
    66          FilterInvocationDefinitionMap map = (FilterInvocationDefinitionMap) editor
    67              .getValue();
    68          assertTrue(map instanceof PathBasedFilterInvocationDefinitionMap);
    69      }
    70
    71      public void testConvertUrlToLowercaseDefaultSettingUnchangedByEditor() {
    72          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    73          editor.setAsText(
    74              "PATTERN_TYPE_APACHE_ANT\r\n/secure/super/*=ROLE_WE_DONT_HAVE\r\n/secure/*=ROLE_SUPERVISOR,ROLE_TELLER");
    75
    76          PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    77              .getValue();
    78          assertFalse(map.isConvertUrlToLowercaseBeforeComparison());
    79      }
    80
    81      public void testConvertUrlToLowercaseSettingApplied() {
    82          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    83          editor.setAsText(
    84              "CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON\r\nPATTERN_TYPE_APACHE_ANT\r\n/secure/super/*=ROLE_WE_DONT_HAVE\r\n/secure/*=ROLE_SUPERVISOR,ROLE_TELLER");
    85
    86          PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    87              .getValue();
    88          assertTrue(map.isConvertUrlToLowercaseBeforeComparison());
    89      }
    90
    91      public void testIterator() {
    92          FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    93          editor.setAsText(
    94              "PATTERN_TYPE_APACHE_ANT\r\n/secure/super/*=ROLE_WE_DONT_HAVE\r\n/secure/*=ROLE_SUPERVISOR,ROLE_TELLER");
    95
    96          PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    97              .getValue();
    98          Iterator iter = map.getConfigAttributeDefinitions();
    99          int counter = 0;
    100
    101         while (iter.hasNext()) {
    102             iter.next();
    103             counter++;
    104         }
    105
    106         assertEquals(2, counter);
    107     }
    108
    109     public void testMapReturnsNullWhenNoMatchFound() throws Exception {
    110         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    111         editor.setAsText(
    112             "PATTERN_TYPE_APACHE_ANT\r\n/secure/super/*=ROLE_WE_DONT_HAVE");
    113
    114         PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    115             .getValue();
    116
    117         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    118                 null);
    119         httpRequest.setServletPath("/totally/different/path/index.html");
    120
    121         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    122                     httpRequest, new MockHttpServletResponse(),
    123                     new MockFilterChain()));
    124
    125         assertEquals(null, returned);
    126     }
    127
    128     public void testMultiUrlParsing() {
    129         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    130         editor.setAsText(
    131             "PATTERN_TYPE_APACHE_ANT\r\n/secure/super/*=ROLE_WE_DONT_HAVE\r\n/secure/*=ROLE_SUPERVISOR,ROLE_TELLER");
    132
    133         PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    134             .getValue();
    135         assertEquals(2, map.getMapSize());
    136     }
    137
    138     public void testNoArgsConstructor() {
    139         try {
    140             new PathBasedFilterInvocationDefinitionMap().new EntryHolder();
    141             fail("Should have thrown IllegalArgumentException");
    142         } catch (IllegalArgumentException expected) {
    143             assertTrue(true);
    144         }
    145     }
    146
    147     public void testOrderOfEntriesIsPreservedOrderA() {
    148         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    149         editor.setAsText(
    150             "PATTERN_TYPE_APACHE_ANT\r\n/secure/super/**=ROLE_WE_DONT_HAVE,ANOTHER_ROLE\r\n/secure/**=ROLE_SUPERVISOR,ROLE_TELLER");
    151
    152         PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    153             .getValue();
    154
    155         // Test ensures we match the first entry, not the second
    156         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    157                 null);
    158         httpRequest.setServletPath("/secure/super/very_secret.html");
    159
    160         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    161                     httpRequest, new MockHttpServletResponse(),
    162                     new MockFilterChain()));
    163
    164         ConfigAttributeDefinition expected = new ConfigAttributeDefinition();
    165         expected.addConfigAttribute(new SecurityConfig("ROLE_WE_DONT_HAVE"));
    166         expected.addConfigAttribute(new SecurityConfig("ANOTHER_ROLE"));
    167
    168         assertEquals(expected, returned);
    169     }
    170
    171     public void testOrderOfEntriesIsPreservedOrderB() {
    172         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    173         editor.setAsText(
    174             "PATTERN_TYPE_APACHE_ANT\r\n/secure/**=ROLE_SUPERVISOR,ROLE_TELLER\r\n/secure/super/**=ROLE_WE_DONT_HAVE");
    175
    176         PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    177             .getValue();
    178
    179         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    180                 null);
    181         httpRequest.setServletPath("/secure/super/very_secret.html");
    182
    183         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    184                     httpRequest, new MockHttpServletResponse(),
    185                     new MockFilterChain()));
    186
    187         ConfigAttributeDefinition expected = new ConfigAttributeDefinition();
    188         expected.addConfigAttribute(new SecurityConfig("ROLE_SUPERVISOR"));
    189         expected.addConfigAttribute(new SecurityConfig("ROLE_TELLER"));
    190
    191         assertEquals(expected, returned);
    192     }
    193
    194     public void testSingleUrlParsing() throws Exception {
    195         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    196         editor.setAsText(
    197             "PATTERN_TYPE_APACHE_ANT\r\n/secure/super/*=ROLE_WE_DONT_HAVE,ANOTHER_ROLE");
    198
    199         PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    200             .getValue();
    201
    202         MockHttpServletRequest httpRequest = new MockHttpServletRequest(null,
    203                 null);
    204         httpRequest.setServletPath("/secure/super/very_secret.html");
    205
    206         ConfigAttributeDefinition returned = map.getAttributes(new FilterInvocation(
    207                     httpRequest, new MockHttpServletResponse(),
    208                     new MockFilterChain()));
    209
    210         ConfigAttributeDefinition expected = new ConfigAttributeDefinition();
    211         expected.addConfigAttribute(new SecurityConfig("ROLE_WE_DONT_HAVE"));
    212         expected.addConfigAttribute(new SecurityConfig("ANOTHER_ROLE"));
    213
    214         assertEquals(expected, returned);
    215     }
    216
    217     public void testWhitespaceAndCommentsAndLinesWithoutEqualsSignsAreIgnored() {
    218         FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
    219         editor.setAsText(
    220             "         PATTERN_TYPE_APACHE_ANT\r\n    /secure/super/*=ROLE_WE_DONT_HAVE\r\n    /secure/*=ROLE_SUPERVISOR,ROLE_TELLER      \r\n   \r\n     \r\n   // comment line  \r\n    \r\n");
    221
    222         PathBasedFilterInvocationDefinitionMap map = (PathBasedFilterInvocationDefinitionMap) editor
    223             .getValue();
    224         assertEquals(2, map.getMapSize());
    225     }
    226 }