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.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
37
38 private final AuthorizeTag authorizeTag = new AuthorizeTag();
39 private TestingAuthenticationToken currentUser;
40
41
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
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 }