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
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
32
33
34
35
36 public class AuthorizeTagCustomGrantedAuthorityTests extends TestCase {
37
38
39 private final AuthorizeTag authorizeTag = new AuthorizeTag();
40 private TestingAuthenticationToken currentUser;
41
42
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
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 }