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