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