Search in sources :

Example 21 with MockServletContext

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

the class ManagementWebSecurityAutoConfigurationTests method testWebConfiguration.

@Test
public void testWebConfiguration() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(SecurityAutoConfiguration.class, WebMvcAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, AuditAutoConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false");
    this.context.refresh();
    assertThat(this.context.getBean(AuthenticationManagerBuilder.class)).isNotNull();
    FilterChainProxy filterChainProxy = this.context.getBean(FilterChainProxy.class);
    // 1 for static resources, one for management endpoints and one for the rest
    assertThat(filterChainProxy.getFilterChains()).hasSize(3);
    assertThat(filterChainProxy.getFilters("/beans")).isNotEmpty();
    assertThat(filterChainProxy.getFilters("/beans/")).isNotEmpty();
    assertThat(filterChainProxy.getFilters("/beans.foo")).isNotEmpty();
    assertThat(filterChainProxy.getFilters("/beans/foo/bar")).isNotEmpty();
}
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 22 with MockServletContext

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

the class ManagementWebSecurityAutoConfigurationTests method testDisableBasicAuthOnApplicationPaths.

@Test
public void testDisableBasicAuthOnApplicationPaths() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(WebConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false");
    this.context.refresh();
    // Just the management endpoints (one filter) and ignores now plus the backup
    // filter on app endpoints
    assertThat(this.context.getBean(FilterChainProxy.class).getFilterChains()).hasSize(3);
}
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 23 with MockServletContext

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

the class ManagementWebSecurityAutoConfigurationTests method testOverrideAuthenticationManager.

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

Example 24 with MockServletContext

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

the class ManagementWebSecurityAutoConfigurationTests method testMarkAllEndpointsSensitive.

@Test
public void testMarkAllEndpointsSensitive() throws Exception {
    // gh-4368
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(WebConfiguration.class);
    EnvironmentTestUtils.addEnvironment(this.context, "endpoints.sensitive:true");
    this.context.refresh();
    MockMvc mockMvc = //
    MockMvcBuilders.webAppContextSetup(this.context).apply(//
    springSecurity()).build();
    //
    mockMvc.perform(//
    get("/health")).andExpect(status().isUnauthorized());
    //
    mockMvc.perform(//
    get("/info")).andExpect(status().isUnauthorized());
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 25 with MockServletContext

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

the class ManagementWebSecurityAutoConfigurationTests method realmSameForManagement.

// gh-2466
@Test
public void realmSameForManagement() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(AuthenticationConfig.class, SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class, JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, AuditAutoConfiguration.class);
    this.context.refresh();
    Filter filter = this.context.getBean("springSecurityFilterChain", Filter.class);
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).addFilters(filter).build();
    // no user (Main)
    mockMvc.perform(MockMvcRequestBuilders.get("/home")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
    // invalid user (Main)
    mockMvc.perform(MockMvcRequestBuilders.get("/home").header("authorization", "Basic xxx")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
    // no user (Management)
    mockMvc.perform(MockMvcRequestBuilders.get("/beans")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
    // invalid user (Management)
    mockMvc.perform(MockMvcRequestBuilders.get("/beans").header("authorization", "Basic xxx")).andExpect(MockMvcResultMatchers.status().isUnauthorized()).andExpect(springAuthenticateRealmHeader());
}
Also used : Filter(javax.servlet.Filter) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.MockServletContext)173 Test (org.junit.Test)126 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)79 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)71 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)63 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)43 MockRequestContext (org.springframework.webflow.test.MockRequestContext)38 Before (org.junit.Before)14 MockMvc (org.springframework.test.web.servlet.MockMvc)13 Event (org.springframework.webflow.execution.Event)11 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)10 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 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)7 FilterChainProxy (org.springframework.security.web.FilterChainProxy)6 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)6