Search in sources :

Example 6 with MockHttpSession

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

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method setUp.

@Before
public void setUp() {
    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 7 with MockHttpSession

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

the class ConcurrentSessionFilterTests method doFilterWhenOverrideThenCustomRedirectStrategyUsed.

@Test
public void doFilterWhenOverrideThenCustomRedirectStrategyUsed() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RedirectStrategy redirect = mock(RedirectStrategy.class);
    SessionRegistry registry = mock(SessionRegistry.class);
    SessionInformation information = new SessionInformation("user", "sessionId", new Date(System.currentTimeMillis() - 1000));
    information.expireNow();
    when(registry.getSessionInformation(anyString())).thenReturn(information);
    final String expiredUrl = "/expired";
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl + "will-be-overrridden") {

        /* (non-Javadoc)
					 * @see org.springframework.security.web.session.ConcurrentSessionFilter#determineExpiredUrl(javax.servlet.http.HttpServletRequest, org.springframework.security.core.session.SessionInformation)
					 */
        @Override
        protected String determineExpiredUrl(HttpServletRequest request, SessionInformation info) {
            return expiredUrl;
        }
    };
    filter.setRedirectStrategy(redirect);
    filter.doFilter(request, response, new MockFilterChain());
    verify(redirect).sendRedirect(request, response, expiredUrl);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SessionInformation(org.springframework.security.core.session.SessionInformation) SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) Matchers.anyString(org.mockito.Matchers.anyString) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 8 with MockHttpSession

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

the class ConcurrentSessionFilterTests method doFilterWhenCustomRedirectStrategyThenCustomRedirectStrategyUsed.

@Test
public void doFilterWhenCustomRedirectStrategyThenCustomRedirectStrategyUsed() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RedirectStrategy redirect = mock(RedirectStrategy.class);
    SessionRegistry registry = mock(SessionRegistry.class);
    SessionInformation information = new SessionInformation("user", "sessionId", new Date(System.currentTimeMillis() - 1000));
    information.expireNow();
    when(registry.getSessionInformation(anyString())).thenReturn(information);
    String expiredUrl = "/expired";
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl);
    filter.setRedirectStrategy(redirect);
    filter.doFilter(request, response, new MockFilterChain());
    verify(redirect).sendRedirect(request, response, expiredUrl);
}
Also used : SessionInformation(org.springframework.security.core.session.SessionInformation) SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) Matchers.anyString(org.mockito.Matchers.anyString) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 9 with MockHttpSession

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

the class ConcurrentSessionFilterTests method doFilterWhenCustomLogoutHandlersThenHandlersUsed.

@Test
public void doFilterWhenCustomLogoutHandlersThenHandlersUsed() throws Exception {
    LogoutHandler handler = mock(LogoutHandler.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    SessionRegistry registry = mock(SessionRegistry.class);
    SessionInformation information = new SessionInformation("user", "sessionId", new Date(System.currentTimeMillis() - 1000));
    information.expireNow();
    when(registry.getSessionInformation(anyString())).thenReturn(information);
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry);
    filter.setLogoutHandlers(new LogoutHandler[] { handler });
    filter.doFilter(request, response, new MockFilterChain());
    verify(handler).logout(eq(request), eq(response), any(Authentication.class));
}
Also used : SessionInformation(org.springframework.security.core.session.SessionInformation) SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpSession(org.springframework.mock.web.MockHttpSession) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) SecurityContextLogoutHandler(org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler) LogoutHandler(org.springframework.security.web.authentication.logout.LogoutHandler) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 10 with MockHttpSession

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

the class ConcurrentSessionFilterTests method returnsExpectedMessageWhenNoExpiredUrlSet.

// As above, but with no expiredUrl set.
@Test
public void returnsExpectedMessageWhenNoExpiredUrlSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    MockHttpServletResponse response = new MockHttpServletResponse();
    SessionRegistry registry = new SessionRegistryImpl();
    registry.registerNewSession(session.getId(), "principal");
    registry.getSessionInformation(session.getId()).expireNow();
    ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry);
    FilterChain fc = mock(FilterChain.class);
    filter.doFilter(request, response, fc);
    verifyZeroInteractions(fc);
    assertThat(response.getContentAsString()).isEqualTo("This session has been expired (possibly due to multiple concurrent logins being " + "attempted as the same user).");
}
Also used : SessionRegistry(org.springframework.security.core.session.SessionRegistry) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SessionRegistryImpl(org.springframework.security.core.session.SessionRegistryImpl) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpSession(org.springframework.mock.web.MockHttpSession) ConcurrentSessionFilter(org.springframework.security.web.session.ConcurrentSessionFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

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