Expanded unit test coverage.

This commit is contained in:
Ben Alex 2004-03-28 11:39:38 +00:00
parent 6038d56ece
commit 8808f5e8dd

View File

@ -17,31 +17,16 @@ package net.sf.acegisecurity.providers.dao.memory;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.acegisecurity.AccessDeniedException; import net.sf.acegisecurity.providers.dao.UsernameNotFoundException;
import net.sf.acegisecurity.BadCredentialsException;
import net.sf.acegisecurity.DisabledException;
import net.sf.acegisecurity.context.Account;
import net.sf.acegisecurity.context.BankManager;
import net.sf.acegisecurity.context.Context;
import net.sf.acegisecurity.context.ContextHolder;
import net.sf.acegisecurity.context.SecureContext;
import net.sf.acegisecurity.context.SecureContextImpl;
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* Tests {@link DaoAuthenticationProvider} with {@link InMemoryDaoImpl}. * Tests {@link InMemoryDaoImpl}.
* *
* @author Ben Alex * @author Ben Alex
* @version $Id$ * @version $Id$
*/ */
public class InMemoryDaoTests extends TestCase { public class InMemoryDaoTests extends TestCase {
//~ Instance fields ========================================================
private ClassPathXmlApplicationContext ctx;
//~ Constructors =========================================================== //~ Constructors ===========================================================
public InMemoryDaoTests() { public InMemoryDaoTests() {
@ -56,142 +41,76 @@ public class InMemoryDaoTests extends TestCase {
public final void setUp() throws Exception { public final void setUp() throws Exception {
super.setUp(); super.setUp();
ctx = new ClassPathXmlApplicationContext(
"/net/sf/acegisecurity/providers/dao/memory/applicationContext.xml");
} }
public static void main(String[] args) { public static void main(String[] args) {
junit.textui.TestRunner.run(InMemoryDaoTests.class); junit.textui.TestRunner.run(InMemoryDaoTests.class);
} }
public void testAuthentication() throws Exception { public void testLookupFails() throws Exception {
Account account = new Account(1, "someone"); InMemoryDaoImpl dao = new InMemoryDaoImpl();
BankManager bank = (BankManager) ctx.getBean("bankManager"); dao.setUserMap(makeUserMap());
dao.afterPropertiesSet();
// Try with an invalid username and password
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("jennifer",
"zebra");
SecureContext secureContext = new SecureContextImpl();
secureContext.setAuthentication(token);
ContextHolder.setContext((Context) secureContext);
try { try {
bank.saveAccount(account); dao.loadUserByUsername("UNKNOWN_USER");
fail("Should have thrown a BadCredentialsException"); fail("Should have thrown UsernameNotFoundException");
} catch (BadCredentialsException expected) { } catch (UsernameNotFoundException expected) {
assertTrue(true); assertTrue(true);
} }
// Check our token represents itself properly as a String
System.out.println(token.toString());
assertTrue(token.toString().length() > 10);
// Now try with a valid username, but invalid password
token = new UsernamePasswordAuthenticationToken("marissa", "zebra");
secureContext.setAuthentication(token);
ContextHolder.setContext((Context) secureContext);
try {
bank.saveAccount(account);
fail("Should have thrown a BadCredentialsException");
} catch (BadCredentialsException expected) {
assertTrue(true);
}
// Now try with a valid username and password, but disabled user
token = new UsernamePasswordAuthenticationToken("dianne", "emu");
secureContext.setAuthentication(token);
ContextHolder.setContext((Context) secureContext);
try {
bank.saveAccount(account);
fail("Should have thrown a DisabledException");
} catch (DisabledException expected) {
assertTrue(true);
}
// Now try as a user who didn't have a password defined, and thus
// would have been considered invalid at time of creation
token = new UsernamePasswordAuthenticationToken("someoneelse", "");
secureContext.setAuthentication(token);
ContextHolder.setContext((Context) secureContext);
try {
bank.saveAccount(account);
fail("Should have thrown a BadCredentialsException");
} catch (BadCredentialsException expected) {
assertTrue(true);
}
// Now try as a user who had a password, but no granted authorities,
// and thus would have been considered invalid at time of creation
token = new UsernamePasswordAuthenticationToken("someone", "password");
secureContext.setAuthentication(token);
ContextHolder.setContext((Context) secureContext);
try {
bank.saveAccount(account);
fail("Should have thrown a BadCredentialsException");
} catch (BadCredentialsException expected) {
assertTrue(true);
}
// Now try with a valid mixed case username, valid mixed case password,
// (application context requires passwords to be case matched)
token = new UsernamePasswordAuthenticationToken("MaRiSsA", "kOaLa");
secureContext.setAuthentication(token);
ContextHolder.setContext((Context) secureContext);
try {
bank.saveAccount(account);
fail("Should have thrown a BadCredentialsException");
} catch (BadCredentialsException expected) {
assertTrue(true);
}
// Now try with a valid mixed case username, correct case password,
// (application context does not require usernames to be case matched)
token = new UsernamePasswordAuthenticationToken("MaRiSsA", "koala");
secureContext.setAuthentication(token);
ContextHolder.setContext((Context) secureContext);
bank.saveAccount(account);
ContextHolder.setContext(null);
} }
public void testAuthorization() throws Exception { public void testLookupSuccess() throws Exception {
Account account = new Account(45, "someone"); InMemoryDaoImpl dao = new InMemoryDaoImpl();
BankManager bank = (BankManager) ctx.getBean("bankManager"); dao.setUserMap(makeUserMap());
dao.afterPropertiesSet();
assertEquals("koala", dao.loadUserByUsername("marissa").getPassword());
assertEquals("wombat", dao.loadUserByUsername("scott").getPassword());
}
// Try as a user without access to the account public void testLookupSuccessWithMixedeCase() throws Exception {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("peter", InMemoryDaoImpl dao = new InMemoryDaoImpl();
"opal"); dao.setUserMap(makeUserMap());
SecureContext secureContext = new SecureContextImpl(); dao.afterPropertiesSet();
secureContext.setAuthentication(token); assertEquals("koala", dao.loadUserByUsername("MaRiSSA").getPassword());
ContextHolder.setContext((Context) secureContext); assertEquals("wombat", dao.loadUserByUsername("ScOTt").getPassword());
}
public void testStartupFailsIfUserMapNotSet() throws Exception {
InMemoryDaoImpl dao = new InMemoryDaoImpl();
try { try {
// NB: account number 45 != granted authority for account 77 dao.afterPropertiesSet();
bank.loadAccount(account.getId()); fail("Shoudl have thrown IllegalArgumentException");
fail("Should have thrown an AccessDeniedException"); } catch (IllegalArgumentException expected) {
} catch (AccessDeniedException expected) {
assertTrue(true); assertTrue(true);
} }
}
// Now try as user with access to account number 45 public void testStartupFailsIfUserMapSetToNull() throws Exception {
token = new UsernamePasswordAuthenticationToken("scott", "wombat"); InMemoryDaoImpl dao = new InMemoryDaoImpl();
secureContext.setAuthentication(token); dao.setUserMap(null);
ContextHolder.setContext((Context) secureContext);
bank.loadAccount(account.getId());
assertTrue(true);
// Now try as user with ROLE_SUPERVISOR access to the account try {
token = new UsernamePasswordAuthenticationToken("marissa", "koala"); dao.afterPropertiesSet();
secureContext.setAuthentication(token); fail("Shoudl have thrown IllegalArgumentException");
ContextHolder.setContext((Context) secureContext); } catch (IllegalArgumentException expected) {
bank.loadAccount(account.getId()); assertTrue(true);
assertTrue(true); }
}
ContextHolder.setContext(null); public void testStartupSuccessIfUserMapSet() throws Exception {
InMemoryDaoImpl dao = new InMemoryDaoImpl();
dao.setUserMap(makeUserMap());
dao.afterPropertiesSet();
assertEquals(2, dao.getUserMap().getUserCount());
}
private UserMap makeUserMap() {
UserMapEditor editor = new UserMapEditor();
editor.setAsText(
"marissa=koala,ROLE_ONE,ROLE_TWO,enabled\r\nscott=wombat,ROLE_ONE,ROLE_TWO,enabled");
return (UserMap) editor.getValue();
} }
} }