use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.
the class JdbcUserDetailsManagerTests method changePasswordFailsIfReAuthenticationFails.
@Test
public void changePasswordFailsIfReAuthenticationFails() {
insertJoe();
authenticateJoe();
AuthenticationManager am = mock(AuthenticationManager.class);
when(am.authenticate(any(Authentication.class))).thenThrow(new BadCredentialsException(""));
manager.setAuthenticationManager(am);
try {
manager.changePassword("password", "newPassword");
fail("Expected BadCredentialsException");
} catch (BadCredentialsException expected) {
}
// Check password hasn't changed.
UserDetails newJoe = manager.loadUserByUsername("joe");
assertThat(newJoe.getPassword()).isEqualTo("password");
assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials()).isEqualTo("password");
assertThat(cache.getUserMap().containsKey("joe")).isTrue();
}
use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.
the class JdbcUserDetailsManagerTests method updateUserChangesDataCorrectlyAndClearsCache.
@Test
public void updateUserChangesDataCorrectlyAndClearsCache() {
insertJoe();
User newJoe = new User("joe", "newpassword", false, true, true, true, AuthorityUtils.createAuthorityList(new String[] { "D", "F", "E" }));
manager.updateUser(newJoe);
UserDetails joe = manager.loadUserByUsername("joe");
assertThat(joe).isEqualTo(newJoe);
assertThat(cache.getUserMap().containsKey("joe")).isFalse();
}
use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.
the class JdbcUserDetailsManagerTests method createUserInsertsCorrectData.
@Test
public void createUserInsertsCorrectData() {
manager.createUser(joe);
UserDetails joe2 = manager.loadUserByUsername("joe");
assertThat(joe2).isEqualTo(joe);
}
use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.
the class LdapAuthenticationProviderTests method useWithNullAuthoritiesPopulatorReturnsCorrectRole.
@Test
public void useWithNullAuthoritiesPopulatorReturnsCorrectRole() {
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator());
LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
userMapper.setRoleAttributes(new String[] { "ou" });
ldapProvider.setUserDetailsContextMapper(userMapper);
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben", "benspassword");
UserDetails user = (UserDetails) ldapProvider.authenticate(authRequest).getPrincipal();
assertThat(user.getAuthorities()).hasSize(1);
assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_FROM_ENTRY");
}
use of org.springframework.security.core.userdetails.UserDetails in project spring-security by spring-projects.
the class OpenIDAuthenticationProviderTests method testAuthenticateSuccess.
/*
* Test method for
* 'org.springframework.security.authentication.openid.OpenIDAuthenticationProvider.
* authenticate(Authentication)'
*/
@Test
public void testAuthenticateSuccess() {
OpenIDAuthenticationProvider provider = new OpenIDAuthenticationProvider();
provider.setUserDetailsService(new MockUserDetailsService());
Authentication preAuth = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.SUCCESS, USERNAME, "", null);
assertThat(preAuth.isAuthenticated()).isFalse();
Authentication postAuth = provider.authenticate(preAuth);
assertThat(postAuth).isNotNull();
assertThat(postAuth instanceof OpenIDAuthenticationToken).isTrue();
assertThat(postAuth.isAuthenticated()).isTrue();
assertThat(postAuth.getPrincipal()).isNotNull();
assertThat(postAuth.getPrincipal() instanceof UserDetails).isTrue();
assertThat(postAuth.getAuthorities()).isNotNull();
assertThat(postAuth.getAuthorities().size() > 0).isTrue();
assertThat(((OpenIDAuthenticationToken) postAuth).getStatus() == OpenIDAuthenticationStatus.SUCCESS).isTrue();
assertThat(((OpenIDAuthenticationToken) postAuth).getMessage() == null).isTrue();
}
Aggregations