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 AffirmativeBased}.
033: *
034: * @author Ben Alex
035: * @version $Id: AffirmativeBasedTests.java 1496 2006-05-23 13:38:33Z benalex $
036: */
037: public class AffirmativeBasedTests extends TestCase {
038: //~ Constructors ===================================================================================================
039:
040: public AffirmativeBasedTests() {
041: super ();
042: }
043:
044: public AffirmativeBasedTests(String arg0) {
045: super (arg0);
046: }
047:
048: //~ Methods ========================================================================================================
049:
050: public static void main(String[] args) {
051: junit.textui.TestRunner.run(AffirmativeBasedTests.class);
052: }
053:
054: private AffirmativeBased makeDecisionManager() {
055: AffirmativeBased decisionManager = new AffirmativeBased();
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 TestingAuthenticationToken makeTestToken() {
069: return new TestingAuthenticationToken("somebody", "password",
070: new GrantedAuthority[] {
071: new GrantedAuthorityImpl("ROLE_1"),
072: new GrantedAuthorityImpl("ROLE_2") });
073: }
074:
075: public final void setUp() throws Exception {
076: super .setUp();
077: }
078:
079: public void testOneAffirmativeVoteOneDenyVoteOneAbstainVoteGrantsAccess()
080: throws Exception {
081: TestingAuthenticationToken auth = makeTestToken();
082: AffirmativeBased mgr = makeDecisionManager();
083:
084: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
085: config.addConfigAttribute(new SecurityConfig("ROLE_1")); // grant
086: config.addConfigAttribute(new SecurityConfig("DENY_FOR_SURE")); // deny
087:
088: mgr.decide(auth, new Object(), config);
089: assertTrue(true);
090: }
091:
092: public void testOneAffirmativeVoteTwoAbstainVotesGrantsAccess()
093: throws Exception {
094: TestingAuthenticationToken auth = makeTestToken();
095: AffirmativeBased mgr = makeDecisionManager();
096:
097: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
098: config.addConfigAttribute(new SecurityConfig("ROLE_2")); // grant
099:
100: mgr.decide(auth, new Object(), config);
101: assertTrue(true);
102: }
103:
104: public void testOneDenyVoteTwoAbstainVotesDeniesAccess()
105: throws Exception {
106: TestingAuthenticationToken auth = makeTestToken();
107: AffirmativeBased mgr = makeDecisionManager();
108:
109: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
110: config.addConfigAttribute(new SecurityConfig(
111: "ROLE_WE_DO_NOT_HAVE")); // deny
112:
113: try {
114: mgr.decide(auth, new Object(), config);
115: fail("Should have thrown AccessDeniedException");
116: } catch (AccessDeniedException expected) {
117: assertTrue(true);
118: }
119: }
120:
121: public void testThreeAbstainVotesDeniesAccessWithDefault()
122: throws Exception {
123: TestingAuthenticationToken auth = makeTestToken();
124: AffirmativeBased mgr = makeDecisionManager();
125:
126: assertTrue(!mgr.isAllowIfAllAbstainDecisions()); // check default
127:
128: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
129: config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
130:
131: try {
132: mgr.decide(auth, new Object(), config);
133: fail("Should have thrown AccessDeniedException");
134: } catch (AccessDeniedException expected) {
135: assertTrue(true);
136: }
137: }
138:
139: public void testThreeAbstainVotesGrantsAccessWithoutDefault()
140: throws Exception {
141: TestingAuthenticationToken auth = makeTestToken();
142: AffirmativeBased mgr = makeDecisionManager();
143: mgr.setAllowIfAllAbstainDecisions(true);
144: assertTrue(mgr.isAllowIfAllAbstainDecisions()); // check changed
145:
146: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
147: config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
148:
149: mgr.decide(auth, new Object(), config);
150: assertTrue(true);
151: }
152:
153: public void testTwoAffirmativeVotesTwoAbstainVotesGrantsAccess()
154: throws Exception {
155: TestingAuthenticationToken auth = makeTestToken();
156: AffirmativeBased mgr = makeDecisionManager();
157:
158: ConfigAttributeDefinition config = new ConfigAttributeDefinition();
159: config.addConfigAttribute(new SecurityConfig("ROLE_1")); // grant
160: config.addConfigAttribute(new SecurityConfig("ROLE_2")); // grant
161:
162: mgr.decide(auth, new Object(), config);
163: assertTrue(true);
164: }
165: }
|