Search in sources :

Example 76 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class SecurityAutoConfigurationTests method testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser.

@Test
public void testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(AuthenticationManagerCustomizer.class, SecurityAutoConfiguration.class);
    this.context.refresh();
    SecurityProperties security = this.context.getBean(SecurityProperties.class);
    AuthenticationManager manager = this.context.getBean(AuthenticationManager.class);
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(security.getUser().getName(), security.getUser().getPassword());
    try {
        manager.authenticate(token);
        fail("Expected Exception");
    } catch (AuthenticationException success) {
    // Expected
    }
    token = new UsernamePasswordAuthenticationToken("foo", "bar");
    assertThat(manager.authenticate(token)).isNotNull();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 77 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class SecurityAutoConfigurationTests method testWebConfiguration.

@Test
public void testWebConfiguration() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(SecurityAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    assertThat(this.context.getBean(AuthenticationManagerBuilder.class)).isNotNull();
    // 1 for static resources and one for the rest
    assertThat(this.context.getBean(FilterChainProxy.class).getFilterChains()).hasSize(2);
}
Also used : FilterChainProxy(org.springframework.security.web.FilterChainProxy) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 78 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class SecurityAutoConfigurationTests method testAuthenticationManagerCreated.

@Test
public void testAuthenticationManagerCreated() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(SecurityAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    assertThat(this.context.getBean(AuthenticationManager.class)).isNotNull();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 79 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project spring-boot by spring-projects.

the class SecurityAutoConfigurationTests method testOverrideAuthenticationManagerWithBuilderAndInjectBuilderIntoSecurityFilter.

@Test
public void testOverrideAuthenticationManagerWithBuilderAndInjectBuilderIntoSecurityFilter() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(AuthenticationManagerCustomizer.class, WorkaroundSecurityCustomizer.class, SecurityAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken("foo", "bar", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    assertThat(this.context.getBean(AuthenticationManager.class).authenticate(user)).isNotNull();
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 80 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project cas by apereo.

the class SamlMetadataUIParserActionTests method verifyEntityIdUIInfoExists.

@Test
public void verifyEntityIdUIInfoExists() throws Exception {
    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(SamlProtocolConstants.PARAMETER_ENTITY_ID, "https://carmenwiki.osu.edu/shibboleth");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockServletContext sCtx = new MockServletContext();
    ctx.setExternalContext(new ServletExternalContext(sCtx, request, response));
    ctx.getFlowScope().put(CasProtocolConstants.PARAMETER_SERVICE, RegisteredServiceTestUtils.getService());
    samlMetadataUIParserAction.execute(ctx);
    assertNotNull(WebUtils.getServiceUserInterfaceMetadata(ctx, SamlMetadataUIInfo.class));
}
Also used : SamlMetadataUIInfo(org.apereo.cas.support.saml.mdui.SamlMetadataUIInfo) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.MockServletContext)186 Test (org.junit.Test)135 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)82 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)80 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)74 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)54 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Event (org.springframework.webflow.execution.Event)18 Before (org.junit.Before)16 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)14 MockMvc (org.springframework.test.web.servlet.MockMvc)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8 MockFilterChain (org.springframework.mock.web.MockFilterChain)8 MockHttpSession (org.springframework.mock.web.MockHttpSession)8 Map (java.util.Map)7 Credential (org.apereo.cas.authentication.Credential)7 Cookie (javax.servlet.http.Cookie)6 FilterChainProxy (org.springframework.security.web.FilterChainProxy)6