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.Authentication;
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.UsernamePasswordAuthenticationToken;
27  import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
28  import org.acegisecurity.providers.rememberme.RememberMeAuthenticationToken;
29  
30  
31  /**
32   * Tests {@link AuthenticatedVoter}.
33   *
34   * @author Ben Alex
35   * @version $Id: AuthenticatedVoterTests.java 1496 2006-05-23 13:38:33Z benalex $
36   */
37  public class AuthenticatedVoterTests extends TestCase {
38      //~ Constructors ===================================================================================================
39  
40      public AuthenticatedVoterTests() {
41          super();
42      }
43  
44      public AuthenticatedVoterTests(String arg0) {
45          super(arg0);
46      }
47  
48      //~ Methods ========================================================================================================
49  
50      private Authentication createAnonymous() {
51          return new AnonymousAuthenticationToken("ignored", "ignored",
52              new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")});
53      }
54  
55      private Authentication createFullyAuthenticated() {
56          return new UsernamePasswordAuthenticationToken("ignored", "ignored",
57              new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")});
58      }
59  
60      private Authentication createRememberMe() {
61          return new RememberMeAuthenticationToken("ignored", "ignored",
62              new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")});
63      }
64  
65      public static void main(String[] args) {
66          junit.textui.TestRunner.run(AuthenticatedVoterTests.class);
67      }
68  
69      public final void setUp() throws Exception {
70          super.setUp();
71      }
72  
73      public void testAnonymousWorks() {
74          AuthenticatedVoter voter = new AuthenticatedVoter();
75          ConfigAttributeDefinition def = new ConfigAttributeDefinition();
76          def.addConfigAttribute(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_ANONYMOUSLY));
77          assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createAnonymous(), null, def));
78          assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createRememberMe(), null, def));
79          assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createFullyAuthenticated(), null, def));
80      }
81  
82      public void testFullyWorks() {
83          AuthenticatedVoter voter = new AuthenticatedVoter();
84          ConfigAttributeDefinition def = new ConfigAttributeDefinition();
85          def.addConfigAttribute(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_FULLY));
86          assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(createAnonymous(), null, def));
87          assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(createRememberMe(), null, def));
88          assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createFullyAuthenticated(), null, def));
89      }
90  
91      public void testRememberMeWorks() {
92          AuthenticatedVoter voter = new AuthenticatedVoter();
93          ConfigAttributeDefinition def = new ConfigAttributeDefinition();
94          def.addConfigAttribute(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_REMEMBERED));
95          assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(createAnonymous(), null, def));
96          assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createRememberMe(), null, def));
97          assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createFullyAuthenticated(), null, def));
98      }
99  
100     public void testSetterRejectsNull() {
101         AuthenticatedVoter voter = new AuthenticatedVoter();
102 
103         try {
104             voter.setAuthenticationTrustResolver(null);
105             fail("Expected IAE");
106         } catch (IllegalArgumentException expected) {
107             assertTrue(true);
108         }
109     }
110 
111     public void testSupports() {
112         AuthenticatedVoter voter = new AuthenticatedVoter();
113         assertTrue(voter.supports(String.class));
114         assertTrue(voter.supports(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_ANONYMOUSLY)));
115         assertTrue(voter.supports(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_FULLY)));
116         assertTrue(voter.supports(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_REMEMBERED)));
117         assertFalse(voter.supports(new SecurityConfig("FOO")));
118     }
119 }