    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.taglibs.authz;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.Authentication;
    21  import org.acegisecurity.GrantedAuthority;
    22  import org.acegisecurity.MockAclManager;
    23  import org.acegisecurity.MockApplicationContext;
    24  import org.acegisecurity.acl.AclEntry;
    25  import org.acegisecurity.acl.AclManager;
    26  import org.acegisecurity.acl.basic.MockAclObjectIdentity;
    27  import org.acegisecurity.acl.basic.SimpleAclEntry;
    28  import org.acegisecurity.context.SecurityContextHolder;
    29  import org.acegisecurity.providers.TestingAuthenticationToken;
    30
    31  import org.springframework.context.ApplicationContext;
    32  import org.springframework.context.ConfigurableApplicationContext;
    33
    34  import javax.servlet.jsp.JspException;
    35  import javax.servlet.jsp.PageContext;
    36  import javax.servlet.jsp.tagext.Tag;
    37
    38
    39  /***
    40   * Tests {@link AclTag}.
    41   *
    42   * @author Ben Alex
    43   * @version $Id: AclTagTests.java,v 1.5 2005/11/17 00:55:51 benalex Exp $
    44   */
    45  public class AclTagTests extends TestCase {
    46      //~ Instance fields ========================================================
    47
    48      private final MyAclTag aclTag = new MyAclTag();
    49
    50      //~ Methods ================================================================
    51
    52      public void testInclusionDeniedWhenAclManagerUnawareOfObject()
    53          throws JspException {
    54          Authentication auth = new TestingAuthenticationToken("marissa",
    55                  "koala", new GrantedAuthority[] {});
    56          SecurityContextHolder.getContext().setAuthentication(auth);
    57
    58          aclTag.setHasPermission(new Long(SimpleAclEntry.ADMINISTRATION)
    59              .toString());
    60          aclTag.setDomainObject(new Integer(54));
    61          assertEquals(Tag.SKIP_BODY, aclTag.doStartTag());
    62
    63          SecurityContextHolder.getContext().setAuthentication(null);
    64      }
    65
    66      public void testInclusionDeniedWhenNoListOfPermissionsGiven()
    67          throws JspException {
    68          Authentication auth = new TestingAuthenticationToken("marissa",
    69                  "koala", new GrantedAuthority[] {});
    70          SecurityContextHolder.getContext().setAuthentication(auth);
    71
    72          aclTag.setHasPermission(null);
    73          aclTag.setDomainObject("object1");
    74          assertEquals(Tag.SKIP_BODY, aclTag.doStartTag());
    75
    76          SecurityContextHolder.getContext().setAuthentication(null);
    77      }
    78
    79      public void testInclusionDeniedWhenPrincipalDoesNotHoldAnyPermissions()
    80          throws JspException {
    81          Authentication auth = new TestingAuthenticationToken("john", "crow",
    82                  new GrantedAuthority[] {});
    83          SecurityContextHolder.getContext().setAuthentication(auth);
    84
    85          aclTag.setHasPermission(new Integer(SimpleAclEntry.ADMINISTRATION)
    86              + "," + new Integer(SimpleAclEntry.READ));
    87          assertEquals(new Integer(SimpleAclEntry.ADMINISTRATION) + ","
    88              + new Integer(SimpleAclEntry.READ), aclTag.getHasPermission());
    89          aclTag.setDomainObject("object1");
    90          assertEquals("object1", aclTag.getDomainObject());
    91          assertEquals(Tag.SKIP_BODY, aclTag.doStartTag());
    92
    93          SecurityContextHolder.getContext().setAuthentication(null);
    94      }
    95
    96      public void testInclusionDeniedWhenPrincipalDoesNotHoldRequiredPermissions()
    97          throws JspException {
    98          Authentication auth = new TestingAuthenticationToken("marissa",
    99                  "koala", new GrantedAuthority[] {});
    100         SecurityContextHolder.getContext().setAuthentication(auth);
    101
    102         aclTag.setHasPermission(new Integer(SimpleAclEntry.DELETE).toString());
    103         aclTag.setDomainObject("object1");
    104         assertEquals(Tag.SKIP_BODY, aclTag.doStartTag());
    105
    106         SecurityContextHolder.getContext().setAuthentication(null);
    107     }
    108
    109     public void testInclusionDeniedWhenSecurityContextEmpty()
    110         throws JspException {
    111         SecurityContextHolder.getContext().setAuthentication(null);
    112
    113         aclTag.setHasPermission(new Long(SimpleAclEntry.ADMINISTRATION)
    114             .toString());
    115         aclTag.setDomainObject("object1");
    116         assertEquals(Tag.SKIP_BODY, aclTag.doStartTag());
    117
    118         SecurityContextHolder.getContext().setAuthentication(null);
    119     }
    120
    121     public void testInclusionPermittedWhenDomainObjectIsNull()
    122         throws JspException {
    123         aclTag.setHasPermission(new Integer(SimpleAclEntry.READ).toString());
    124         aclTag.setDomainObject(null);
    125         assertEquals(Tag.EVAL_BODY_INCLUDE, aclTag.doStartTag());
    126     }
    127
    128     public void testJspExceptionThrownIfHasPermissionNotValidFormat()
    129         throws JspException {
    130         Authentication auth = new TestingAuthenticationToken("john", "crow",
    131                 new GrantedAuthority[] {});
    132         SecurityContextHolder.getContext().setAuthentication(auth);
    133
    134         aclTag.setHasPermission("0,5, 6"); // shouldn't be any space
    135
    136         try {
    137             aclTag.doStartTag();
    138             fail("Should have thrown JspException");
    139         } catch (JspException expected) {
    140             assertTrue(true);
    141         }
    142
    143         SecurityContextHolder.getContext().setAuthentication(null);
    144     }
    145
    146     public void testOperationWhenPrincipalHoldsPermissionOfMultipleList()
    147         throws JspException {
    148         Authentication auth = new TestingAuthenticationToken("marissa",
    149                 "koala", new GrantedAuthority[] {});
    150         SecurityContextHolder.getContext().setAuthentication(auth);
    151
    152         aclTag.setHasPermission(new Integer(SimpleAclEntry.ADMINISTRATION)
    153             + "," + new Integer(SimpleAclEntry.READ));
    154         aclTag.setDomainObject("object1");
    155         assertEquals(Tag.EVAL_BODY_INCLUDE, aclTag.doStartTag());
    156
    157         SecurityContextHolder.getContext().setAuthentication(null);
    158     }
    159
    160     public void testOperationWhenPrincipalHoldsPermissionOfSingleList()
    161         throws JspException {
    162         Authentication auth = new TestingAuthenticationToken("marissa",
    163                 "koala", new GrantedAuthority[] {});
    164         SecurityContextHolder.getContext().setAuthentication(auth);
    165
    166         aclTag.setHasPermission(new Integer(SimpleAclEntry.READ).toString());
    167         aclTag.setDomainObject("object1");
    168         assertEquals(Tag.EVAL_BODY_INCLUDE, aclTag.doStartTag());
    169
    170         SecurityContextHolder.getContext().setAuthentication(null);
    171     }
    172
    173     //~ Inner Classes ==========================================================
    174
    175     private class MockAclEntry implements AclEntry {
    176         // just so AclTag iterates some different types of AclEntrys
    177     }
    178
    179     private class MyAclTag extends AclTag {
    180         protected ApplicationContext getContext(PageContext pageContext) {
    181             ConfigurableApplicationContext context = MockApplicationContext
    182                 .getContext();
    183
    184             // Create an AclManager
    185             AclManager aclManager = new MockAclManager("object1", "marissa",
    186                     new AclEntry[] {new MockAclEntry(), new SimpleAclEntry(
    187                             "marissa", new MockAclObjectIdentity(), null,
    188                             SimpleAclEntry.ADMINISTRATION), new SimpleAclEntry(
    189                             "marissa", new MockAclObjectIdentity(), null,
    190                             SimpleAclEntry.READ)});
    191
    192             // Register the AclManager into our ApplicationContext
    193             context.getBeanFactory().registerSingleton("aclManager", aclManager);
    194
    195             return context;
    196         }
    197     }
    198 }