Package com.mockrunner.mock.web

Examples of com.mockrunner.mock.web.MockFilterChain


            LOGGER.info("Cas proxy callback: " + uri.toString());
            String query = uri.getQuery();

            MockHttpServletRequest request = createRequest(GeoServerCasConstants.CAS_PROXY_RECEPTOR_PATTERN);
            MockHttpServletResponse response = new MockHttpServletResponse();
            MockFilterChain chain = new MockFilterChain();

            // CAS sends the callback twice, the first time without parameters
            if (query != null) {
                request.setQueryString(query);
                String[] kvps = query.split("&");
View Full Code Here


            in.close();

            MockHttpServletRequest request = createRequest(service);
            request.setMethod("POST");
            MockHttpServletResponse response = new MockHttpServletResponse();
            MockFilterChain chain = new MockFilterChain();
            String paramValue = URLDecoder.decode(buff.toString(), "utf-8");
            request.setupAddParameter("logoutRequest",
                    paramValue.substring(paramValue.indexOf("=") + 1));
            try {
                GeoServerSecurityFilterChainProxy proxy = getProxy();
View Full Code Here

        SecurityContextHolder.getContext().setAuthentication(null);

        // Test entry point
        MockHttpServletRequest request = createRequest("/foo/bar");
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);

        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertTrue(response.wasRedirectSent());
        String redirectURL = response.getHeader("Location");
        assertTrue(redirectURL.contains(GeoServerCasConstants.LOGIN_URI));
        assertTrue(redirectURL.endsWith("bar"));

        // test success
        String username = "castest";
        String password = username;
        CasFormAuthenticationHelper helper = new CasFormAuthenticationHelper(casServerURLPrefix,
                username, password);
        helper.ssoLogin();
       
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        String ticket =loginUsingTicket(helper, request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertFalse(response.wasRedirectSent());


        SecurityContext ctx = (SecurityContext) request.getSession(false).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertNotNull(ctx);
        Authentication auth = ctx.getAuthentication();
        assertNotNull(auth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        checkForAuthenticatedRole(auth);
        assertEquals(username, auth.getPrincipal());
        assertTrue(auth.getAuthorities().contains(new GeoServerRole(rootRole)));
        assertTrue(auth.getAuthorities().contains(new GeoServerRole(derivedRole)));
        assertNotNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
                .removeSessionByMappingId(ticket));
        helper.ssoLogout();

        // check unknown user
        username = "unknown";
        password = username;
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
        helper.ssoLogin();
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        ticket =loginUsingTicket(helper, request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertFalse(response.wasRedirectSent());

       
        ctx = (SecurityContext) request.getSession(true).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertNotNull(ctx);
        auth = ctx.getAuthentication();
        assertNotNull(auth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        checkForAuthenticatedRole(ctx.getAuthentication());
        assertEquals(username, auth.getPrincipal());
        assertEquals(1, auth.getAuthorities().size());
        assertNotNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
                .removeSessionByMappingId(ticket));
        helper.ssoLogout();

        // test root user
        username = GeoServerUser.ROOT_USERNAME;
        password = username;
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
        helper.ssoLogin();

        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        ticket =loginUsingTicket(helper, request, response, chain);
        ctx = (SecurityContext) request.getSession(true).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertFalse(response.wasRedirectSent());
        auth = ctx.getAuthentication();
        assertNotNull(auth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        // checkForAuthenticatedRole(auth);
        assertEquals(GeoServerUser.ROOT_USERNAME, auth.getPrincipal());
        assertTrue(auth.getAuthorities().size() == 1);
        assertTrue(auth.getAuthorities().contains(GeoServerRole.ADMIN_ROLE));
        assertNotNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
                .removeSessionByMappingId(ticket));
        helper.ssoLogout();

        // check disabled user
        username = "castest";
        password = username;
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
        helper.ssoLogin();
        updateUser("ug1", username, false);
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        ticket =loginUsingTicket(helper, request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertTrue(response.wasRedirectSent());
        redirectURL = response.getHeader("Location");
        assertTrue(redirectURL.contains("login"));
        ctx = (SecurityContext) request.getSession(true).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertNull(ctx);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        assertNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
                .removeSessionByMappingId(ticket));
        updateUser("ug1", username, true);
        helper.ssoLogout();

        insertAnonymousFilter();
        request = createRequest("foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        // Anonymous context is not stored in http session, no further testing
        removeAnonymousFilter();

        // test invalid ticket

        username = "castest";
        password = username;
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
        helper.ssoLogin();

        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString()));
        ticket += "ST-A";
        request.setupAddParameter("ticket", ticket);
        request.setQueryString("ticket=" + ticket);

        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertTrue(response.wasRedirectSent());
        redirectURL = response.getHeader("Location");
        assertTrue(redirectURL.contains(GeoServerCasConstants.LOGIN_URI));       
        ctx = (SecurityContext) request.getSession(true).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertNull(ctx);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        assertNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
                .removeSessionByMappingId(ticket));
        helper.ssoLogout();

        // test success with proxy granting ticket
        config.setProxyCallbackUrlPrefix(proxyCallbackUrlPrefix.toString());
        getSecurityManager().saveFilter(config);

        username = "castest";
        password = username;
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
        helper.ssoLogin();

        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString()));
        request.setupAddParameter("ticket", ticket);
        request.setQueryString("ticket=" + ticket);
        getProxy().doFilter(request, response, chain);
View Full Code Here

                username, password);
        helper.ssoLogin();
       
        MockHttpServletRequest request = createRequest(pattern);
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain  chain = new MockFilterChain();
        loginUsingTicket(helper, request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertFalse(response.wasRedirectSent());


        SecurityContext ctx = (SecurityContext) request.getSession(false).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertNotNull(ctx);
        Authentication auth = ctx.getAuthentication();
        assertNotNull(auth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        MockHttpSession session = (MockHttpSession) request.getSession(false);
        assertNotNull(session);
        assertTrue(session.isValid());



        // logout triggered by geoserver
        request = createRequest(logoutchain.getPatterns().get(0));
        //request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, ctx);
        SecurityContextHolder.setContext(ctx);
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        //getProxy().doFilter(request, response, chain);
        GeoServerLogoutFilter logoutFilter=
                (GeoServerLogoutFilter) getSecurityManager().loadFilter(GeoServerSecurityFilterChain.FORM_LOGOUT_FILTER);
        logoutFilter.doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertTrue(response.wasRedirectSent());
        String redirectUrl = response.getHeader("Location");
        assertNotNull(redirectUrl);
        assertTrue(redirectUrl.contains(GeoServerCasConstants.LOGOUT_URI));
        session = (MockHttpSession) request.getSession(false);

        // login
        helper = new CasFormAuthenticationHelper(casServerURLPrefix,
                username, password);
        helper.ssoLogin();

        request = createRequest(pattern);
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        String ticket = loginUsingTicket(helper, request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertFalse(response.wasRedirectSent());


        ctx = (SecurityContext) request.getSession(false).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertNotNull(ctx);
        auth = ctx.getAuthentication();
        assertNotNull(auth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        session = (MockHttpSession) request.getSession(false);
        assertNotNull(session);
        assertTrue(session.isValid());

       
        // logout triggered by cas server
        request = createRequest(pattern);
        //request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, ctx);
        SecurityContextHolder.setContext(ctx);      
        request.setMethod("POST");       
        request.setSession(session);
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("logoutRequest", getBodyForLogoutRequest(ticket));
        GeoServerCasAuthenticationFilter casFilter = (GeoServerCasAuthenticationFilter)
                getSecurityManager().loadFilter(casFilterName);
        //getProxy().doFilter(request, response, chain);
        casFilter.doFilter(request, response, chain);
View Full Code Here

        SecurityContextHolder.getContext().setAuthentication(null);

        // test entry point
        MockHttpServletRequest request = createRequest("wms");
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain();
        request.setupAddParameter("ticket", "ST-blabla");
        request.setQueryString("ticket=ST-blabla");
        request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());

        // test successful
        getCache().removeAll();
        String username = "castest";
        CasFormAuthenticationHelper helper = new CasFormAuthenticationHelper(casServerURLPrefix,
                username, username);
        helper.ssoLogin();

        request = createRequest("wms");
        request.setQueryString("request=getCapabilities");
        request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        String ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString() + "?"
                + request.getQueryString()));
        assertNotNull(ticket);
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("ticket", ticket);
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        TestingAuthenticationCache cache = getCache();
        Authentication casAuth = cache.get(casProxyFilterName, username);
        assertNotNull(casAuth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());

        checkForAuthenticatedRole(casAuth);
        assertEquals(username, casAuth.getPrincipal());
        assertTrue(casAuth.getAuthorities().contains(new GeoServerRole(rootRole)));
        assertTrue(casAuth.getAuthorities().contains(new GeoServerRole(derivedRole)));
        assertNotNull(request.getAttribute(GeoServerCasConstants.CAS_ASSERTION_KEY));

        assertNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
                .removeSessionByMappingId(ticket));
        helper.ssoLogout();

        // check unknown user

        username = "unknown";
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, username);
        helper.ssoLogin();

        request = createRequest("wms");
        ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString()));
        assertNotNull(ticket);
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("ticket", ticket);
        request.setQueryString("ticket=" + ticket);
        request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        cache = getCache();
        casAuth = cache.get(casProxyFilterName, username);
        assertNotNull(casAuth);
        assertNotNull(casAuth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());

        checkForAuthenticatedRole(casAuth);
        assertEquals(username, casAuth.getPrincipal());
        assertEquals(1, casAuth.getAuthorities().size());
        assertNotNull(request.getAttribute(GeoServerCasConstants.CAS_ASSERTION_KEY));

        // check for disabled user
        getCache().removeAll();
        updateUser("ug1", "castest", false);

        username = "castest";
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, username);
        helper.ssoLogin();

        request = createRequest("wms");
        ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString()));
        assertNotNull(ticket);

        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("ticket", ticket);
        request.setQueryString("ticket=" + ticket);
        request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());
        cache = getCache();
        casAuth = cache.get(casProxyFilterName, ticket);
        assertNull(casAuth);
        assertNull(request.getAttribute(GeoServerCasConstants.CAS_ASSERTION_KEY));
        assertNull(request.getSession(false));

        updateUser("ug1", "castest", true);
        helper.ssoLogout();

        // Test anonymous
        insertAnonymousFilter();
        request = createRequest("wms");
        request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        // Anonymous context is not stored in http session, no further testing
        removeAnonymousFilter();

        // test proxy granting ticket

        pconfig1.setProxyCallbackUrlPrefix(proxyCallbackUrlPrefix.toString());
        getSecurityManager().saveFilter(pconfig1);

               
        getCache().removeAll();
        username = "castest";
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, username);
        authenticateWithPGT(helper);
        request = createRequest("wms");
        ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString()));
        request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        assertNotNull(ticket);

        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("ticket", ticket);
        getProxy().doFilter(request, response, chain);

        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        cache = getCache();
View Full Code Here

        SecurityContextHolder.getContext().setAuthentication(null);

        // test entry point with header attribute
        MockHttpServletRequest request = createRequest("wms");
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain();
        request.setupAddParameter("ticket", "ST-blabla");
        request.setQueryString("ticket=ST-blabla");
        request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());
       
        // test entry point with url param
        request = createRequest("wms");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("ticket", "ST-blabla");
        request.setupAddParameter(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        request.setQueryString("ticket=ST-blabla&"+GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT+"=false");       
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());


        // test successful

        getCache().removeAll();
        String username = "castest";
        CasFormAuthenticationHelper helper = new CasFormAuthenticationHelper(casServerURLPrefix,
                username, username);
        Assertion ass = authenticateWithPGT(helper);
        String proxyTicket = null;
        for (int i = 0; i < 2; i++) {
            request = createRequest("wms");
            request.setQueryString("request=getCapabilities");
            proxyTicket = ass.getPrincipal().getProxyTicketFor(
                    request.getRequestURL().toString() + "?" + request.getQueryString());
            assertNotNull(proxyTicket);
            response = new MockHttpServletResponse();
            chain = new MockFilterChain();
            request.setupAddParameter("ticket", proxyTicket);
            if (i==0) {
                request.setupAddParameter(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
                request.setQueryString(request.getQueryString()+"&ticket="+proxyTicket+"&"+GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT+"=false");
            } else {
                request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
                request.setQueryString(request.getQueryString()+"&ticket="+proxyTicket);
            }           
            getProxy().doFilter(request, response, chain);
            assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
            TestingAuthenticationCache cache = getCache();
            Authentication casAuth = cache.get(casProxyFilterName, username);
            assertNotNull(casAuth);
            checkForAuthenticatedRole(casAuth);
            assertEquals(username, casAuth.getPrincipal());
            assertTrue(casAuth.getAuthorities().contains(new GeoServerRole(rootRole)));
            assertTrue(casAuth.getAuthorities().contains(new GeoServerRole(derivedRole)));
            assertNotNull(request.getAttribute(GeoServerCasConstants.CAS_ASSERTION_KEY));
            assertNull(request.getSession(false));
        }
        assertNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
                .removeSessionByMappingId(proxyTicket));
        helper.ssoLogout();

        // check unknown user

        username = "unknown";
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, username);
        ass = authenticateWithPGT(helper);
        for (int i = 0; i < 2; i++) {
            request = createRequest("wms");
            request.setQueryString("request=getCapabilities");
            proxyTicket = ass.getPrincipal().getProxyTicketFor(request.getRequestURL().toString() + "?" + request.getQueryString());
            assertNotNull(proxyTicket);
            response = new MockHttpServletResponse();
            chain = new MockFilterChain();
            request.setupAddParameter("ticket", proxyTicket);
            if (i==0) {
                request.setupAddParameter(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
                request.setQueryString(request.getQueryString()+"&ticket="+proxyTicket+"&"+GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT+"=false");
            } else {
                request.setHeader(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
                request.setQueryString(request.getQueryString()+"&ticket="+proxyTicket);
            }           
            getProxy().doFilter(request, response, chain);
            assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
            TestingAuthenticationCache cache = getCache();
            Authentication casAuth = cache.get(casProxyFilterName, username);
            assertNotNull(casAuth);
            checkForAuthenticatedRole(casAuth);
            assertEquals(username, casAuth.getPrincipal());
            assertEquals(1, casAuth.getAuthorities().size());
            assertNotNull(request.getAttribute(GeoServerCasConstants.CAS_ASSERTION_KEY));
            assertNull(request.getSession(false));

        }
        helper.ssoLogout();

        // check for disabled user
        getCache().removeAll();
        updateUser("ug1", "castest", false);

        username = "castest";
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, username);
        ass = authenticateWithPGT(helper);
        request = createRequest("wms");
        proxyTicket = ass.getPrincipal().getProxyTicketFor(request.getRequestURL().toString());
        assertNotNull(proxyTicket);
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("ticket", proxyTicket);
        request.setupAddParameter(GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT, "false");
        request.setQueryString("ticket="+proxyTicket+"&"+GeoServerCasAuthenticationEntryPoint.CAS_REDIRECT+"=false");

        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());
        TestingAuthenticationCache cache = getCache();
        Authentication casAuth = cache.get(casProxyFilterName, proxyTicket);
        assertNull(casAuth);
        assertNull(request.getAttribute(GeoServerCasConstants.CAS_ASSERTION_KEY));
        assertNull(request.getSession(false));

        updateUser("ug1", "castest", true);
        helper.ssoLogout();

        // Test anonymous
        insertAnonymousFilter();
        request = createRequest("wms");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        // Anonymous context is not stored in http session, no further testing
        removeAnonymousFilter();

        // test proxy granting ticket in proxied auth filter

        pconfig1.setProxyCallbackUrlPrefix(proxyCallbackUrlPrefix.toString());
        getSecurityManager().saveFilter(pconfig1);

        getCache().removeAll();
        username = "castest";
        helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, username);
        ass = authenticateWithPGT(helper);
        request = createRequest("wms");
        proxyTicket = ass.getPrincipal().getProxyTicketFor(request.getRequestURL().toString());
        assertNotNull(proxyTicket);
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        request.setupAddParameter("ticket", proxyTicket);
        getProxy().doFilter(request, response, chain);

        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        cache = getCache();
View Full Code Here

    public void testFilterIp() throws IOException, ServletException {
        Properties props = new Properties();
        props.put("ip.blacklist", "192.168.1.8,192.168.1.10");
        IpBlacklistFilter filter = new IpBlacklistFilter(props);
        assertNotNull(filter);
        MockFilterChain filterChain = new MockFilterChain();
        filterChain.addFilter(filter);
        TestServlet testServlet = new TestServlet();
        filterChain.setServlet(testServlet);
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setRemoteAddr("192.168.1.8");
        MockHttpServletResponse response = new MockHttpServletResponse();
        filterChain.doFilter(request, response);
        assertFalse(testServlet.wasServiceCalled());
        testServlet.reset();
        request.setRemoteAddr("192.168.1.9");
        filterChain.doFilter(request, response);
        assertTrue(testServlet.wasServiceCalled());
        testServlet.reset();
        request.setRemoteAddr("192.168.1.10");
        filterChain.doFilter(request, response);
        assertFalse(testServlet.wasServiceCalled());
        testServlet.reset();
    }
View Full Code Here

        SecurityContextHolder.getContext().setAuthentication(null);

        // Test entry point
        MockHttpServletRequest request = createRequest("/foo/bar");
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getErrorCode());

        // test success
        String authKey=null;
        for (Entry<Object,Object> entry : mapper.authKeyProps.entrySet()) {
            if (testUserName.equals(entry.getValue())) {
                authKey=(String)entry.getKey();
                break;
            }
        }
       
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();       
        request.setQueryString(authKeyUrlParam+"=" + authKey);       
        request.setupAddParameter(authKeyUrlParam, authKey);
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertFalse(response.wasRedirectSent());


        SecurityContext ctx = (SecurityContext) request.getSession(false).getAttribute(
                HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
        assertNotNull(ctx);
        Authentication auth = ctx.getAuthentication();
        assertNotNull(auth);
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        checkForAuthenticatedRole(auth);
        assertEquals(testUserName, auth.getPrincipal());

        // check unknown user
        username = "unknown";
        password = username;
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();

        request.setQueryString(authKeyUrlParam+"=abc");       
        request.setupAddParameter(authKeyUrlParam, "abc");
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getErrorCode());
       
        assertNull(SecurityContextHolder.getContext().getAuthentication());
       

        // check disabled user
        username = testUserName;
        password = username;
        updateUser("ug1", username, false);
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
       
        request.setQueryString(authKeyUrlParam+"=" + authKey);       
        request.setupAddParameter(authKeyUrlParam, authKey);
        getProxy().doFilter(request, response, chain);
       
        assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getErrorCode());
       
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        updateUser("ug1", username, true);

        insertAnonymousFilter();
        request = createRequest("foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        // Anonymous context is not stored in http session, no further testing
        removeAnonymousFilter();
View Full Code Here

        SecurityContextHolder.getContext().setAuthentication(null);

        // Test entry point
        MockHttpServletRequest request = createRequest("/foo/bar");
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getErrorCode());

        // test success
        GeoServerUser user= (GeoServerUser) getSecurityManager().loadUserGroupService("ug1").loadUserByUsername(testUserName);
        String authKey=user.getProperties().getProperty(mapper.getUserPropertyName());
        assertNotNull(authKey);
       
               
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();       
        request.setQueryString(authKeyUrlParam+"=" + authKey);       
        request.setupAddParameter(authKeyUrlParam, authKey);
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        assertFalse(response.wasRedirectSent());

        Authentication auth = (Authentication) getCache().get(filterName,authKey);
        assertNotNull(auth);
        assertNull(request.getSession(false));
        checkForAuthenticatedRole(auth);
        assertEquals(testUserName, auth.getPrincipal());

       
        // check unknown user
        username = "unknown";
        password = username;
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();

        request.setQueryString(authKeyUrlParam+"=abc");       
        request.setupAddParameter(authKeyUrlParam, "abc");
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getErrorCode());
       
        assertNull(SecurityContextHolder.getContext().getAuthentication());
       
       
        getCache().removeAll();
       
        // check disabled user
        username = testUserName;
        password = username;
        updateUser("ug1", username, false);
        request = createRequest("/foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
       
        request.setQueryString(authKeyUrlParam+"=" + authKey);       
        request.setupAddParameter(authKeyUrlParam, authKey);
        getProxy().doFilter(request, response, chain);
       
        assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getErrorCode());
        assertNull(getCache().get(filterName, authKey));
        assertNull(SecurityContextHolder.getContext().getAuthentication());
        updateUser("ug1", username, true);

        insertAnonymousFilter();
        request = createRequest("foo/bar");
        response = new MockHttpServletResponse();
        chain = new MockFilterChain();
        getProxy().doFilter(request, response, chain);
        assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
        // Anonymous context is not stored in http session, no further testing
        removeAnonymousFilter();
View Full Code Here

    public void testPathIsNullNPE() {
        MockHttpServletRequest request = new MyMockRequest();
        request.setServerName("localhost");
        request.setRequestURL("/test?name=0");
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain filterChain = new MockFilterChain();
       
        AdvancedDispatchFilter instance = new AdvancedDispatchFilter();
        try {
            instance.doFilter(request, response, filterChain);
        } catch (Exception ex) {
View Full Code Here

TOP

Related Classes of com.mockrunner.mock.web.MockFilterChain

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.