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