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.runas;
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.TestingAuthenticationToken;
026: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
027:
028: /**
029: * Tests {@link RunAsImplAuthenticationProvider}.
030: */
031: public class RunAsImplAuthenticationProviderTests extends TestCase {
032: //~ Constructors ===================================================================================================
033:
034: public RunAsImplAuthenticationProviderTests() {
035: super ();
036: }
037:
038: public RunAsImplAuthenticationProviderTests(String arg0) {
039: super (arg0);
040: }
041:
042: //~ Methods ========================================================================================================
043:
044: public static void main(String[] args) {
045: junit.textui.TestRunner
046: .run(RunAsImplAuthenticationProviderTests.class);
047: }
048:
049: public final void setUp() throws Exception {
050: super .setUp();
051: }
052:
053: public void testAuthenticationFailDueToWrongKey() {
054: RunAsUserToken token = new RunAsUserToken("WRONG_PASSWORD",
055: "Test", "Password", new GrantedAuthority[] {
056: new GrantedAuthorityImpl("ROLE_ONE"),
057: new GrantedAuthorityImpl("ROLE_TWO") },
058: UsernamePasswordAuthenticationToken.class);
059: RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
060: provider.setKey("hello_world");
061:
062: try {
063: provider.authenticate(token);
064: fail("Should have thrown BadCredentialsException");
065: } catch (BadCredentialsException expected) {
066: assertTrue(true);
067: }
068: }
069:
070: public void testAuthenticationSuccess() {
071: RunAsUserToken token = new RunAsUserToken("my_password",
072: "Test", "Password", new GrantedAuthority[] {
073: new GrantedAuthorityImpl("ROLE_ONE"),
074: new GrantedAuthorityImpl("ROLE_TWO") },
075: UsernamePasswordAuthenticationToken.class);
076: RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
077: provider.setKey("my_password");
078:
079: Authentication result = provider.authenticate(token);
080:
081: if (!(result instanceof RunAsUserToken)) {
082: fail("Should have returned RunAsUserToken");
083: }
084:
085: RunAsUserToken resultCast = (RunAsUserToken) result;
086: assertEquals("my_password".hashCode(), resultCast.getKeyHash());
087: }
088:
089: public void testStartupFailsIfNoKey() throws Exception {
090: RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
091:
092: try {
093: provider.afterPropertiesSet();
094: fail("Should have thrown IllegalArgumentException");
095: } catch (IllegalArgumentException expected) {
096: assertTrue(true);
097: }
098: }
099:
100: public void testStartupSuccess() throws Exception {
101: RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
102: provider.setKey("hello_world");
103: assertEquals("hello_world", provider.getKey());
104: provider.afterPropertiesSet();
105: assertTrue(true);
106: }
107:
108: public void testSupports() {
109: RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
110: assertTrue(provider.supports(RunAsUserToken.class));
111: assertTrue(!provider.supports(TestingAuthenticationToken.class));
112: }
113: }
|