1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.vote;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.AccessDeniedException;
21 import org.acegisecurity.ConfigAttributeDefinition;
22 import org.acegisecurity.GrantedAuthority;
23 import org.acegisecurity.GrantedAuthorityImpl;
24 import org.acegisecurity.SecurityConfig;
25
26 import org.acegisecurity.providers.TestingAuthenticationToken;
27
28 import java.util.List;
29 import java.util.Vector;
30
31
32
33
34
35
36
37
38 public class AffirmativeBasedTests extends TestCase {
39
40
41 public AffirmativeBasedTests() {
42 super();
43 }
44
45 public AffirmativeBasedTests(String arg0) {
46 super(arg0);
47 }
48
49
50
51 public static void main(String[] args) {
52 junit.textui.TestRunner.run(AffirmativeBasedTests.class);
53 }
54
55 private AffirmativeBased makeDecisionManager() {
56 AffirmativeBased decisionManager = new AffirmativeBased();
57 RoleVoter roleVoter = new RoleVoter();
58 DenyVoter denyForSureVoter = new DenyVoter();
59 DenyAgainVoter denyAgainForSureVoter = new DenyAgainVoter();
60 List voters = new Vector();
61 voters.add(roleVoter);
62 voters.add(denyForSureVoter);
63 voters.add(denyAgainForSureVoter);
64 decisionManager.setDecisionVoters(voters);
65
66 return decisionManager;
67 }
68
69 private TestingAuthenticationToken makeTestToken() {
70 return new TestingAuthenticationToken("somebody", "password",
71 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_1"), new GrantedAuthorityImpl("ROLE_2")});
72 }
73
74 public final void setUp() throws Exception {
75 super.setUp();
76 }
77
78 public void testOneAffirmativeVoteOneDenyVoteOneAbstainVoteGrantsAccess()
79 throws Exception {
80 TestingAuthenticationToken auth = makeTestToken();
81 AffirmativeBased mgr = makeDecisionManager();
82
83 ConfigAttributeDefinition config = new ConfigAttributeDefinition();
84 config.addConfigAttribute(new SecurityConfig("ROLE_1"));
85 config.addConfigAttribute(new SecurityConfig("DENY_FOR_SURE"));
86
87 mgr.decide(auth, new Object(), config);
88 assertTrue(true);
89 }
90
91 public void testOneAffirmativeVoteTwoAbstainVotesGrantsAccess()
92 throws Exception {
93 TestingAuthenticationToken auth = makeTestToken();
94 AffirmativeBased mgr = makeDecisionManager();
95
96 ConfigAttributeDefinition config = new ConfigAttributeDefinition();
97 config.addConfigAttribute(new SecurityConfig("ROLE_2"));
98
99 mgr.decide(auth, new Object(), config);
100 assertTrue(true);
101 }
102
103 public void testOneDenyVoteTwoAbstainVotesDeniesAccess()
104 throws Exception {
105 TestingAuthenticationToken auth = makeTestToken();
106 AffirmativeBased mgr = makeDecisionManager();
107
108 ConfigAttributeDefinition config = new ConfigAttributeDefinition();
109 config.addConfigAttribute(new SecurityConfig("ROLE_WE_DO_NOT_HAVE"));
110
111 try {
112 mgr.decide(auth, new Object(), config);
113 fail("Should have thrown AccessDeniedException");
114 } catch (AccessDeniedException expected) {
115 assertTrue(true);
116 }
117 }
118
119 public void testThreeAbstainVotesDeniesAccessWithDefault()
120 throws Exception {
121 TestingAuthenticationToken auth = makeTestToken();
122 AffirmativeBased mgr = makeDecisionManager();
123
124 assertTrue(!mgr.isAllowIfAllAbstainDecisions());
125
126 ConfigAttributeDefinition config = new ConfigAttributeDefinition();
127 config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL"));
128
129 try {
130 mgr.decide(auth, new Object(), config);
131 fail("Should have thrown AccessDeniedException");
132 } catch (AccessDeniedException expected) {
133 assertTrue(true);
134 }
135 }
136
137 public void testThreeAbstainVotesGrantsAccessWithoutDefault()
138 throws Exception {
139 TestingAuthenticationToken auth = makeTestToken();
140 AffirmativeBased mgr = makeDecisionManager();
141 mgr.setAllowIfAllAbstainDecisions(true);
142 assertTrue(mgr.isAllowIfAllAbstainDecisions());
143
144 ConfigAttributeDefinition config = new ConfigAttributeDefinition();
145 config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL"));
146
147 mgr.decide(auth, new Object(), config);
148 assertTrue(true);
149 }
150
151 public void testTwoAffirmativeVotesTwoAbstainVotesGrantsAccess()
152 throws Exception {
153 TestingAuthenticationToken auth = makeTestToken();
154 AffirmativeBased mgr = makeDecisionManager();
155
156 ConfigAttributeDefinition config = new ConfigAttributeDefinition();
157 config.addConfigAttribute(new SecurityConfig("ROLE_1"));
158 config.addConfigAttribute(new SecurityConfig("ROLE_2"));
159
160 mgr.decide(auth, new Object(), config);
161 assertTrue(true);
162 }
163 }