From 0a856b7f1573a84256bdc304fd1a59da0a17a575 Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Sun, 18 Apr 2004 12:04:43 +0000 Subject: [PATCH] Expand coverage to test SaltProvider integration. --- .../dao/DaoAuthenticationProviderTests.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/core/src/test/java/org/acegisecurity/providers/dao/DaoAuthenticationProviderTests.java b/core/src/test/java/org/acegisecurity/providers/dao/DaoAuthenticationProviderTests.java index 1b77d87a0e..9986dbf8e5 100644 --- a/core/src/test/java/org/acegisecurity/providers/dao/DaoAuthenticationProviderTests.java +++ b/core/src/test/java/org/acegisecurity/providers/dao/DaoAuthenticationProviderTests.java @@ -25,6 +25,8 @@ import net.sf.acegisecurity.GrantedAuthority; import net.sf.acegisecurity.GrantedAuthorityImpl; import net.sf.acegisecurity.providers.TestingAuthenticationToken; import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken; +import net.sf.acegisecurity.providers.dao.salt.SystemWideSaltSource; +import net.sf.acegisecurity.providers.encoding.ShaPasswordEncoder; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataRetrievalFailureException; @@ -158,6 +160,42 @@ public class DaoAuthenticationProviderTests extends TestCase { assertEquals("ROLE_TWO", castResult.getAuthorities()[1].getAuthority()); } + public void testAuthenticatesWhenASaltIsUsed() { + UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("marissa", + "koala"); + + SystemWideSaltSource salt = new SystemWideSaltSource(); + salt.setSystemWideSalt("SYSTEM_SALT_VALUE"); + + DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); + provider.setAuthenticationDao(new MockAuthenticationDaoUserMarissaWithSalt()); + provider.setSaltSource(salt); + + Authentication result = provider.authenticate(token); + + if (!(result instanceof UsernamePasswordAuthenticationToken)) { + fail( + "Should have returned instance of UsernamePasswordAuthenticationToken"); + } + + UsernamePasswordAuthenticationToken castResult = (UsernamePasswordAuthenticationToken) result; + assertEquals("marissa", castResult.getPrincipal()); + assertEquals("koala", castResult.getCredentials()); + assertEquals("ROLE_ONE", castResult.getAuthorities()[0].getAuthority()); + assertEquals("ROLE_TWO", castResult.getAuthorities()[1].getAuthority()); + } + + public void testGettersSetters() { + DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); + provider.setPasswordEncoder(new ShaPasswordEncoder()); + assertEquals(ShaPasswordEncoder.class, + provider.getPasswordEncoder().getClass()); + + provider.setSaltSource(new SystemWideSaltSource()); + assertEquals(SystemWideSaltSource.class, + provider.getSaltSource().getClass()); + } + public void testStartupFailsIfNoAuthenticationDao() throws Exception { DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); @@ -210,6 +248,21 @@ public class DaoAuthenticationProviderTests extends TestCase { } } + private class MockAuthenticationDaoUserMarissaWithSalt + implements AuthenticationDao { + public User loadUserByUsername(String username) + throws UsernameNotFoundException, DataAccessException { + if ("marissa".equals(username)) { + return new User("marissa", "koala{SYSTEM_SALT_VALUE}", true, + new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl( + "ROLE_TWO")}); + } else { + throw new UsernameNotFoundException("Could not find: " + + username); + } + } + } + private class MockAuthenticationDaoUserPeter implements AuthenticationDao { public User loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {