1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests {@link AffirmativeBased}.
34   *
35   * @author Ben Alex
36   * @version $Id: AffirmativeBasedTests.java 1496 2006-05-23 13:38:33Z benalex $
37   */
38  public class AffirmativeBasedTests extends TestCase {
39      //~ Constructors ===================================================================================================
40  
41      public AffirmativeBasedTests() {
42          super();
43      }
44  
45      public AffirmativeBasedTests(String arg0) {
46          super(arg0);
47      }
48  
49      //~ Methods ========================================================================================================
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")); // grant
85          config.addConfigAttribute(new SecurityConfig("DENY_FOR_SURE")); // deny
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")); // grant
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")); // 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 testThreeAbstainVotesDeniesAccessWithDefault()
120         throws Exception {
121         TestingAuthenticationToken auth = makeTestToken();
122         AffirmativeBased mgr = makeDecisionManager();
123 
124         assertTrue(!mgr.isAllowIfAllAbstainDecisions()); // check default
125 
126         ConfigAttributeDefinition config = new ConfigAttributeDefinition();
127         config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
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()); // check changed
143 
144         ConfigAttributeDefinition config = new ConfigAttributeDefinition();
145         config.addConfigAttribute(new SecurityConfig("IGNORED_BY_ALL")); // abstain
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")); // grant
158         config.addConfigAttribute(new SecurityConfig("ROLE_2")); // grant
159 
160         mgr.decide(auth, new Object(), config);
161         assertTrue(true);
162     }
163 }