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.adapters;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.Authentication;
021: import org.acegisecurity.BadCredentialsException;
022: import org.acegisecurity.GrantedAuthority;
023: import org.acegisecurity.GrantedAuthorityImpl;
024:
025: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
026:
027: import java.util.Arrays;
028:
029: /**
030: * Tests {@link AuthByAdapterProvider}
031: *
032: * @author Ben Alex
033: * @version $Id: AuthByAdapterTests.java 1496 2006-05-23 13:38:33Z benalex $
034: */
035: public class AuthByAdapterTests extends TestCase {
036: //~ Constructors ===================================================================================================
037:
038: public AuthByAdapterTests() {
039: super ();
040: }
041:
042: public AuthByAdapterTests(String arg0) {
043: super (arg0);
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: public static void main(String[] args) {
049: junit.textui.TestRunner.run(AuthByAdapterTests.class);
050: }
051:
052: public final void setUp() throws Exception {
053: super .setUp();
054: }
055:
056: public void testAuthByAdapterProviderCorrectAuthenticationOperation()
057: throws Exception {
058: AuthByAdapterProvider provider = new AuthByAdapterProvider();
059: provider.setKey("my_password");
060:
061: PrincipalAcegiUserToken token = new PrincipalAcegiUserToken(
062: "my_password", "Test", "Password",
063: new GrantedAuthority[] {
064: new GrantedAuthorityImpl("ROLE_ONE"),
065: new GrantedAuthorityImpl("ROLE_TWO") }, null);
066: assertTrue(provider.supports(token.getClass()));
067:
068: Authentication response = provider.authenticate(token);
069: assertTrue(true);
070:
071: assertEquals(token.getCredentials(), response.getCredentials());
072: assertEquals(token.getPrincipal(), response.getPrincipal());
073: assertTrue(Arrays.equals(token.getAuthorities(), response
074: .getAuthorities()));
075:
076: if (!response.getClass().equals(token.getClass())) {
077: fail("Should have returned same type of object it was given");
078: }
079:
080: PrincipalAcegiUserToken castResponse = (PrincipalAcegiUserToken) response;
081: assertEquals(token.getName(), castResponse.getName());
082: }
083:
084: public void testAuthByAdapterProviderNonAuthenticationMethods()
085: throws Exception {
086: AuthByAdapterProvider provider = new AuthByAdapterProvider();
087:
088: try {
089: provider.afterPropertiesSet();
090: fail("Should have thrown IllegalArgumentException as key not set");
091: } catch (IllegalArgumentException expected) {
092: assertTrue(true);
093: }
094:
095: provider.setKey("my_password");
096: provider.afterPropertiesSet();
097: assertTrue(true);
098:
099: assertEquals("my_password", provider.getKey());
100: }
101:
102: public void testAuthByAdapterProviderOnlyAcceptsAuthByAdapterImplementations()
103: throws Exception {
104: AuthByAdapterProvider provider = new AuthByAdapterProvider();
105: provider.setKey("my_password");
106:
107: // Should fail as UsernamePassword is not interface of AuthByAdapter
108: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
109: "Test", "Password");
110:
111: assertTrue(!provider.supports(token.getClass()));
112:
113: try {
114: provider.authenticate(token);
115: fail("Should have thrown ClassCastException (supports() false response was ignored)");
116: } catch (ClassCastException expected) {
117: assertTrue(true);
118: }
119: }
120:
121: public void testAuthByAdapterProviderRequiresCorrectKey()
122: throws Exception {
123: AuthByAdapterProvider provider = new AuthByAdapterProvider();
124: provider.setKey("my_password");
125:
126: // Should fail as PrincipalAcegiUserToken has different key
127: PrincipalAcegiUserToken token = new PrincipalAcegiUserToken(
128: "wrong_password", "Test", "Password", null, null);
129:
130: try {
131: provider.authenticate(token);
132: fail("Should have thrown BadCredentialsException");
133: } catch (BadCredentialsException expected) {
134: assertTrue(true);
135: }
136: }
137: }
|