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