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: package org.acegisecurity.providers.openid;
016:
017: import junit.framework.TestCase;
018:
019: import org.acegisecurity.Authentication;
020: import org.acegisecurity.AuthenticationServiceException;
021: import org.acegisecurity.BadCredentialsException;
022:
023: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
024:
025: /**
026: * Tests {@link OpenIDAuthenticationProvider}
027: *
028: * @author Robin Bramley, Opsera Ltd
029: */
030: public class OpenIDAuthenticationProviderTests extends TestCase {
031: //~ Static fields/initializers =====================================================================================
032:
033: private static final String USERNAME = "user.acegiopenid.com";
034:
035: //~ Methods ========================================================================================================
036:
037: /*
038: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.authenticate(Authentication)'
039: */
040: public void testAuthenticateCancel() {
041: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
042: provider
043: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
044:
045: Authentication preAuth = new OpenIDAuthenticationToken(
046: OpenIDAuthenticationStatus.CANCELLED, USERNAME, "");
047:
048: assertFalse(preAuth.isAuthenticated());
049:
050: try {
051: provider.authenticate(preAuth);
052: fail("Should throw an AuthenticationException");
053: } catch (AuthenticationCancelledException expected) {
054: assertEquals("Log in cancelled", expected.getMessage());
055: }
056: }
057:
058: /*
059: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.authenticate(Authentication)'
060: */
061: public void testAuthenticateError() {
062: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
063: provider
064: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
065:
066: Authentication preAuth = new OpenIDAuthenticationToken(
067: OpenIDAuthenticationStatus.ERROR, USERNAME, "");
068:
069: assertFalse(preAuth.isAuthenticated());
070:
071: try {
072: provider.authenticate(preAuth);
073: fail("Should throw an AuthenticationException");
074: } catch (AuthenticationServiceException expected) {
075: assertEquals("Error message from server: ", expected
076: .getMessage());
077: }
078: }
079:
080: /*
081: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.authenticate(Authentication)'
082: */
083: public void testAuthenticateFailure() {
084: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
085: provider
086: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
087:
088: Authentication preAuth = new OpenIDAuthenticationToken(
089: OpenIDAuthenticationStatus.FAILURE, USERNAME, "");
090:
091: assertFalse(preAuth.isAuthenticated());
092:
093: try {
094: provider.authenticate(preAuth);
095: fail("Should throw an AuthenticationException");
096: } catch (BadCredentialsException expected) {
097: assertEquals(
098: "Log in failed - identity could not be verified",
099: expected.getMessage());
100: }
101: }
102:
103: /*
104: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.authenticate(Authentication)'
105: */
106: public void testAuthenticateSetupNeeded() {
107: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
108: provider
109: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
110:
111: Authentication preAuth = new OpenIDAuthenticationToken(
112: OpenIDAuthenticationStatus.SETUP_NEEDED, USERNAME, "");
113:
114: assertFalse(preAuth.isAuthenticated());
115:
116: try {
117: provider.authenticate(preAuth);
118: fail("Should throw an AuthenticationException");
119: } catch (AuthenticationServiceException expected) {
120: assertEquals(
121: "The server responded setup was needed, which shouldn't happen",
122: expected.getMessage());
123: }
124: }
125:
126: /*
127: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.authenticate(Authentication)'
128: */
129: public void testAuthenticateSuccess() {
130: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
131: provider
132: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
133:
134: Authentication preAuth = new OpenIDAuthenticationToken(
135: OpenIDAuthenticationStatus.SUCCESS, USERNAME, "");
136:
137: assertFalse(preAuth.isAuthenticated());
138:
139: Authentication postAuth = provider.authenticate(preAuth);
140:
141: assertNotNull(postAuth);
142: assertTrue(postAuth instanceof OpenIDAuthenticationToken);
143: assertTrue(postAuth.isAuthenticated());
144: assertNotNull(postAuth.getPrincipal());
145: assertEquals(preAuth.getPrincipal(), postAuth.getPrincipal());
146: assertNotNull(postAuth.getAuthorities());
147: assertTrue(postAuth.getAuthorities().length > 0);
148: assertTrue(((OpenIDAuthenticationToken) postAuth).getStatus() == OpenIDAuthenticationStatus.SUCCESS);
149: assertTrue(((OpenIDAuthenticationToken) postAuth).getMessage() == null);
150: }
151:
152: public void testDetectsMissingAuthoritiesPopulator() {
153: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
154:
155: try {
156: provider.afterPropertiesSet();
157: fail("Should have thrown Exception");
158: } catch (Exception expected) {
159: assertEquals("The ssoAuthoritiesPopulator must be set",
160: expected.getMessage());
161: }
162: }
163:
164: /*
165: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.supports(Class)'
166: */
167: public void testDoesntSupport() {
168: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
169: provider
170: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
171:
172: assertFalse(provider
173: .supports(UsernamePasswordAuthenticationToken.class));
174: }
175:
176: /*
177: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.authenticate(Authentication)'
178: */
179: public void testIgnoresUserPassAuthToken() {
180: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
181: provider
182: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
183:
184: UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
185: USERNAME, "password");
186: assertEquals(null, provider.authenticate(token));
187: }
188:
189: /*
190: * Test method for 'org.acegisecurity.providers.openid.OpenIDAuthenticationProvider.supports(Class)'
191: */
192: public void testSupports() {
193: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
194: provider
195: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
196:
197: assertTrue(provider.supports(OpenIDAuthenticationToken.class));
198: }
199:
200: public void testValidation() throws Exception {
201: OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
202: provider
203: .setSsoAuthoritiesPopulator(new MockAuthoritiesPopulator());
204: provider.afterPropertiesSet();
205:
206: provider.setSsoAuthoritiesPopulator(null);
207:
208: try {
209: provider.afterPropertiesSet();
210: fail("IllegalArgumentException expected, ssoAuthoritiesPopulator is null");
211: } catch (IllegalArgumentException e) {
212: //expected
213: }
214: }
215: }
|