001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.vote;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.AccessDeniedException;
021: import org.acegisecurity.ConfigAttributeDefinition;
022: import org.acegisecurity.GrantedAuthority;
023: import org.acegisecurity.GrantedAuthorityImpl;
024: import org.acegisecurity.SecurityConfig;
025:
026: import org.acegisecurity.providers.TestingAuthenticationToken;
027:
028: import java.util.List;
029: import java.util.Vector;
030:
031: /**
032: * Tests {@link UnanimousBased}.
033: *
034: * @author Ben Alex
035: * @version $Id: UnanimousBasedTests.java 1496 2006-05-23 13:38:33Z benalex $
036: */
037: public class UnanimousBasedTests extends TestCase {
038: //~ Constructors ===================================================================================================
039:
040: public UnanimousBasedTests() {
041: super ();
042: }
043:
044: public UnanimousBasedTests(String arg0) {
045: super (arg0);
046: }
047:
048: //~ Methods ========================================================================================================
049:
050: public static void main(String[] args) {
051: junit.textui.TestRunner.run(UnanimousBasedTests.class);
052: }
053:
054: private UnanimousBased makeDecisionManager() {
055: UnanimousBased decisionManager = new UnanimousBased();
056: RoleVoter roleVoter = new RoleVoter();
057: DenyVoter denyForSureVoter = new DenyVoter();
058: DenyAgainVoter denyAgainForSureVoter = new DenyAgainVoter();
059: List voters = new Vector();
060: voters.add(roleVoter);
061: voters.add(denyForSureVoter);
062: voters.add(denyAgainForSureVoter);
063: decisionManager.setDecisionVoters(voters);
064:
065: return decisionManager;
066: }
067:
068: private UnanimousBased makeDecisionManagerWithFooBarPrefix() {
069: UnanimousBased decisionManager = new UnanimousBased();
070: RoleVoter roleVoter = new RoleVoter();
071: roleVoter.setRolePrefix("FOOBAR_");
072:
073: DenyVoter denyForSureVoter = new DenyVoter();
074: DenyAgainVoter denyAgainForSureVoter = new DenyAgainVoter();
075: List voters = new Vector();
076: voters.add(roleVoter);
077: voters.add(denyForSureVoter);
078: voters.add(denyAgainForSureVoter);
079: decisionManager.setDecisionVoters(voters);
080:
081: return decisionManager;
082: }
083:
084: private TestingAuthenticationToken makeTestToken() {
085: return new TestingAuthenticationToken("somebody", "password",
086: new GrantedAuthority[] {
087: new GrantedAuthorityImpl("ROLE_1"),
088: new GrantedAuthorityImpl("ROLE_2") });
089: }
090:
091: private TestingAuthenticationToken makeTestTokenWithFooBarPrefix() {
092: return new TestingAuthenticationToken("somebody", "password",
093: new GrantedAuthority[] {
094: new GrantedAuthorityImpl("FOOBAR_1"),
095: new GrantedAuthorityImpl("FOOBAR_2") });
096: }
097:
098: public final void setUp() throws Exception {
099: super .setUp();
100: }
101:
102: public void testOneAffirmativeVoteOneDenyVoteOneAbstainVoteDeniesAccess()
103: throws Exception {
104: TestingAuthenticationToken auth = makeTestToken();
105: UnanimousBased mgr = makeDecisionManager();
106:
107: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
108: config.addConfigAttribute(new SecurityConfig("ROLE_1")); // grant
109: config.addConfigAttribute(new SecurityConfig("DENY_FOR_SURE")); // deny
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 testOneAffirmativeVoteTwoAbstainVotesGrantsAccess()
120: throws Exception {
121: TestingAuthenticationToken auth = makeTestToken();
122: UnanimousBased mgr = makeDecisionManager();
123:
124: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
125: config.addConfigAttribute(new SecurityConfig("ROLE_2")); // grant
126:
127: mgr.decide(auth, new Object(), config);
128: assertTrue(true);
129: }
130:
131: public void testOneDenyVoteTwoAbstainVotesDeniesAccess()
132: throws Exception {
133: TestingAuthenticationToken auth = makeTestToken();
134: UnanimousBased mgr = makeDecisionManager();
135:
136: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
137: config.addConfigAttribute(new SecurityConfig(
138: "ROLE_WE_DO_NOT_HAVE")); // deny
139:
140: try {
141: mgr.decide(auth, new Object(), config);
142: fail("Should have thrown AccessDeniedException");
143: } catch (AccessDeniedException expected) {
144: assertTrue(true);
145: }
146: }
147:
148: public void testRoleVoterPrefixObserved() throws Exception {
149: TestingAuthenticationToken auth = makeTestTokenWithFooBarPrefix();
150: UnanimousBased mgr = makeDecisionManagerWithFooBarPrefix();
151:
152: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
153: config.addConfigAttribute(new SecurityConfig("FOOBAR_1")); // grant
154: config.addConfigAttribute(new SecurityConfig("FOOBAR_2")); // grant
155:
156: mgr.decide(auth, new Object(), config);
157: assertTrue(true);
158: }
159:
160: public void testThreeAbstainVotesDeniesAccessWithDefault()
161: throws Exception {
162: TestingAuthenticationToken auth = makeTestToken();
163: UnanimousBased mgr = makeDecisionManager();
164:
165: assertTrue(!mgr.isAllowIfAllAbstainDecisions()); // check default
166:
167: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
168: config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
169:
170: try {
171: mgr.decide(auth, new Object(), config);
172: fail("Should have thrown AccessDeniedException");
173: } catch (AccessDeniedException expected) {
174: assertTrue(true);
175: }
176: }
177:
178: public void testThreeAbstainVotesGrantsAccessWithoutDefault()
179: throws Exception {
180: TestingAuthenticationToken auth = makeTestToken();
181: UnanimousBased mgr = makeDecisionManager();
182: mgr.setAllowIfAllAbstainDecisions(true);
183: assertTrue(mgr.isAllowIfAllAbstainDecisions()); // check changed
184:
185: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
186: config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
187:
188: mgr.decide(auth, new Object(), config);
189: assertTrue(true);
190: }
191:
192: public void testTwoAffirmativeVotesTwoAbstainVotesGrantsAccess()
193: throws Exception {
194: TestingAuthenticationToken auth = makeTestToken();
195: UnanimousBased mgr = makeDecisionManager();
196:
197: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
198: config.addConfigAttribute(new SecurityConfig("ROLE_1")); // grant
199: config.addConfigAttribute(new SecurityConfig("ROLE_2")); // grant
200:
201: mgr.decide(auth, new Object(), config);
202: assertTrue(true);
203: }
204: }
|