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.GrantedAuthorityImpl;
22  import org.acegisecurity.context.SecurityContextHolder;
23  import org.acegisecurity.context.SecurityContextImpl;
24  import org.acegisecurity.providers.TestingAuthenticationToken;
25  
26  import org.springframework.mock.web.MockPageContext;
27  
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.tagext.Tag;
30  
31  
32  /***
33   * Test case to implement commons-el expression language expansion.
34   */
35  public class AuthorizeTagExpressionLanguageTests extends TestCase {
36      //~ Instance fields ========================================================
37  
38      private final AuthorizeTag authorizeTag = new AuthorizeTag();
39      private MockPageContext pageContext;
40      private TestingAuthenticationToken currentUser;
41  
42      //~ Methods ================================================================
43  
44      public void testAllGrantedUsesExpressionLanguageWhenExpressionIsEL()
45          throws JspException {
46          pageContext.setAttribute("authority", "ROLE_TELLER");
47          authorizeTag.setIfAllGranted("${authority}");
48  
49          assertEquals("allows body - authority var contains ROLE_TELLER",
50              Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
51      }
52  
53      public void testAnyGrantedUsesExpressionLanguageWhenExpressionIsEL()
54          throws JspException {
55          pageContext.setAttribute("authority", "ROLE_TELLER");
56          authorizeTag.setIfAnyGranted("${authority}");
57  
58          assertEquals("allows body - authority var contains ROLE_TELLER",
59              Tag.EVAL_BODY_INCLUDE, authorizeTag.doStartTag());
60      }
61  
62      public void testNotGrantedUsesExpressionLanguageWhenExpressionIsEL()
63          throws JspException {
64          pageContext.setAttribute("authority", "ROLE_TELLER");
65          authorizeTag.setIfNotGranted("${authority}");
66  
67          assertEquals("allows body - authority var contains ROLE_TELLER",
68              Tag.SKIP_BODY, authorizeTag.doStartTag());
69      }
70  
71      protected void setUp() throws Exception {
72          super.setUp();
73  
74          pageContext = new MockPageContext();
75          authorizeTag.setPageContext(pageContext);
76  
77          currentUser = new TestingAuthenticationToken("abc", "123",
78                  new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_TELLER"),});
79  
80          SecurityContextHolder.getContext().setAuthentication(currentUser);
81      }
82  
83      protected void tearDown() throws Exception {
84          SecurityContextHolder.setContext(new SecurityContextImpl());
85      }
86  }