Search in sources :

Example 1 with ResultHandler

use of org.springframework.test.web.servlet.ResultHandler in project spring-security by spring-projects.

the class ConcurrentSessionManagementTests method maxConcurrentLoginsValueIsRespected.

@Test
public void maxConcurrentLoginsValueIsRespected() throws Exception {
    final MockHttpSession session1 = new MockHttpSession();
    final MockHttpSession session2 = new MockHttpSession();
    MockMvc mockMvc = createMockMvc("classpath:/spring/http-security-concurrency.xml", "classpath:/spring/in-memory-provider.xml", "classpath:/spring/testapp-servlet.xml");
    mockMvc.perform(get("secure/index").session(session1)).andExpect(status().is3xxRedirection());
    MockHttpServletRequestBuilder login1 = login().session(session1);
    mockMvc.perform(login1).andExpect(authenticated().withUsername("jimi"));
    MockHttpServletRequestBuilder login2 = login().session(session2);
    mockMvc.perform(login2).andExpect(redirectedUrl("/login.jsp?login_error=true"));
    Exception exception = (Exception) session2.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
    assertThat(exception).isNotNull();
    assertThat(exception.getMessage()).contains("Maximum sessions of 1 for this principal exceeded");
    // Now logout to kill first session
    mockMvc.perform(post("/logout").with(csrf())).andExpect(status().is3xxRedirection()).andDo(new ResultHandler() {

        @SuppressWarnings("serial")
        @Override
        public void handle(MvcResult result) throws Exception {
            context.publishEvent(new SessionDestroyedEvent(session1) {

                @Override
                public List<SecurityContext> getSecurityContexts() {
                    return Collections.emptyList();
                }

                @Override
                public String getId() {
                    return session1.getId();
                }
            });
        }
    });
    // Try second session again
    login2 = login().session(session2);
    mockMvc.perform(login2).andExpect(authenticated().withUsername("jimi"));
    mockMvc.perform(get("/secure/index").session(session2)).andExpect(content().string(containsString("A Secure Page")));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) SecurityContext(org.springframework.security.core.context.SecurityContext) MockHttpSession(org.springframework.mock.web.MockHttpSession) ResultHandler(org.springframework.test.web.servlet.ResultHandler) SessionDestroyedEvent(org.springframework.security.core.session.SessionDestroyedEvent) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 2 with ResultHandler

use of org.springframework.test.web.servlet.ResultHandler in project spring-boot by spring-projects.

the class EndpointDocumentation method endpoints.

@Test
public void endpoints() throws Exception {
    final File docs = new File("src/main/asciidoc");
    final Map<String, Object> model = new LinkedHashMap<>();
    final List<EndpointDoc> endpoints = new ArrayList<>();
    model.put("endpoints", endpoints);
    for (MvcEndpoint endpoint : getEndpoints()) {
        final String endpointPath = (StringUtils.hasText(endpoint.getPath()) ? endpoint.getPath() : "/");
        if (!SKIPPED.contains(endpointPath)) {
            String output = endpointPath.substring(1);
            output = output.length() > 0 ? output : "./";
            this.mockMvc.perform(get(endpointPath).accept(ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON)).andExpect(status().isOk()).andDo(document(output)).andDo(new ResultHandler() {

                @Override
                public void handle(MvcResult mvcResult) throws Exception {
                    EndpointDoc endpoint = new EndpointDoc(docs, endpointPath);
                    endpoints.add(endpoint);
                }
            });
        }
    }
    File file = new File(RESTDOCS_OUTPUT_DIR + "/endpoints.adoc");
    file.getParentFile().mkdirs();
    PrintWriter writer = new PrintWriter(file, "UTF-8");
    try {
        Template template = this.templates.createTemplate(new File("src/restdoc/resources/templates/endpoints.adoc.tpl"));
        template.make(model).writeTo(writer);
    } finally {
        writer.close();
    }
}
Also used : ArrayList(java.util.ArrayList) ResultHandler(org.springframework.test.web.servlet.ResultHandler) MvcResult(org.springframework.test.web.servlet.MvcResult) LinkedHashMap(java.util.LinkedHashMap) Template(groovy.text.Template) MvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 MvcResult (org.springframework.test.web.servlet.MvcResult)2 ResultHandler (org.springframework.test.web.servlet.ResultHandler)2 Template (groovy.text.Template)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 MvcEndpoint (org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint)1 MockHttpSession (org.springframework.mock.web.MockHttpSession)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1 SessionDestroyedEvent (org.springframework.security.core.session.SessionDestroyedEvent)1 MockMvc (org.springframework.test.web.servlet.MockMvc)1 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)1