001: /* Copyright 2004 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 java.util.List;
019: import java.util.Vector;
020:
021: import junit.framework.TestCase;
022:
023: import org.acegisecurity.AccessDeniedException;
024: import org.acegisecurity.ConfigAttributeDefinition;
025: import org.acegisecurity.GrantedAuthority;
026: import org.acegisecurity.GrantedAuthorityImpl;
027: import org.acegisecurity.SecurityConfig;
028: import org.acegisecurity.providers.TestingAuthenticationToken;
029:
030: /**
031: * Tests {@link ConsensusBased}.
032: *
033: * @author Ben Alex
034: * @version $Id: ConsensusBasedTests.java 1144 2005-11-30 01:23:36Z benalex $
035: */
036: public class ConsensusBasedTests extends TestCase {
037: //~ Constructors ===========================================================
038:
039: public ConsensusBasedTests() {
040: super ();
041: }
042:
043: public ConsensusBasedTests(String arg0) {
044: super (arg0);
045: }
046:
047: //~ Methods ================================================================
048:
049: public final void setUp() throws Exception {
050: super .setUp();
051: }
052:
053: public static void main(String[] args) {
054: junit.textui.TestRunner.run(ConsensusBasedTests.class);
055: }
056:
057: public void testOneAffirmativeVoteOneDenyVoteOneAbstainVoteDeniesAccessWithoutDefault()
058: throws Exception {
059: TestingAuthenticationToken auth = makeTestToken();
060: ConsensusBased mgr = makeDecisionManager();
061: mgr.setAllowIfEqualGrantedDeniedDecisions(false);
062: assertTrue(!mgr.isAllowIfEqualGrantedDeniedDecisions()); // check changed
063:
064: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
065: config.addConfigAttribute(new SecurityConfig("ROLE_1")); // grant
066: config.addConfigAttribute(new SecurityConfig("DENY_FOR_SURE")); // deny
067:
068: try {
069: mgr.decide(auth, new Object(), config);
070: fail("Should have thrown AccessDeniedException");
071: } catch (AccessDeniedException expected) {
072: assertTrue(true);
073: }
074: }
075:
076: public void testOneAffirmativeVoteOneDenyVoteOneAbstainVoteGrantsAccessWithDefault()
077: throws Exception {
078: TestingAuthenticationToken auth = makeTestToken();
079: ConsensusBased mgr = makeDecisionManager();
080:
081: assertTrue(mgr.isAllowIfEqualGrantedDeniedDecisions()); // check default
082:
083: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
084: config.addConfigAttribute(new SecurityConfig("ROLE_1")); // grant
085: config.addConfigAttribute(new SecurityConfig("DENY_FOR_SURE")); // deny
086:
087: mgr.decide(auth, new Object(), config);
088: assertTrue(true);
089: }
090:
091: public void testOneAffirmativeVoteTwoAbstainVotesGrantsAccess()
092: throws Exception {
093: TestingAuthenticationToken auth = makeTestToken();
094: ConsensusBased mgr = makeDecisionManager();
095:
096: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
097: config.addConfigAttribute(new SecurityConfig("ROLE_2")); // grant
098:
099: mgr.decide(auth, new Object(), config);
100: assertTrue(true);
101: }
102:
103: public void testOneDenyVoteTwoAbstainVotesDeniesAccess()
104: throws Exception {
105: TestingAuthenticationToken auth = makeTestToken();
106: ConsensusBased mgr = makeDecisionManager();
107:
108: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
109: config.addConfigAttribute(new SecurityConfig(
110: "ROLE_WE_DO_NOT_HAVE")); // deny
111:
112: try {
113: mgr.decide(auth, new Object(), config);
114: fail("Should have thrown AccessDeniedException");
115: } catch (AccessDeniedException expected) {
116: assertTrue(true);
117: }
118: }
119:
120: public void testThreeAbstainVotesDeniesAccessWithDefault()
121: throws Exception {
122: TestingAuthenticationToken auth = makeTestToken();
123: ConsensusBased mgr = makeDecisionManager();
124:
125: assertTrue(!mgr.isAllowIfAllAbstainDecisions()); // check default
126:
127: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
128: config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
129:
130: try {
131: mgr.decide(auth, new Object(), config);
132: fail("Should have thrown AccessDeniedException");
133: } catch (AccessDeniedException expected) {
134: assertTrue(true);
135: }
136: }
137:
138: public void testThreeAbstainVotesGrantsAccessWithoutDefault()
139: throws Exception {
140: TestingAuthenticationToken auth = makeTestToken();
141: ConsensusBased mgr = makeDecisionManager();
142: mgr.setAllowIfAllAbstainDecisions(true);
143: assertTrue(mgr.isAllowIfAllAbstainDecisions()); // check changed
144:
145: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
146: config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
147:
148: mgr.decide(auth, new Object(), config);
149: assertTrue(true);
150: }
151:
152: public void testTwoAffirmativeVotesTwoAbstainVotesGrantsAccess()
153: throws Exception {
154: TestingAuthenticationToken auth = makeTestToken();
155: ConsensusBased mgr = makeDecisionManager();
156:
157: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
158: config.addConfigAttribute(new SecurityConfig("ROLE_1")); // grant
159: config.addConfigAttribute(new SecurityConfig("ROLE_2")); // grant
160:
161: mgr.decide(auth, new Object(), config);
162: assertTrue(true);
163: }
164:
165: private ConsensusBased makeDecisionManager() {
166: ConsensusBased decisionManager = new ConsensusBased();
167: RoleVoter roleVoter = new RoleVoter();
168: DenyVoter denyForSureVoter = new DenyVoter();
169: DenyAgainVoter denyAgainForSureVoter = new DenyAgainVoter();
170: List voters = new Vector();
171: voters.add(roleVoter);
172: voters.add(denyForSureVoter);
173: voters.add(denyAgainForSureVoter);
174: decisionManager.setDecisionVoters(voters);
175:
176: return decisionManager;
177: }
178:
179: private TestingAuthenticationToken makeTestToken() {
180: return new TestingAuthenticationToken("somebody", "password",
181: new GrantedAuthority[] {
182: new GrantedAuthorityImpl("ROLE_1"),
183: new GrantedAuthorityImpl("ROLE_2") });
184: }
185: }
|