1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.taglibs.velocity;
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
28
29
30
31 public class AuthzImplAuthorizeTagTest extends TestCase {
32
33
34 private Authz authz = new AuthzImpl();
35 private TestingAuthenticationToken currentUser;
36
37
38
39 protected void setUp() throws Exception {
40 super.setUp();
41
42 currentUser = new TestingAuthenticationToken("abc", "123",
43 new GrantedAuthority[] {
44 new GrantedAuthorityImpl("ROLE_SUPERVISOR"), new GrantedAuthorityImpl("ROLE_TELLER"),
45 });
46
47 SecurityContextHolder.getContext().setAuthentication(currentUser);
48 }
49
50 protected void tearDown() throws Exception {
51 SecurityContextHolder.clearContext();
52 }
53
54 public void testAlwaysReturnsUnauthorizedIfNoUserFound() {
55 SecurityContextHolder.getContext().setAuthentication(null);
56
57
58 assertFalse(authz.allGranted("ROLE_TELLER"));
59 }
60
61 public void testDefaultsToNotOutputtingBodyWhenNoRequiredAuthorities() {
62
63 assertFalse(authz.allGranted(""));
64 assertFalse(authz.anyGranted(""));
65 assertFalse(authz.noneGranted(""));
66 }
67
68 public void testOutputsBodyIfOneRolePresent() {
69
70 assertTrue(authz.anyGranted("ROLE_TELLER"));
71 }
72
73 public void testOutputsBodyWhenAllGranted() {
74
75 assertTrue(authz.allGranted("ROLE_SUPERVISOR,ROLE_TELLER"));
76 }
77
78 public void testOutputsBodyWhenNotGrantedSatisfied() {
79
80 assertTrue(authz.noneGranted("ROLE_BANKER"));
81 }
82
83 public void testPreventsBodyOutputIfNoSecureContext() {
84 SecurityContextHolder.getContext().setAuthentication(null);
85
86
87 assertFalse(authz.anyGranted("ROLE_BANKER"));
88 }
89
90 public void testSkipsBodyIfNoAnyRolePresent() {
91
92 assertFalse(authz.anyGranted("ROLE_BANKER"));
93 }
94
95 public void testSkipsBodyWhenMissingAnAllGranted() {
96
97 assertFalse(authz.allGranted("ROLE_SUPERVISOR,ROLE_TELLER,ROLE_BANKER"));
98 }
99
100 public void testSkipsBodyWhenNotGrantedUnsatisfied() {
101
102 assertFalse(authz.noneGranted("ROLE_TELLER"));
103 }
104 }