Polish DaoAuthenticationProviderTests

This commit is contained in:
Johnny Lim 2018-02-22 18:48:04 +09:00 committed by Rob Winch
parent 1ed51033cc
commit d316803596
1 changed files with 37 additions and 60 deletions

View File

@ -73,7 +73,7 @@ public class DaoAuthenticationProviderTests {
"rod", "KOala"); "rod", "KOala");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -89,7 +89,7 @@ public class DaoAuthenticationProviderTests {
public void testReceivedBadCredentialsWhenCredentialsNotProvided() { public void testReceivedBadCredentialsWhenCredentialsNotProvided() {
// Test related to SEC-434 // Test related to SEC-434
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
@ -110,7 +110,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService( provider.setUserDetailsService(
new MockAuthenticationDaoUserPeterAccountExpired()); new MockUserDetailsServiceUserPeterAccountExpired());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -128,7 +128,7 @@ public class DaoAuthenticationProviderTests {
"peter", "opal"); "peter", "opal");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserPeterAccountLocked()); provider.setUserDetailsService(new MockUserDetailsServiceUserPeterAccountLocked());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -147,7 +147,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService( provider.setUserDetailsService(
new MockAuthenticationDaoUserPeterCredentialsExpired()); new MockUserDetailsServiceUserPeterCredentialsExpired());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -177,7 +177,7 @@ public class DaoAuthenticationProviderTests {
"peter", "opal"); "peter", "opal");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserPeter()); provider.setUserDetailsService(new MockUserDetailsServiceUserPeter());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -195,7 +195,7 @@ public class DaoAuthenticationProviderTests {
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoSimulateBackendError()); provider.setUserDetailsService(new MockUserDetailsServiceSimulateBackendError());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -212,7 +212,7 @@ public class DaoAuthenticationProviderTests {
null, "koala"); null, "koala");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -230,7 +230,7 @@ public class DaoAuthenticationProviderTests {
"rod", "INVALID_PASSWORD"); "rod", "INVALID_PASSWORD");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -250,7 +250,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setHideUserNotFoundExceptions(false); // we want provider.setHideUserNotFoundExceptions(false); // we want
// UsernameNotFoundExceptions // UsernameNotFoundExceptions
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -269,7 +269,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
assertThat(provider.isHideUserNotFoundExceptions()).isTrue(); assertThat(provider.isHideUserNotFoundExceptions()).isTrue();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -288,7 +288,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
assertThat(provider.isHideUserNotFoundExceptions()).isTrue(); assertThat(provider.isHideUserNotFoundExceptions()).isTrue();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -316,7 +316,7 @@ public class DaoAuthenticationProviderTests {
"RoD", "koala"); "RoD", "koala");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
try { try {
@ -335,7 +335,7 @@ public class DaoAuthenticationProviderTests {
token.setDetails("192.168.0.1"); token.setDetails("192.168.0.1");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
Authentication result = provider.authenticate(token); Authentication result = provider.authenticate(token);
@ -359,7 +359,7 @@ public class DaoAuthenticationProviderTests {
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
Authentication result = provider.authenticate(token); Authentication result = provider.authenticate(token);
@ -384,7 +384,7 @@ public class DaoAuthenticationProviderTests {
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
provider.setForcePrincipalAsString(true); provider.setForcePrincipalAsString(true);
@ -405,7 +405,7 @@ public class DaoAuthenticationProviderTests {
"rod", "koala"); "rod", "koala");
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoReturnsNull()); provider.setUserDetailsService(new MockUserDetailsServiceReturnsNull());
try { try {
provider.authenticate(token); provider.authenticate(token);
@ -439,7 +439,7 @@ public class DaoAuthenticationProviderTests {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
"rod", "koala"); "rod", "koala");
MockAuthenticationDaoUserrod authenticationDao = new MockAuthenticationDaoUserrod(); MockUserDetailsServiceUserRod authenticationDao = new MockUserDetailsServiceUserRod();
MockUserCache cache = new MockUserCache(); MockUserCache cache = new MockUserCache();
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(authenticationDao); provider.setUserDetailsService(authenticationDao);
@ -480,7 +480,7 @@ public class DaoAuthenticationProviderTests {
@Test @Test
public void testStartupFailsIfNoUserCacheSet() throws Exception { public void testStartupFailsIfNoUserCacheSet() throws Exception {
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
assertThat(provider.getUserCache().getClass()).isEqualTo(NullUserCache.class); assertThat(provider.getUserCache().getClass()).isEqualTo(NullUserCache.class);
provider.setUserCache(null); provider.setUserCache(null);
@ -496,7 +496,7 @@ public class DaoAuthenticationProviderTests {
@Test @Test
public void testStartupSuccess() throws Exception { public void testStartupSuccess() throws Exception {
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
UserDetailsService userDetailsService = new MockAuthenticationDaoUserrod(); UserDetailsService userDetailsService = new MockUserDetailsServiceUserRod();
provider.setUserDetailsService(userDetailsService); provider.setUserDetailsService(userDetailsService);
provider.setUserCache(new MockUserCache()); provider.setUserCache(new MockUserCache());
assertThat(provider.getUserDetailsService()).isEqualTo(userDetailsService); assertThat(provider.getUserDetailsService()).isEqualTo(userDetailsService);
@ -521,7 +521,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setHideUserNotFoundExceptions(false); provider.setHideUserNotFoundExceptions(false);
provider.setPasswordEncoder(encoder); provider.setPasswordEncoder(encoder);
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
provider.afterPropertiesSet(); provider.afterPropertiesSet();
try { try {
provider.authenticate(token); provider.authenticate(token);
@ -543,7 +543,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setHideUserNotFoundExceptions(false); provider.setHideUserNotFoundExceptions(false);
provider.setPasswordEncoder(encoder); provider.setPasswordEncoder(encoder);
MockAuthenticationDaoUserrod userDetailsService = new MockAuthenticationDaoUserrod(); MockUserDetailsServiceUserRod userDetailsService = new MockUserDetailsServiceUserRod();
userDetailsService.password = encoder.encode( userDetailsService.password = encoder.encode(
(CharSequence) token.getCredentials()); (CharSequence) token.getCredentials());
provider.setUserDetailsService(userDetailsService); provider.setUserDetailsService(userDetailsService);
@ -561,7 +561,7 @@ public class DaoAuthenticationProviderTests {
"missing", null); "missing", null);
DaoAuthenticationProvider provider = createProvider(); DaoAuthenticationProvider provider = createProvider();
provider.setHideUserNotFoundExceptions(false); provider.setHideUserNotFoundExceptions(false);
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
try { try {
provider.authenticate(token); provider.authenticate(token);
fail("Expected Exception"); fail("Expected Exception");
@ -584,7 +584,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setHideUserNotFoundExceptions(false); provider.setHideUserNotFoundExceptions(false);
provider.setPasswordEncoder(encoder); provider.setPasswordEncoder(encoder);
MockAuthenticationDaoUserrod userDetailsService = new MockAuthenticationDaoUserrod(); MockUserDetailsServiceUserRod userDetailsService = new MockUserDetailsServiceUserRod();
userDetailsService.password = encoder.encode( userDetailsService.password = encoder.encode(
(CharSequence) foundUser.getCredentials()); (CharSequence) foundUser.getCredentials());
provider.setUserDetailsService(userDetailsService); provider.setUserDetailsService(userDetailsService);
@ -634,7 +634,7 @@ public class DaoAuthenticationProviderTests {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setHideUserNotFoundExceptions(false); provider.setHideUserNotFoundExceptions(false);
provider.setPasswordEncoder(encoder); provider.setPasswordEncoder(encoder);
provider.setUserDetailsService(new MockAuthenticationDaoUserrod()); provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
try { try {
provider.authenticate(token); provider.authenticate(token);
fail("Expected Exception"); fail("Expected Exception");
@ -648,14 +648,14 @@ public class DaoAuthenticationProviderTests {
// ~ Inner Classes // ~ Inner Classes
// ================================================================================================== // ==================================================================================================
private class MockAuthenticationDaoReturnsNull implements UserDetailsService { private class MockUserDetailsServiceReturnsNull implements UserDetailsService {
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(String username) {
return null; return null;
} }
} }
private class MockAuthenticationDaoSimulateBackendError private class MockUserDetailsServiceSimulateBackendError
implements UserDetailsService { implements UserDetailsService {
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(String username) {
@ -664,7 +664,7 @@ public class DaoAuthenticationProviderTests {
} }
} }
private class MockAuthenticationDaoUserrod implements UserDetailsService { private class MockUserDetailsServiceUserRod implements UserDetailsService {
private String password = "koala"; private String password = "koala";
@ -672,79 +672,56 @@ public class DaoAuthenticationProviderTests {
if ("rod".equals(username)) { if ("rod".equals(username)) {
return new User("rod", password, true, true, true, true, ROLES_12); return new User("rod", password, true, true, true, true, ROLES_12);
} }
else {
throw new UsernameNotFoundException("Could not find: " + username); throw new UsernameNotFoundException("Could not find: " + username);
} }
}
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
} }
private class MockAuthenticationDaoUserrodWithSalt implements UserDetailsService { private class MockUserDetailsServiceUserPeter implements UserDetailsService {
public UserDetails loadUserByUsername(String username) {
if ("rod".equals(username)) {
return new User("rod", "koala{SYSTEM_SALT_VALUE}", true, true, true, true,
ROLES_12);
}
else {
throw new UsernameNotFoundException("Could not find: " + username);
}
}
}
private class MockAuthenticationDaoUserPeter implements UserDetailsService {
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(String username) {
if ("peter".equals(username)) { if ("peter".equals(username)) {
return new User("peter", "opal", false, true, true, true, ROLES_12); return new User("peter", "opal", false, true, true, true, ROLES_12);
} }
else {
throw new UsernameNotFoundException("Could not find: " + username); throw new UsernameNotFoundException("Could not find: " + username);
} }
} }
}
private class MockAuthenticationDaoUserPeterAccountExpired private class MockUserDetailsServiceUserPeterAccountExpired
implements UserDetailsService { implements UserDetailsService {
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(String username) {
if ("peter".equals(username)) { if ("peter".equals(username)) {
return new User("peter", "opal", true, false, true, true, ROLES_12); return new User("peter", "opal", true, false, true, true, ROLES_12);
} }
else {
throw new UsernameNotFoundException("Could not find: " + username); throw new UsernameNotFoundException("Could not find: " + username);
} }
} }
}
private class MockAuthenticationDaoUserPeterAccountLocked private class MockUserDetailsServiceUserPeterAccountLocked
implements UserDetailsService { implements UserDetailsService {
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(String username) {
if ("peter".equals(username)) { if ("peter".equals(username)) {
return new User("peter", "opal", true, true, true, false, ROLES_12); return new User("peter", "opal", true, true, true, false, ROLES_12);
} }
else {
throw new UsernameNotFoundException("Could not find: " + username); throw new UsernameNotFoundException("Could not find: " + username);
} }
} }
}
private class MockAuthenticationDaoUserPeterCredentialsExpired private class MockUserDetailsServiceUserPeterCredentialsExpired
implements UserDetailsService { implements UserDetailsService {
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(String username) {
if ("peter".equals(username)) { if ("peter".equals(username)) {
return new User("peter", "opal", true, true, false, true, ROLES_12); return new User("peter", "opal", true, true, false, true, ROLES_12);
} }
else {
throw new UsernameNotFoundException("Could not find: " + username); throw new UsernameNotFoundException("Could not find: " + username);
} }
} }
}
private DaoAuthenticationProvider createProvider() { private DaoAuthenticationProvider createProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); DaoAuthenticationProvider provider = new DaoAuthenticationProvider();