AuthenticationManagerBeanDefinitionParserTests uses SpringTestContext

Issue: gh-4870
This commit is contained in:
Rob Winch 2017-11-21 15:08:36 -06:00
parent 4d8f11a5a9
commit 5cf2883afc
1 changed files with 16 additions and 11 deletions

View File

@ -20,14 +20,18 @@ import static org.assertj.core.api.Assertions.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestRule;
import org.springframework.security.config.util.InMemoryXmlApplicationContext;
import org.springframework.security.util.FieldUtils;
@ -42,18 +46,21 @@ public class AuthenticationManagerBeanDefinitionParserTests {
+ " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A,ROLE_B' />"
+ " </user-service>" + " </authentication-provider>"
+ "</authentication-manager>";
private AbstractXmlApplicationContext appContext;
@Rule
public final SpringTestRule spring = new SpringTestRule();
@Test
// SEC-1225
public void providersAreRegisteredAsTopLevelBeans() throws Exception {
setContext(CONTEXT);
assertThat(appContext.getBeansOfType(AuthenticationProvider.class)).hasSize(1);
ConfigurableApplicationContext context = this.spring.context(CONTEXT)
.getContext();
assertThat(context.getBeansOfType(AuthenticationProvider.class)).hasSize(1);
}
@Test
public void eventsArePublishedByDefault() throws Exception {
setContext(CONTEXT);
ConfigurableApplicationContext appContext = this.spring.context(CONTEXT)
.getContext();
AuthListener listener = new AuthListener();
appContext.addApplicationListener(listener);
@ -69,7 +76,8 @@ public class AuthenticationManagerBeanDefinitionParserTests {
@Test
public void credentialsAreClearedByDefault() throws Exception {
setContext(CONTEXT);
ConfigurableApplicationContext appContext = this.spring.context(CONTEXT)
.getContext();
ProviderManager pm = (ProviderManager) appContext
.getBeansOfType(ProviderManager.class).values().toArray()[0];
assertThat(pm.isEraseCredentialsAfterAuthentication()).isTrue();
@ -77,22 +85,19 @@ public class AuthenticationManagerBeanDefinitionParserTests {
@Test
public void clearCredentialsPropertyIsRespected() throws Exception {
setContext("<authentication-manager erase-credentials='false'/>");
ConfigurableApplicationContext appContext = this.spring.context("<authentication-manager erase-credentials='false'/>")
.getContext();
ProviderManager pm = (ProviderManager) appContext
.getBeansOfType(ProviderManager.class).values().toArray()[0];
assertThat(pm.isEraseCredentialsAfterAuthentication()).isFalse();
}
private void setContext(String context) {
appContext = new InMemoryXmlApplicationContext(context);
}
private static class AuthListener implements
ApplicationListener<AbstractAuthenticationEvent> {
List<AbstractAuthenticationEvent> events = new ArrayList<AbstractAuthenticationEvent>();
public void onApplicationEvent(AbstractAuthenticationEvent event) {
events.add(event);
this.events.add(event);
}
}
}