SEC-2781: Remove deprecations
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
package org.springframework.security.access.hierarchicalroles;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class UserDetailsServiceWrapperTests {
|
||||
|
||||
private UserDetailsService wrappedUserDetailsService = null;
|
||||
private UserDetailsServiceWrapper userDetailsServiceWrapper = null;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
|
||||
roleHierarchy.setHierarchy("ROLE_A > ROLE_B");
|
||||
final UserDetails user = new User("EXISTING_USER", "PASSWORD", true, true, true, true,
|
||||
AuthorityUtils.createAuthorityList("ROLE_A"));
|
||||
final UserDetailsService wrappedUserDetailsService = mock(UserDetailsService.class);
|
||||
when(wrappedUserDetailsService.loadUserByUsername("EXISTING_USER")).thenReturn(user);
|
||||
when(wrappedUserDetailsService.loadUserByUsername("USERNAME_NOT_FOUND_EXCEPTION")).thenThrow(new UsernameNotFoundException("USERNAME_NOT_FOUND_EXCEPTION"));
|
||||
|
||||
this.wrappedUserDetailsService = wrappedUserDetailsService;
|
||||
userDetailsServiceWrapper = new UserDetailsServiceWrapper();
|
||||
userDetailsServiceWrapper.setRoleHierarchy(roleHierarchy);
|
||||
userDetailsServiceWrapper.setUserDetailsService(wrappedUserDetailsService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadUserByUsername() {
|
||||
UserDetails expectedUserDetails = new User("EXISTING_USER", "PASSWORD", true, true, true, true,
|
||||
AuthorityUtils.createAuthorityList("ROLE_A", "ROLE_B"));
|
||||
UserDetails userDetails = userDetailsServiceWrapper.loadUserByUsername("EXISTING_USER");
|
||||
assertEquals(expectedUserDetails.getPassword(), userDetails.getPassword());
|
||||
assertEquals(expectedUserDetails.getUsername(), userDetails.getUsername());
|
||||
assertEquals(expectedUserDetails.isAccountNonExpired(), userDetails.isAccountNonExpired());
|
||||
assertEquals(expectedUserDetails.isAccountNonLocked(), userDetails.isAccountNonLocked());
|
||||
assertEquals(expectedUserDetails.isCredentialsNonExpired(), expectedUserDetails.isCredentialsNonExpired());
|
||||
assertEquals(expectedUserDetails.isEnabled(), userDetails.isEnabled());
|
||||
assertTrue(HierarchicalRolesTestHelper.containTheSameGrantedAuthorities(expectedUserDetails.getAuthorities(), userDetails.getAuthorities()));
|
||||
|
||||
try {
|
||||
userDetails = userDetailsServiceWrapper.loadUserByUsername("USERNAME_NOT_FOUND_EXCEPTION");
|
||||
fail("testLoadUserByUsername() - UsernameNotFoundException did not bubble up!");
|
||||
} catch (UsernameNotFoundException e) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWrappedUserDetailsService() {
|
||||
assertTrue(userDetailsServiceWrapper.getWrappedUserDetailsService() == wrappedUserDetailsService);
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package org.springframework.security.access.hierarchicalroles;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link UserDetailsWrapper}.
|
||||
*
|
||||
* @author Michael Mayr
|
||||
*/
|
||||
@SuppressWarnings({"deprecation"})
|
||||
public class UserDetailsWrapperTests extends TestCase {
|
||||
|
||||
private List<GrantedAuthority> authorities = null;
|
||||
private UserDetails userDetails1 = null;
|
||||
private UserDetails userDetails2 = null;
|
||||
private UserDetailsWrapper userDetailsWrapper1 = null;
|
||||
private UserDetailsWrapper userDetailsWrapper2 = null;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
|
||||
roleHierarchy.setHierarchy("ROLE_A > ROLE_B");
|
||||
authorities = AuthorityUtils.createAuthorityList("ROLE_A");
|
||||
userDetails1 = new User("TestUser1", "TestPassword1", true, true, true, true, authorities);
|
||||
userDetails2 = new User("TestUser2", "TestPassword2", false, false, false, false, authorities);
|
||||
userDetailsWrapper1 = new UserDetailsWrapper(userDetails1, roleHierarchy);
|
||||
userDetailsWrapper2 = new UserDetailsWrapper(userDetails2, roleHierarchy);
|
||||
}
|
||||
|
||||
public void testIsAccountNonExpired() {
|
||||
assertEquals(userDetails1.isAccountNonExpired(), userDetailsWrapper1.isAccountNonExpired());
|
||||
assertEquals(userDetails2.isAccountNonExpired(), userDetailsWrapper2.isAccountNonExpired());
|
||||
}
|
||||
|
||||
public void testIsAccountNonLocked() {
|
||||
assertEquals(userDetails1.isAccountNonLocked(), userDetailsWrapper1.isAccountNonLocked());
|
||||
assertEquals(userDetails2.isAccountNonLocked(), userDetailsWrapper2.isAccountNonLocked());
|
||||
}
|
||||
|
||||
public void testGetAuthorities() {
|
||||
List<GrantedAuthority> expectedAuthorities = AuthorityUtils.createAuthorityList("ROLE_A", "ROLE_B");
|
||||
assertTrue(HierarchicalRolesTestHelper.containTheSameGrantedAuthorities(userDetailsWrapper1.getAuthorities(), expectedAuthorities));
|
||||
assertTrue(HierarchicalRolesTestHelper.containTheSameGrantedAuthorities(userDetailsWrapper2.getAuthorities(), expectedAuthorities));
|
||||
}
|
||||
|
||||
public void testIsCredentialsNonExpired() {
|
||||
assertEquals(userDetails1.isCredentialsNonExpired(), userDetailsWrapper1.isCredentialsNonExpired());
|
||||
assertEquals(userDetails2.isCredentialsNonExpired(), userDetailsWrapper2.isCredentialsNonExpired());
|
||||
}
|
||||
|
||||
public void testIsEnabled() {
|
||||
assertEquals(userDetails1.isEnabled(), userDetailsWrapper1.isEnabled());
|
||||
assertEquals(userDetails2.isEnabled(), userDetailsWrapper2.isEnabled());
|
||||
}
|
||||
|
||||
public void testGetPassword() {
|
||||
assertEquals(userDetails1.getPassword(), userDetailsWrapper1.getPassword());
|
||||
assertEquals(userDetails2.getPassword(), userDetailsWrapper2.getPassword());
|
||||
}
|
||||
|
||||
public void testGetUsername() {
|
||||
assertEquals(userDetails1.getUsername(), userDetailsWrapper1.getUsername());
|
||||
assertEquals(userDetails2.getUsername(), userDetailsWrapper2.getUsername());
|
||||
}
|
||||
|
||||
public void testGetUnwrappedUserDetails() {
|
||||
assertTrue(userDetailsWrapper1.getUnwrappedUserDetails() == userDetails1);
|
||||
assertTrue(userDetailsWrapper2.getUnwrappedUserDetails() == userDetails2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,31 +41,34 @@ public class AbstractAccessDecisionManagerTests extends TestCase {
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void testAllowIfAccessDecisionManagerDefaults() {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
List list = new Vector();
|
||||
DenyAgainVoter denyVoter = new DenyAgainVoter();
|
||||
list.add(denyVoter);
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl(list);
|
||||
assertTrue(!mock.isAllowIfAllAbstainDecisions()); // default
|
||||
mock.setAllowIfAllAbstainDecisions(true);
|
||||
assertTrue(mock.isAllowIfAllAbstainDecisions()); // changed
|
||||
}
|
||||
|
||||
public void testDelegatesSupportsClassRequests() throws Exception {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
List list = new Vector();
|
||||
list.add(new DenyVoter());
|
||||
list.add(new MockStringOnlyVoter());
|
||||
mock.setDecisionVoters(list);
|
||||
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl(list);
|
||||
|
||||
assertTrue(mock.supports(String.class));
|
||||
assertTrue(!mock.supports(Integer.class));
|
||||
}
|
||||
|
||||
public void testDelegatesSupportsRequests() throws Exception {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
List list = new Vector();
|
||||
DenyVoter voter = new DenyVoter();
|
||||
DenyAgainVoter denyVoter = new DenyAgainVoter();
|
||||
list.add(voter);
|
||||
list.add(denyVoter);
|
||||
mock.setDecisionVoters(list);
|
||||
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl(list);
|
||||
|
||||
ConfigAttribute attr = new SecurityConfig("DENY_AGAIN_FOR_SURE");
|
||||
assertTrue(mock.supports(attr));
|
||||
@@ -75,40 +78,20 @@ public class AbstractAccessDecisionManagerTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testProperlyStoresListOfVoters() throws Exception {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
List list = new Vector();
|
||||
DenyVoter voter = new DenyVoter();
|
||||
DenyAgainVoter denyVoter = new DenyAgainVoter();
|
||||
list.add(voter);
|
||||
list.add(denyVoter);
|
||||
mock.setDecisionVoters(list);
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl(list);
|
||||
assertEquals(list.size(), mock.getDecisionVoters().size());
|
||||
}
|
||||
|
||||
public void testRejectsEmptyList() throws Exception {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
List list = new Vector();
|
||||
|
||||
try {
|
||||
mock.setDecisionVoters(list);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testRejectsListContainingInvalidObjectTypes() {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
List list = new Vector();
|
||||
DenyVoter voter = new DenyVoter();
|
||||
DenyAgainVoter denyVoter = new DenyAgainVoter();
|
||||
String notAVoter = "NOT_A_VOTER";
|
||||
list.add(voter);
|
||||
list.add(notAVoter);
|
||||
list.add(denyVoter);
|
||||
|
||||
try {
|
||||
mock.setDecisionVoters(list);
|
||||
new MockDecisionManagerImpl(list);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
@@ -116,10 +99,8 @@ public class AbstractAccessDecisionManagerTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testRejectsNullVotersList() throws Exception {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
|
||||
try {
|
||||
mock.setDecisionVoters(null);
|
||||
new MockDecisionManagerImpl(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
@@ -133,10 +114,8 @@ public class AbstractAccessDecisionManagerTests extends TestCase {
|
||||
|
||||
public void testWillNotStartIfDecisionVotersNotSet()
|
||||
throws Exception {
|
||||
MockDecisionManagerImpl mock = new MockDecisionManagerImpl();
|
||||
|
||||
try {
|
||||
mock.afterPropertiesSet();
|
||||
new MockDecisionManagerImpl(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
@@ -146,6 +125,10 @@ public class AbstractAccessDecisionManagerTests extends TestCase {
|
||||
//~ Inner Classes ==================================================================================================
|
||||
|
||||
private class MockDecisionManagerImpl extends AbstractAccessDecisionManager {
|
||||
protected MockDecisionManagerImpl(List<AccessDecisionVoter<? extends Object>> decisionVoters) {
|
||||
super(decisionVoters);
|
||||
}
|
||||
|
||||
public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,6 @@ public class AffirmativeBasedTests {
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setup() {
|
||||
mgr = new AffirmativeBased();
|
||||
|
||||
grant = mock(AccessDecisionVoter.class);
|
||||
abstain = mock(AccessDecisionVoter.class);
|
||||
@@ -61,32 +60,33 @@ public class AffirmativeBasedTests {
|
||||
|
||||
@Test
|
||||
public void oneAffirmativeVoteOneDenyVoteOneAbstainVoteGrantsAccess() throws Exception {
|
||||
mgr.setDecisionVoters(Arrays.<AccessDecisionVoter<? extends Object>>asList(grant, deny, abstain));
|
||||
|
||||
mgr = new AffirmativeBased(Arrays.<AccessDecisionVoter<? extends Object>>asList(grant, deny, abstain));
|
||||
mgr.afterPropertiesSet();
|
||||
mgr.decide(user, new Object(), attrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void oneDenyVoteOneAbstainVoteOneAffirmativeVoteGrantsAccess() throws Exception {
|
||||
mgr.setDecisionVoters(Arrays.<AccessDecisionVoter<? extends Object>>asList(deny, abstain, grant));
|
||||
mgr = new AffirmativeBased(Arrays.<AccessDecisionVoter<? extends Object>>asList(deny, abstain, grant));
|
||||
mgr.decide(user, new Object(), attrs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void oneAffirmativeVoteTwoAbstainVotesGrantsAccess() throws Exception {
|
||||
mgr.setDecisionVoters(Arrays.<AccessDecisionVoter<? extends Object>>asList(grant, abstain, abstain));
|
||||
mgr = new AffirmativeBased(Arrays.<AccessDecisionVoter<? extends Object>>asList(grant, abstain, abstain));
|
||||
mgr.decide(user, new Object(), attrs);
|
||||
}
|
||||
|
||||
@Test(expected=AccessDeniedException.class)
|
||||
public void oneDenyVoteTwoAbstainVotesDeniesAccess() throws Exception {
|
||||
mgr.setDecisionVoters(Arrays.<AccessDecisionVoter<? extends Object>>asList(deny, abstain, abstain));
|
||||
mgr = new AffirmativeBased(Arrays.<AccessDecisionVoter<? extends Object>>asList(deny, abstain, abstain));
|
||||
mgr.decide(user, new Object(), attrs);
|
||||
}
|
||||
|
||||
@Test(expected=AccessDeniedException.class)
|
||||
public void onlyAbstainVotesDeniesAccessWithDefault() throws Exception {
|
||||
mgr.setDecisionVoters(Arrays.<AccessDecisionVoter<? extends Object>>asList(abstain, abstain, abstain));
|
||||
mgr = new AffirmativeBased(Arrays.<AccessDecisionVoter<? extends Object>>asList(abstain, abstain, abstain));
|
||||
assertTrue(!mgr.isAllowIfAllAbstainDecisions()); // check default
|
||||
|
||||
mgr.decide(user, new Object(), attrs);
|
||||
@@ -94,7 +94,7 @@ public class AffirmativeBasedTests {
|
||||
|
||||
@Test
|
||||
public void testThreeAbstainVotesGrantsAccessIfAllowIfAllAbstainDecisionsIsSet() throws Exception {
|
||||
mgr.setDecisionVoters(Arrays.<AccessDecisionVoter<? extends Object>>asList(abstain, abstain, abstain));
|
||||
mgr = new AffirmativeBased(Arrays.<AccessDecisionVoter<? extends Object>>asList(abstain, abstain, abstain));
|
||||
mgr.setAllowIfAllAbstainDecisions(true);
|
||||
assertTrue(mgr.isAllowIfAllAbstainDecisions()); // check changed
|
||||
|
||||
|
||||
@@ -106,7 +106,6 @@ public class ConsensusBasedTests {
|
||||
}
|
||||
|
||||
private ConsensusBased makeDecisionManager() {
|
||||
ConsensusBased decisionManager = new ConsensusBased();
|
||||
RoleVoter roleVoter = new RoleVoter();
|
||||
DenyVoter denyForSureVoter = new DenyVoter();
|
||||
DenyAgainVoter denyAgainForSureVoter = new DenyAgainVoter();
|
||||
@@ -114,9 +113,8 @@ public class ConsensusBasedTests {
|
||||
voters.add(roleVoter);
|
||||
voters.add(denyForSureVoter);
|
||||
voters.add(denyAgainForSureVoter);
|
||||
decisionManager.setDecisionVoters(voters);
|
||||
|
||||
return decisionManager;
|
||||
return new ConsensusBased(voters);
|
||||
}
|
||||
|
||||
private TestingAuthenticationToken makeTestToken() {
|
||||
|
||||
@@ -39,7 +39,6 @@ public class UnanimousBasedTests extends TestCase {
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
private UnanimousBased makeDecisionManager() {
|
||||
UnanimousBased decisionManager = new UnanimousBased();
|
||||
RoleVoter roleVoter = new RoleVoter();
|
||||
DenyVoter denyForSureVoter = new DenyVoter();
|
||||
DenyAgainVoter denyAgainForSureVoter = new DenyAgainVoter();
|
||||
@@ -47,13 +46,10 @@ public class UnanimousBasedTests extends TestCase {
|
||||
voters.add(roleVoter);
|
||||
voters.add(denyForSureVoter);
|
||||
voters.add(denyAgainForSureVoter);
|
||||
decisionManager.setDecisionVoters(voters);
|
||||
|
||||
return decisionManager;
|
||||
return new UnanimousBased(voters);
|
||||
}
|
||||
|
||||
private UnanimousBased makeDecisionManagerWithFooBarPrefix() {
|
||||
UnanimousBased decisionManager = new UnanimousBased();
|
||||
RoleVoter roleVoter = new RoleVoter();
|
||||
roleVoter.setRolePrefix("FOOBAR_");
|
||||
|
||||
@@ -63,9 +59,7 @@ public class UnanimousBasedTests extends TestCase {
|
||||
voters.add(roleVoter);
|
||||
voters.add(denyForSureVoter);
|
||||
voters.add(denyAgainForSureVoter);
|
||||
decisionManager.setDecisionVoters(voters);
|
||||
|
||||
return decisionManager;
|
||||
return new UnanimousBased(voters);
|
||||
}
|
||||
|
||||
private TestingAuthenticationToken makeTestToken() {
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
package org.springframework.security.authentication;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Luke Taylor
|
||||
*/
|
||||
@SuppressWarnings({"deprecation"})
|
||||
public class AuthenticationDetailsSourceImplTests {
|
||||
|
||||
@Test
|
||||
public void buildDetailsReturnsExpectedAuthenticationDetails() {
|
||||
AuthenticationDetailsSourceImpl ads = new AuthenticationDetailsSourceImpl();
|
||||
AuthenticationDetails details = (AuthenticationDetails) ads.buildDetails("the context");
|
||||
assertEquals("the context", details.getContext());
|
||||
assertEquals(new AuthenticationDetails("the context"), details);
|
||||
ads.setClazz(AuthenticationDetails.class);
|
||||
details = (AuthenticationDetails) ads.buildDetails("another context");
|
||||
assertEquals("another context", details.getContext());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void nonMatchingConstructorIsRejected() {
|
||||
AuthenticationDetailsSourceImpl ads = new AuthenticationDetailsSourceImpl();
|
||||
ads.setClazz(String.class);
|
||||
ads.buildDetails(new Object());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void constructorTakingMultipleArgumentsIsRejected() {
|
||||
AuthenticationDetailsSourceImpl ads = new AuthenticationDetailsSourceImpl();
|
||||
ads.setClazz(TestingAuthenticationToken.class);
|
||||
ads.buildDetails(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationDetailsEqualsBehavesAsExpected() {
|
||||
AuthenticationDetails details = new AuthenticationDetails("the context");
|
||||
assertFalse((new AuthenticationDetails("different context")).equals(details));
|
||||
assertFalse((new AuthenticationDetails(null)).equals(details));
|
||||
assertFalse(details.equals(new AuthenticationDetails(null)));
|
||||
assertFalse(details.equals("a string"));
|
||||
// Just check toString() functions OK
|
||||
details.toString();
|
||||
(new AuthenticationDetails(null)).toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,35 +35,29 @@ public class DefaultAuthenticationEventPublisherTests {
|
||||
Exception cause = new Exception();
|
||||
Object extraInfo = new Object();
|
||||
publisher.publishAuthenticationFailure(new BadCredentialsException(""), a);
|
||||
publisher.publishAuthenticationFailure(new BadCredentialsException("", extraInfo), a);
|
||||
publisher.publishAuthenticationFailure(new BadCredentialsException("", cause), a);
|
||||
verify(appPublisher, times(3)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
|
||||
verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
|
||||
reset(appPublisher);
|
||||
publisher.publishAuthenticationFailure(new UsernameNotFoundException(""), a);
|
||||
publisher.publishAuthenticationFailure(new UsernameNotFoundException("", extraInfo), a);
|
||||
publisher.publishAuthenticationFailure(new UsernameNotFoundException("", cause), a);
|
||||
publisher.publishAuthenticationFailure(new AccountExpiredException(""), a);
|
||||
publisher.publishAuthenticationFailure(new AccountExpiredException("", extraInfo), a);
|
||||
publisher.publishAuthenticationFailure(new AccountExpiredException("", cause), a);
|
||||
publisher.publishAuthenticationFailure(new ProviderNotFoundException(""), a);
|
||||
publisher.publishAuthenticationFailure(new DisabledException(""), a);
|
||||
publisher.publishAuthenticationFailure(new DisabledException("", extraInfo), a);
|
||||
publisher.publishAuthenticationFailure(new DisabledException("", cause), a);
|
||||
publisher.publishAuthenticationFailure(new LockedException(""), a);
|
||||
publisher.publishAuthenticationFailure(new LockedException("", extraInfo), a);
|
||||
publisher.publishAuthenticationFailure(new LockedException("", cause), a);
|
||||
publisher.publishAuthenticationFailure(new AuthenticationServiceException(""), a);
|
||||
publisher.publishAuthenticationFailure(new AuthenticationServiceException("",cause), a);
|
||||
publisher.publishAuthenticationFailure(new CredentialsExpiredException(""), a);
|
||||
publisher.publishAuthenticationFailure(new CredentialsExpiredException("", extraInfo), a);
|
||||
publisher.publishAuthenticationFailure(new CredentialsExpiredException("", cause), a);
|
||||
verify(appPublisher, times(3)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
|
||||
verify(appPublisher, times(3)).publishEvent(isA(AuthenticationFailureExpiredEvent.class));
|
||||
verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
|
||||
verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureExpiredEvent.class));
|
||||
verify(appPublisher).publishEvent(isA(AuthenticationFailureProviderNotFoundEvent.class));
|
||||
verify(appPublisher, times(3)).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
|
||||
verify(appPublisher, times(3)).publishEvent(isA(AuthenticationFailureLockedEvent.class));
|
||||
verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
|
||||
verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureLockedEvent.class));
|
||||
verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureServiceExceptionEvent.class));
|
||||
verify(appPublisher, times(3)).publishEvent(isA(AuthenticationFailureCredentialsExpiredEvent.class));
|
||||
verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureCredentialsExpiredEvent.class));
|
||||
verifyNoMoreInteractions(appPublisher);
|
||||
}
|
||||
|
||||
|
||||
@@ -69,10 +69,9 @@ public class ProviderManagerTests {
|
||||
@Test
|
||||
public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() throws Exception {
|
||||
final Authentication a = mock(Authentication.class);
|
||||
ProviderManager mgr = new ProviderManager();
|
||||
ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichReturns(a)));
|
||||
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
||||
mgr.setAuthenticationEventPublisher(publisher);
|
||||
mgr.setProviders(Arrays.asList(createProviderWhichReturns(a)));
|
||||
|
||||
Authentication result = mgr.authenticate(a);
|
||||
assertEquals(a, result);
|
||||
@@ -82,37 +81,24 @@ public class ProviderManagerTests {
|
||||
@Test
|
||||
public void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates() {
|
||||
final Authentication a = mock(Authentication.class);
|
||||
ProviderManager mgr = new ProviderManager();
|
||||
ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichReturns(null), createProviderWhichReturns(a)));
|
||||
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
||||
mgr.setAuthenticationEventPublisher(publisher);
|
||||
mgr.setProviders(Arrays.asList(createProviderWhichReturns(null), createProviderWhichReturns(a)));
|
||||
|
||||
Authentication result = mgr.authenticate(a);
|
||||
assertSame(a, result);
|
||||
verify(publisher).publishAuthenticationSuccess(result);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void startupFailsIfProviderListDoesNotContainProviders() throws Exception {
|
||||
List<Object> providers = new ArrayList<Object>();
|
||||
providers.add("THIS_IS_NOT_A_PROVIDER");
|
||||
|
||||
ProviderManager mgr = new ProviderManager();
|
||||
|
||||
mgr.setProviders(providers);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testStartupFailsIfProvidersNotSet() throws Exception {
|
||||
ProviderManager mgr = new ProviderManager();
|
||||
mgr.afterPropertiesSet();
|
||||
new ProviderManager(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() throws Exception {
|
||||
Object requestDetails = "(Request Details)";
|
||||
final Object resultDetails = "(Result Details)";
|
||||
ProviderManager authMgr = makeProviderManager();
|
||||
|
||||
// A provider which sets the details object
|
||||
AuthenticationProvider provider = new AuthenticationProvider() {
|
||||
@@ -126,7 +112,7 @@ public class ProviderManagerTests {
|
||||
}
|
||||
};
|
||||
|
||||
authMgr.setProviders(Arrays.asList(provider));
|
||||
ProviderManager authMgr = new ProviderManager(Arrays.asList(provider));
|
||||
|
||||
TestingAuthenticationToken request = createAuthenticationToken();
|
||||
request.setDetails(requestDetails);
|
||||
@@ -150,35 +136,32 @@ public class ProviderManagerTests {
|
||||
|
||||
@Test
|
||||
public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() throws Exception {
|
||||
ProviderManager mgr = new ProviderManager();
|
||||
final Authentication authReq = mock(Authentication.class);
|
||||
mgr.setProviders(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", new Throwable())),
|
||||
ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", new Throwable())),
|
||||
createProviderWhichReturns(authReq)));
|
||||
assertSame(authReq, mgr.authenticate(mock(Authentication.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() throws Exception {
|
||||
ProviderManager mgr = new ProviderManager();
|
||||
|
||||
mgr.setProviders(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("", "extra")),
|
||||
ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(new BadCredentialsException("")),
|
||||
createProviderWhichReturns(null)));
|
||||
try {
|
||||
mgr.authenticate(mock(Authentication.class));
|
||||
fail("Expected BadCredentialsException");
|
||||
} catch (BadCredentialsException expected) {
|
||||
assertEquals("extra", expected.getExtraInformation());
|
||||
}
|
||||
}
|
||||
|
||||
// SEC-546
|
||||
@Test
|
||||
public void accountStatusExceptionPreventsCallsToSubsequentProviders() throws Exception {
|
||||
ProviderManager authMgr = makeProviderManager();
|
||||
AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(new AccountStatusException(""){});
|
||||
AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(new AccountStatusException("") {
|
||||
});
|
||||
AuthenticationProvider otherProvider = mock(AuthenticationProvider.class);
|
||||
|
||||
authMgr.setProviders(Arrays.asList(iThrowAccountStatusException, otherProvider));
|
||||
ProviderManager authMgr = new ProviderManager(Arrays.asList(iThrowAccountStatusException, otherProvider));
|
||||
|
||||
try {
|
||||
authMgr.authenticate(mock(Authentication.class));
|
||||
@@ -188,22 +171,6 @@ public class ProviderManagerTests {
|
||||
verifyZeroInteractions(otherProvider);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extraInformationIsClearedIfFlagIsSet() throws Exception {
|
||||
ProviderManager authMgr = makeProviderManager();
|
||||
AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(new AccountStatusException("", "extra"){});
|
||||
|
||||
authMgr.setProviders(Arrays.asList(iThrowAccountStatusException));
|
||||
authMgr.setClearExtraInformation(true);
|
||||
|
||||
try {
|
||||
authMgr.authenticate(mock(Authentication.class));
|
||||
fail("Expected AccountStatusException");
|
||||
} catch (AccountStatusException expected) {
|
||||
assertNull(expected.getExtraInformation());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parentAuthenticationIsUsedIfProvidersDontAuthenticate() throws Exception {
|
||||
AuthenticationManager parent = mock(AuthenticationManager.class);
|
||||
@@ -229,15 +196,15 @@ public class ProviderManagerTests {
|
||||
|
||||
@Test
|
||||
public void providerNotFoundFromParentIsIgnored() throws Exception {
|
||||
ProviderManager mgr = new ProviderManager();
|
||||
final Authentication authReq = mock(Authentication.class);
|
||||
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
|
||||
mgr.setAuthenticationEventPublisher(publisher);
|
||||
// Set a provider that throws an exception - this is the exception we expect to be propagated
|
||||
mgr.setProviders(Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))));
|
||||
AuthenticationManager parent = mock(AuthenticationManager.class);
|
||||
when(parent.authenticate(authReq)).thenThrow(new ProviderNotFoundException(""));
|
||||
mgr.setParent(parent);
|
||||
|
||||
// Set a provider that throws an exception - this is the exception we expect to be propagated
|
||||
ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))), parent);
|
||||
mgr.setAuthenticationEventPublisher(publisher);
|
||||
|
||||
try {
|
||||
mgr.authenticate(authReq);
|
||||
fail("Expected exception");
|
||||
@@ -262,7 +229,6 @@ public class ProviderManagerTests {
|
||||
fail("Expected exception");
|
||||
} catch (BadCredentialsException e) {
|
||||
assertSame(expected, e);
|
||||
assertSame(authReq, e.getAuthentication());
|
||||
}
|
||||
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
||||
}
|
||||
@@ -282,7 +248,6 @@ public class ProviderManagerTests {
|
||||
fail("Expected exception");
|
||||
} catch (LockedException e) {
|
||||
assertSame(expected, e);
|
||||
assertSame(authReq, e.getAuthentication());
|
||||
}
|
||||
verify(publisher).publishAuthenticationFailure(expected, authReq);
|
||||
}
|
||||
|
||||
@@ -37,8 +37,7 @@ public class AnonymousAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testDetectsAnInvalidKey() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider("qwerty");
|
||||
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("WRONG_KEY", "Test",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
@@ -52,10 +51,8 @@ public class AnonymousAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testDetectsMissingKey() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
|
||||
try {
|
||||
aap.afterPropertiesSet();
|
||||
new AnonymousAuthenticationProvider(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
@@ -64,16 +61,13 @@ public class AnonymousAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testGettersSetters() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
aap.afterPropertiesSet();
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider("qwerty");
|
||||
assertEquals("qwerty", aap.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoresClassesItDoesNotSupport() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider("qwerty");
|
||||
|
||||
TestingAuthenticationToken token = new TestingAuthenticationToken("user", "password", "ROLE_A");
|
||||
assertFalse(aap.supports(TestingAuthenticationToken.class));
|
||||
@@ -84,8 +78,7 @@ public class AnonymousAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testNormalOperation() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider("qwerty");
|
||||
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("qwerty", "Test",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
@@ -97,7 +90,7 @@ public class AnonymousAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testSupports() {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider("qwerty");
|
||||
assertTrue(aap.supports(AnonymousAuthenticationToken.class));
|
||||
assertFalse(aap.supports(TestingAuthenticationToken.class));
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@ public class DefaultJaasAuthenticationProviderTests {
|
||||
@Test
|
||||
public void publishNullPublisher() {
|
||||
provider.setApplicationEventPublisher(null);
|
||||
AuthenticationException ae = new BadCredentialsException("Failed to login", token);
|
||||
AuthenticationException ae = new BadCredentialsException("Failed to login");
|
||||
|
||||
provider.publishFailureEvent(token, ae);
|
||||
provider.publishSuccessEvent(token);
|
||||
|
||||
@@ -34,8 +34,7 @@ public class RememberMeAuthenticationProviderTests extends TestCase {
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void testDetectsAnInvalidKey() throws Exception {
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider("qwerty");
|
||||
|
||||
RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("WRONG_KEY", "Test",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
@@ -48,10 +47,8 @@ public class RememberMeAuthenticationProviderTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testDetectsMissingKey() throws Exception {
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
|
||||
|
||||
try {
|
||||
aap.afterPropertiesSet();
|
||||
new RememberMeAuthenticationProvider(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
@@ -59,15 +56,13 @@ public class RememberMeAuthenticationProviderTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testGettersSetters() throws Exception {
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider("qwerty");
|
||||
aap.afterPropertiesSet();
|
||||
assertEquals("qwerty", aap.getKey());
|
||||
}
|
||||
|
||||
public void testIgnoresClassesItDoesNotSupport() throws Exception {
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider("qwerty");
|
||||
|
||||
TestingAuthenticationToken token = new TestingAuthenticationToken("user", "password","ROLE_A");
|
||||
assertFalse(aap.supports(TestingAuthenticationToken.class));
|
||||
@@ -77,8 +72,7 @@ public class RememberMeAuthenticationProviderTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testNormalOperation() throws Exception {
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider("qwerty");
|
||||
|
||||
RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("qwerty", "Test",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
@@ -89,7 +83,7 @@ public class RememberMeAuthenticationProviderTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testSupports() {
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider();
|
||||
RememberMeAuthenticationProvider aap = new RememberMeAuthenticationProvider("qwerty");
|
||||
assertTrue(aap.supports(RememberMeAuthenticationToken.class));
|
||||
assertFalse(aap.supports(TestingAuthenticationToken.class));
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed 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
|
||||
*
|
||||
* 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.springframework.security.core.userdetails.memory;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link InMemoryDaoImpl}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
*/
|
||||
@SuppressWarnings({"deprecation"})
|
||||
public class InMemoryDaoTests extends TestCase {
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
private UserMap makeUserMap() {
|
||||
UserMapEditor editor = new UserMapEditor();
|
||||
editor.setAsText("rod=koala,ROLE_ONE,ROLE_TWO,enabled\nScott=wombat,ROLE_ONE,ROLE_TWO,enabled");
|
||||
|
||||
return (UserMap) editor.getValue();
|
||||
}
|
||||
|
||||
public void testLookupFails() throws Exception {
|
||||
InMemoryDaoImpl dao = new InMemoryDaoImpl();
|
||||
dao.setUserMap(makeUserMap());
|
||||
dao.afterPropertiesSet();
|
||||
|
||||
try {
|
||||
dao.loadUserByUsername("UNKNOWN_USER");
|
||||
fail("Should have thrown UsernameNotFoundException");
|
||||
} catch (UsernameNotFoundException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testLookupSuccess() throws Exception {
|
||||
InMemoryDaoImpl dao = new InMemoryDaoImpl();
|
||||
dao.setUserMap(makeUserMap());
|
||||
dao.afterPropertiesSet();
|
||||
assertEquals("koala", dao.loadUserByUsername("rod").getPassword());
|
||||
assertEquals("wombat", dao.loadUserByUsername("scott").getPassword());
|
||||
}
|
||||
|
||||
public void testLookupSuccessWithMixedCase() throws Exception {
|
||||
InMemoryDaoImpl dao = new InMemoryDaoImpl();
|
||||
dao.setUserMap(makeUserMap());
|
||||
dao.afterPropertiesSet();
|
||||
assertEquals("koala", dao.loadUserByUsername("rod").getPassword());
|
||||
assertEquals("wombat", dao.loadUserByUsername("ScOTt").getPassword());
|
||||
}
|
||||
|
||||
public void testStartupFailsIfUserMapNotSet() throws Exception {
|
||||
InMemoryDaoImpl dao = new InMemoryDaoImpl();
|
||||
|
||||
try {
|
||||
dao.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testStartupFailsIfUserMapSetToNull() throws Exception {
|
||||
InMemoryDaoImpl dao = new InMemoryDaoImpl();
|
||||
dao.setUserMap(null);
|
||||
|
||||
try {
|
||||
dao.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testStartupSuccessIfUserMapSet() throws Exception {
|
||||
InMemoryDaoImpl dao = new InMemoryDaoImpl();
|
||||
dao.setUserMap(makeUserMap());
|
||||
dao.afterPropertiesSet();
|
||||
assertEquals(2, dao.getUserMap().getUserCount());
|
||||
}
|
||||
|
||||
public void testUseOfExternalPropertiesObject() throws Exception {
|
||||
InMemoryDaoImpl dao = new InMemoryDaoImpl();
|
||||
Properties props = new Properties();
|
||||
props.put("rod", "koala,ROLE_ONE,ROLE_TWO,enabled");
|
||||
props.put("scott", "wombat,ROLE_ONE,ROLE_TWO,enabled");
|
||||
dao.setUserProperties(props);
|
||||
assertEquals("koala", dao.loadUserByUsername("rod").getPassword());
|
||||
assertEquals("wombat", dao.loadUserByUsername("scott").getPassword());
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed 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
|
||||
*
|
||||
* 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.springframework.security.core.userdetails.memory;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link UserMapEditor}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class UserMapEditorTests extends TestCase {
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void testConvertedIntoUserSuccessfullyWhenDisabled() {
|
||||
UserMapEditor editor = new UserMapEditor();
|
||||
editor.setAsText("rod=koala,ROLE_ONE,ROLE_TWO,disabled");
|
||||
|
||||
UserMap map = (UserMap) editor.getValue();
|
||||
assertTrue(!map.getUser("rod").isEnabled());
|
||||
}
|
||||
|
||||
public void testConvertedIntoUserSuccessfullyWhenEnabled() {
|
||||
UserMapEditor editor = new UserMapEditor();
|
||||
editor.setAsText("rod=koala,ROLE_ONE,ROLE_TWO");
|
||||
|
||||
UserMap map = (UserMap) editor.getValue();
|
||||
assertEquals("rod", map.getUser("rod").getUsername());
|
||||
assertEquals("koala", map.getUser("rod").getPassword());
|
||||
assertTrue(AuthorityUtils.authorityListToSet(map.getUser("rod").getAuthorities()).contains("ROLE_ONE"));
|
||||
assertTrue(AuthorityUtils.authorityListToSet(map.getUser("rod").getAuthorities()).contains("ROLE_TWO"));
|
||||
assertTrue(map.getUser("rod").isEnabled());
|
||||
}
|
||||
|
||||
public void testEmptyStringReturnsEmptyMap() {
|
||||
UserMapEditor editor = new UserMapEditor();
|
||||
editor.setAsText("");
|
||||
|
||||
UserMap map = (UserMap) editor.getValue();
|
||||
assertEquals(0, map.getUserCount());
|
||||
}
|
||||
|
||||
public void testMalformedStringReturnsEmptyMap() {
|
||||
UserMapEditor editor = new UserMapEditor();
|
||||
editor.setAsText("MALFORMED_STRING");
|
||||
|
||||
UserMap map = (UserMap) editor.getValue();
|
||||
assertEquals(0, map.getUserCount());
|
||||
}
|
||||
|
||||
public void testMultiUserParsing() {
|
||||
UserMapEditor editor = new UserMapEditor();
|
||||
editor.setAsText("rod=koala,ROLE_ONE,ROLE_TWO,enabled\r\nscott=wombat,ROLE_ONE,ROLE_TWO,enabled");
|
||||
|
||||
UserMap map = (UserMap) editor.getValue();
|
||||
assertEquals("rod", map.getUser("rod").getUsername());
|
||||
assertEquals("scott", map.getUser("scott").getUsername());
|
||||
}
|
||||
|
||||
public void testNullReturnsEmptyMap() {
|
||||
UserMapEditor editor = new UserMapEditor();
|
||||
editor.setAsText(null);
|
||||
|
||||
UserMap map = (UserMap) editor.getValue();
|
||||
assertEquals(0, map.getUserCount());
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed 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
|
||||
*
|
||||
* 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.springframework.security.core.userdetails.memory;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link UserMap}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class UserMapTests {
|
||||
@Test
|
||||
public void testAddAndRetrieveUser() {
|
||||
UserDetails rod = new User("rod", "koala", true, true, true, true,
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_TWO"));
|
||||
UserDetails scott = new User("scott", "wombat", true, true, true, true,
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_THREE"));
|
||||
UserDetails peter = new User("peter", "opal", true, true, true, true,
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_FOUR"));
|
||||
UserMap map = new UserMap();
|
||||
map.addUser(rod);
|
||||
map.addUser(scott);
|
||||
map.addUser(peter);
|
||||
assertEquals(3, map.getUserCount());
|
||||
|
||||
assertEquals(rod, map.getUser("rod"));
|
||||
assertEquals(scott, map.getUser("scott"));
|
||||
assertEquals(peter, map.getUser("peter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullUserCannotBeAdded() {
|
||||
UserMap map = new UserMap();
|
||||
assertEquals(0, map.getUserCount());
|
||||
|
||||
try {
|
||||
map.addUser(null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unknownUserIsNotRetrieved() {
|
||||
UserDetails rod = new User("rod", "koala", true, true, true, true,
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_TWO"));
|
||||
UserMap map = new UserMap();
|
||||
assertEquals(0, map.getUserCount());
|
||||
map.addUser(rod);
|
||||
assertEquals(1, map.getUserCount());
|
||||
|
||||
try {
|
||||
map.getUser("scott");
|
||||
fail("Should have thrown UsernameNotFoundException");
|
||||
} catch (UsernameNotFoundException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user