Polish LdapUserDetailsManagerModifyPasswordTests

Issue gh-3392
This commit is contained in:
Josh Cummings 2019-08-07 14:37:46 -06:00
parent 65f6025cef
commit 3a831820f6
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
1 changed files with 14 additions and 30 deletions

View File

@ -17,14 +17,11 @@ package org.springframework.security.ldap.userdetails;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.ContextSource;
@ -33,9 +30,9 @@ import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource; import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.ldap.SpringSecurityLdapTemplate; import org.springframework.security.ldap.SpringSecurityLdapTemplate;
import org.springframework.security.ldap.server.UnboundIdContainer; import org.springframework.security.ldap.server.UnboundIdContainer;
import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatCode;
@ -46,30 +43,22 @@ import static org.assertj.core.api.Assertions.assertThatCode;
* *
* @author Josh Cummings * @author Josh Cummings
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringRunner.class)
@SecurityTestExecutionListeners @ContextConfiguration(classes=LdapUserDetailsManagerModifyPasswordTests.UnboundIdContainerConfiguration.class)
public class LdapUserDetailsManagerModifyPasswordTests { public class LdapUserDetailsManagerModifyPasswordTests {
ConfigurableApplicationContext context;
LdapUserDetailsManager userDetailsManager; LdapUserDetailsManager userDetailsManager;
@Autowired
ContextSource contextSource; ContextSource contextSource;
@Before @Before
public void setup() { public void setup() {
this.context = new AnnotationConfigApplicationContext(ContainerConfiguration.class, LdapConfiguration.class);
this.contextSource = this.context.getBean(ContextSource.class);
this.userDetailsManager = new LdapUserDetailsManager(this.contextSource); this.userDetailsManager = new LdapUserDetailsManager(this.contextSource);
this.userDetailsManager.setUsePasswordModifyExtensionOperation(true); this.userDetailsManager.setUsePasswordModifyExtensionOperation(true);
this.userDetailsManager.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid")); this.userDetailsManager.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=people", "uid"));
} }
@After
public void teardown() {
this.context.close();
}
@Test @Test
@WithMockUser(username="bob", password="bobspassword", authorities="ROLE_USER") @WithMockUser(username="bob", password="bobspassword", authorities="ROLE_USER")
public void changePasswordWhenOldPasswordIsIncorrectThenThrowsException() { public void changePasswordWhenOldPasswordIsIncorrectThenThrowsException() {
@ -91,19 +80,8 @@ public class LdapUserDetailsManagerModifyPasswordTests {
} }
@Configuration @Configuration
static class LdapConfiguration { static class UnboundIdContainerConfiguration {
@Autowired UnboundIdContainer container; private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
@Bean
ContextSource contextSource() throws Exception {
return new DefaultSpringSecurityContextSource("ldap://127.0.0.1:"
+ this.container.getPort() + "/dc=springframework,dc=org");
}
}
@Configuration
static class ContainerConfiguration {
UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
"classpath:test-server.ldif"); "classpath:test-server.ldif");
@Bean @Bean
@ -112,6 +90,12 @@ public class LdapUserDetailsManagerModifyPasswordTests {
return this.container; return this.container;
} }
@Bean
ContextSource contextSource(UnboundIdContainer container) {
return new DefaultSpringSecurityContextSource("ldap://127.0.0.1:"
+ container.getPort() + "/dc=springframework,dc=org");
}
@PreDestroy @PreDestroy
void shutdown() { void shutdown() {
this.container.stop(); this.container.stop();