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.Authentication;
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.UsernamePasswordAuthenticationToken;
027: import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
028: import org.acegisecurity.providers.rememberme.RememberMeAuthenticationToken;
029:
030: /**
031: * Tests {@link AuthenticatedVoter}.
032: *
033: * @author Ben Alex
034: * @version $Id: AuthenticatedVoterTests.java 1496 2006-05-23 13:38:33Z benalex $
035: */
036: public class AuthenticatedVoterTests extends TestCase {
037: //~ Constructors ===================================================================================================
038:
039: public AuthenticatedVoterTests() {
040: super ();
041: }
042:
043: public AuthenticatedVoterTests(String arg0) {
044: super (arg0);
045: }
046:
047: //~ Methods ========================================================================================================
048:
049: private Authentication createAnonymous() {
050: return new AnonymousAuthenticationToken("ignored", "ignored",
051: new GrantedAuthority[] { new GrantedAuthorityImpl(
052: "ignored") });
053: }
054:
055: private Authentication createFullyAuthenticated() {
056: return new UsernamePasswordAuthenticationToken("ignored",
057: "ignored",
058: new GrantedAuthority[] { new GrantedAuthorityImpl(
059: "ignored") });
060: }
061:
062: private Authentication createRememberMe() {
063: return new RememberMeAuthenticationToken("ignored", "ignored",
064: new GrantedAuthority[] { new GrantedAuthorityImpl(
065: "ignored") });
066: }
067:
068: public static void main(String[] args) {
069: junit.textui.TestRunner.run(AuthenticatedVoterTests.class);
070: }
071:
072: public final void setUp() throws Exception {
073: super .setUp();
074: }
075:
076: public void testAnonymousWorks() {
077: AuthenticatedVoter voter = new AuthenticatedVoter();
078: ConfigAttributeDefinition def = new ConfigAttributeDefinition();
079: def.addConfigAttribute(new SecurityConfig(
080: AuthenticatedVoter.IS_AUTHENTICATED_ANONYMOUSLY));
081: assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(
082: createAnonymous(), null, def));
083: assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(
084: createRememberMe(), null, def));
085: assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(
086: createFullyAuthenticated(), null, def));
087: }
088:
089: public void testFullyWorks() {
090: AuthenticatedVoter voter = new AuthenticatedVoter();
091: ConfigAttributeDefinition def = new ConfigAttributeDefinition();
092: def.addConfigAttribute(new SecurityConfig(
093: AuthenticatedVoter.IS_AUTHENTICATED_FULLY));
094: assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(
095: createAnonymous(), null, def));
096: assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(
097: createRememberMe(), null, def));
098: assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(
099: createFullyAuthenticated(), null, def));
100: }
101:
102: public void testRememberMeWorks() {
103: AuthenticatedVoter voter = new AuthenticatedVoter();
104: ConfigAttributeDefinition def = new ConfigAttributeDefinition();
105: def.addConfigAttribute(new SecurityConfig(
106: AuthenticatedVoter.IS_AUTHENTICATED_REMEMBERED));
107: assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(
108: createAnonymous(), null, def));
109: assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(
110: createRememberMe(), null, def));
111: assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(
112: createFullyAuthenticated(), null, def));
113: }
114:
115: public void testSetterRejectsNull() {
116: AuthenticatedVoter voter = new AuthenticatedVoter();
117:
118: try {
119: voter.setAuthenticationTrustResolver(null);
120: fail("Expected IAE");
121: } catch (IllegalArgumentException expected) {
122: assertTrue(true);
123: }
124: }
125:
126: public void testSupports() {
127: AuthenticatedVoter voter = new AuthenticatedVoter();
128: assertTrue(voter.supports(String.class));
129: assertTrue(voter.supports(new SecurityConfig(
130: AuthenticatedVoter.IS_AUTHENTICATED_ANONYMOUSLY)));
131: assertTrue(voter.supports(new SecurityConfig(
132: AuthenticatedVoter.IS_AUTHENTICATED_FULLY)));
133: assertTrue(voter.supports(new SecurityConfig(
134: AuthenticatedVoter.IS_AUTHENTICATED_REMEMBERED)));
135: assertFalse(voter.supports(new SecurityConfig("FOO")));
136: }
137: }
|