    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.GrantedAuthority;
    21  import org.acegisecurity.context.SecurityContextHolder;
    22  import org.acegisecurity.context.SecurityContextImpl;
    23  import org.acegisecurity.providers.TestingAuthenticationToken;
    24
    25  import javax.servlet.jsp.JspException;
    26  import javax.servlet.jsp.tagext.Tag;
    27
    28
    29  /***
    30   * DOCUMENT ME!
    31   *
    32   * @author Francois Beausoleil
    33   * @version $Id: AuthorizeTagCustomGrantedAuthorityTests.java,v 1.4 2005/11/17 00:55:51 benalex Exp $
    34   */
    35  public class AuthorizeTagCustomGrantedAuthorityTests extends TestCase {
    36      //~ Instance fields ========================================================
    37
    38      private final AuthorizeTag authorizeTag = new AuthorizeTag();
    39      private TestingAuthenticationToken currentUser;
    40
    41      //~ Methods ================================================================
    42
    43      public void testAllowsRequestWhenCustomAuthorityPresentsCorrectRole()
    44          throws JspException {
    45          authorizeTag.setIfAnyGranted("ROLE_TELLER");
    46          assertEquals("authorized - ROLE_TELLER in both sets",
    47              Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
    48      }
    49
    50      public void testRejectsRequestWhenCustomAuthorityReturnsNull()
    51          throws JspException {
    52          authorizeTag.setIfAnyGranted("ROLE_TELLER");
    53          SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
    54                  "abc", "123",
    55                  new GrantedAuthority[] {new CustomGrantedAuthority(null)}));
    56
    57          try {
    58              authorizeTag.doStartTag();
    59              fail("Failed to reject GrantedAuthority with NULL getAuthority()");
    60          } catch (IllegalArgumentException expected) {
    61              assertTrue("expected", true);
    62          }
    63      }
    64
    65      protected void setUp() throws Exception {
    66          super.setUp();
    67
    68          currentUser = new TestingAuthenticationToken("abc", "123",
    69                  new GrantedAuthority[] {new CustomGrantedAuthority(
    70                          "ROLE_TELLER")});
    71
    72          SecurityContextHolder.getContext().setAuthentication(currentUser);
    73      }
    74
    75      protected void tearDown() throws Exception {
    76          SecurityContextHolder.setContext(new SecurityContextImpl());
    77      }
    78
    79      //~ Inner Classes ==========================================================
    80
    81      private static class CustomGrantedAuthority implements GrantedAuthority {
    82          private final String authority;
    83
    84          public CustomGrantedAuthority(String authority) {
    85              this.authority = authority;
    86          }
    87
    88          public String getAuthority() {
    89              return authority;
    90          }
    91      }
    92  }