/* * Licensed to Jasig under one or more contributor license * agreements. See the NOTICE file distributed with this work * for additional information regarding copyright ownership. * Jasig licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a * copy of the License at the following location: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.jasig.cas.adaptors.generic; import static org.junit.Assert.*; import java.net.MalformedURLException; import java.net.URL; import javax.security.auth.login.AccountNotFoundException; import javax.security.auth.login.FailedLoginException; import org.jasig.cas.authentication.PreventedException; import org.jasig.cas.authentication.UsernamePasswordCredential; import org.jasig.cas.authentication.HttpBasedServiceCredential; import org.junit.Before; import org.junit.Test; import org.springframework.core.io.ClassPathResource; /** * @author Scott Battaglia */ public class FileAuthenticationHandlerTests { private FileAuthenticationHandler authenticationHandler; @Before public void setUp() throws Exception { this.authenticationHandler = new FileAuthenticationHandler(); this.authenticationHandler.setFileName( new ClassPathResource("org/jasig/cas/adaptors/generic/authentication.txt")); } @Test public void testSupportsProperUserCredentials() throws Exception { UsernamePasswordCredential c = new UsernamePasswordCredential(); c.setUsername("scott"); c.setPassword("rutgers"); assertNotNull(this.authenticationHandler.authenticate(c)); } @Test public void testDoesntSupportBadUserCredentials() { try { final HttpBasedServiceCredential c = new HttpBasedServiceCredential( new URL("http://www.rutgers.edu")); assertFalse(this.authenticationHandler.supports(c)); } catch (final MalformedURLException e) { fail("MalformedURLException caught."); } } @Test public void testAuthenticatesUserInFileWithDefaultSeparator() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); c.setUsername("scott"); c.setPassword("rutgers"); assertNotNull(this.authenticationHandler.authenticate(c)); } @Test(expected = AccountNotFoundException.class) public void testFailsUserNotInFileWithDefaultSeparator() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); c.setUsername("fds"); c.setPassword("rutgers"); this.authenticationHandler.authenticate(c); } @Test(expected = AccountNotFoundException.class) public void testFailsNullUserName() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); c.setUsername(null); c.setPassword("user"); this.authenticationHandler.authenticate(c); } @Test(expected = AccountNotFoundException.class) public void testFailsNullUserNameAndPassword() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); c.setUsername(null); c.setPassword(null); this.authenticationHandler.authenticate(c); } @Test(expected = FailedLoginException.class) public void testFailsNullPassword() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); c.setUsername("scott"); c.setPassword(null); this.authenticationHandler.authenticate(c); } @Test public void testAuthenticatesUserInFileWithCommaSeparator() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); this.authenticationHandler.setFileName( new ClassPathResource("org/jasig/cas/adaptors/generic/authentication2.txt")); this.authenticationHandler.setSeparator(","); c.setUsername("scott"); c.setPassword("rutgers"); assertNotNull(this.authenticationHandler.authenticate(c)); } @Test(expected = AccountNotFoundException.class) public void testFailsUserNotInFileWithCommaSeparator() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); this.authenticationHandler.setFileName( new ClassPathResource("org/jasig/cas/adaptors/generic/authentication2.txt")); this.authenticationHandler.setSeparator(","); c.setUsername("fds"); c.setPassword("rutgers"); this.authenticationHandler.authenticate(c); } @Test(expected = FailedLoginException.class) public void testFailsGoodUsernameBadPassword() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); this.authenticationHandler.setFileName( new ClassPathResource("org/jasig/cas/adaptors/generic/authentication2.txt")); this.authenticationHandler.setSeparator(","); c.setUsername("scott"); c.setPassword("rutgers1"); this.authenticationHandler.authenticate(c); } @Test(expected = PreventedException.class) public void testAuthenticateNoFileName() throws Exception { final UsernamePasswordCredential c = new UsernamePasswordCredential(); this.authenticationHandler.setFileName(new ClassPathResource("fff")); c.setUsername("scott"); c.setPassword("rutgers"); this.authenticationHandler.authenticate(c); } }