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.providers.rememberme;
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:
027: /**
028: * Tests {@link RememberMeAuthenticationProvider}.
029: *
030: * @author Ben Alex
031: * @version $Id: RememberMeAuthenticationProviderTests.java 1496 2006-05-23 13:38:33Z benalex $
032: */
033: public class RememberMeAuthenticationProviderTests extends TestCase {
034: //~ Constructors ===================================================================================================
035:
036: public RememberMeAuthenticationProviderTests() {
037: super ();
038: }
039:
040: public RememberMeAuthenticationProviderTests(String arg0) {
041: super (arg0);
042: }
043:
044: //~ Methods ========================================================================================================
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner
048: .run(RememberMeAuthenticationProviderTests.class);
049: }
050:
051: public final void setUp() throws Exception {
052: super .setUp();
053: }
054:
055: public void testDetectsAnInvalidKey() throws Exception {
056: RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
057: aap.setKey("qwerty");
058:
059: RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(
060: "WRONG_KEY", "Test", new GrantedAuthority[] {
061: new GrantedAuthorityImpl("ROLE_ONE"),
062: new GrantedAuthorityImpl("ROLE_TWO") });
063:
064: try {
065: Authentication result = aap.authenticate(token);
066: fail("Should have thrown BadCredentialsException");
067: } catch (BadCredentialsException expected) {
068: assertEquals(
069: "The presented RememberMeAuthenticationToken does not contain the expected key",
070: expected.getMessage());
071: }
072: }
073:
074: public void testDetectsMissingKey() throws Exception {
075: RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
076:
077: try {
078: aap.afterPropertiesSet();
079: fail("Should have thrown IllegalArgumentException");
080: } catch (IllegalArgumentException expected) {
081: assertTrue(true);
082: }
083: }
084:
085: public void testGettersSetters() throws Exception {
086: RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
087: aap.setKey("qwerty");
088: aap.afterPropertiesSet();
089: assertEquals("qwerty", aap.getKey());
090: }
091:
092: public void testIgnoresClassesItDoesNotSupport() throws Exception {
093: RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
094: aap.setKey("qwerty");
095:
096: TestingAuthenticationToken token = new TestingAuthenticationToken(
097: "user", "password",
098: new GrantedAuthority[] { new GrantedAuthorityImpl(
099: "ROLE_A") });
100: assertFalse(aap.supports(TestingAuthenticationToken.class));
101:
102: // Try it anyway
103: assertNull(aap.authenticate(token));
104: }
105:
106: public void testNormalOperation() throws Exception {
107: RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
108: aap.setKey("qwerty");
109:
110: RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(
111: "qwerty", "Test", new GrantedAuthority[] {
112: new GrantedAuthorityImpl("ROLE_ONE"),
113: new GrantedAuthorityImpl("ROLE_TWO") });
114:
115: Authentication result = aap.authenticate(token);
116:
117: assertEquals(result, token);
118: }
119:
120: public void testSupports() {
121: RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
122: assertTrue(aap.supports(RememberMeAuthenticationToken.class));
123: assertFalse(aap.supports(TestingAuthenticationToken.class));
124: }
125: }
|