Search in sources :

Example 71 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-security-oauth by spring-projects.

the class HttpSessionOAuthRememberMeServicesTests method testEmptySession.

@Test
public void testEmptySession() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = oAuthRememberMeService.loadRememberedTokens(request, response);
    Assert.assertNull(tokens);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 72 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-security-oauth by spring-projects.

the class HttpSessionOAuthRememberMeServicesTests method testStoreEverything.

@Test
public void testStoreEverything() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(false);
        tokens.put("resourceID1", token);
    }
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(true);
        tokens.put("resourceID2", token);
    }
    oAuthRememberMeService.rememberTokens(tokens, request, response);
    Assert.assertEquals(1, oAuthRememberMeService.loadRememberedTokens(request, response).size());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 73 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-security-oauth by spring-projects.

the class HttpSessionOAuthRememberMeServicesTests method testStoreRequestTokensOnly.

@Test
public void testStoreRequestTokensOnly() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(false);
        tokens.put("resourceID1", token);
    }
    {
        OAuthConsumerToken token = new OAuthConsumerToken();
        token.setAccessToken(true);
        tokens.put("resourceID2", token);
    }
    oAuthRememberMeService.rememberTokens(tokens, request, response);
    Map<String, OAuthConsumerToken> storedTokens = oAuthRememberMeService.loadRememberedTokens(request, response);
    Assert.assertEquals(1, storedTokens.size());
    Assert.assertNotNull(storedTokens.get("resourceID1"));
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Example 74 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests method setUp.

@Before
public void setUp() {
    this.oldRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
    this.newRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

Example 75 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession 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)

Aggregations

MockHttpSession (org.springframework.mock.web.MockHttpSession)106 Test (org.junit.Test)84 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)34 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)23 HashMap (java.util.HashMap)13 AbstractWebApiTest (org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)13 MockFilterChain (org.springframework.mock.web.MockFilterChain)12 FieldDescriptor (org.springframework.restdocs.payload.FieldDescriptor)11 ModelAndView (org.springframework.web.servlet.ModelAndView)11 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)9 FilterChain (javax.servlet.FilterChain)8 MockServletContext (org.springframework.mock.web.MockServletContext)8 SessionRegistry (org.springframework.security.core.session.SessionRegistry)8 ConcurrentSessionFilter (org.springframework.security.web.session.ConcurrentSessionFilter)8 Principal (org.apereo.cas.authentication.principal.Principal)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 CasProfile (org.pac4j.cas.profile.CasProfile)7 InputStream (java.io.InputStream)6 Before (org.junit.Before)6