Search in sources :

Example 91 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project cas by apereo.

the class InitialFlowSetupActionSsoTests method disableFlowIfNoService.

@Test
public void disableFlowIfNoService() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    this.thrown.expect(NoSuchFlowExecutionException.class);
    this.action.execute(context);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 92 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project cas by apereo.

the class SendTicketGrantingTicketActionSsoTests method verifySsoSessionCookieOnRenewAsParameter.

@Test
public void verifySsoSessionCookieOnRenewAsParameter() throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(CasProtocolConstants.PARAMETER_RENEW, "true");
    request.setRemoteAddr(LOCALHOST_IP);
    request.setLocalAddr(LOCALHOST_IP);
    request.addHeader(HttpRequestUtils.USER_AGENT_HEADER, "test");
    ClientInfoHolder.setClientInfo(new ClientInfo(request));
    final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class);
    when(tgt.getId()).thenReturn(TEST_STRING);
    request.setCookies(new Cookie("TGT", "test5"));
    WebUtils.putTicketGrantingTicketInScopes(this.context, tgt);
    this.context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    assertEquals(SUCCESS, action.execute(this.context).getId());
    assertEquals(0, response.getCookies().length);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 93 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project cas by apereo.

the class Cas20ResponseViewTests method verifyView.

@Test
public void verifyView() throws Exception {
    final ModelAndView modelAndView = this.getModelAndViewUponServiceValidationWithSecurePgtUrl();
    final MockHttpServletRequest req = new MockHttpServletRequest(new MockServletContext());
    req.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, new GenericWebApplicationContext(req.getServletContext()));
    final MockHttpServletResponse resp = new MockHttpServletResponse();
    final View delegatedView = new View() {

        @Override
        public String getContentType() {
            return "text/html";
        }

        @Override
        public void render(final Map<String, ?> map, final HttpServletRequest request, final HttpServletResponse response) {
            map.forEach(request::setAttribute);
        }
    };
    final Cas20ResponseView view = new Cas20ResponseView(true, null, null, "attribute", delegatedView, new DefaultAuthenticationAttributeReleasePolicy(), new DefaultAuthenticationServiceSelectionPlan());
    view.render(modelAndView.getModel(), req, resp);
    assertNotNull(req.getAttribute(CasViewConstants.MODEL_ATTRIBUTE_NAME_CHAINED_AUTHENTICATIONS));
    assertNotNull(req.getAttribute(CasViewConstants.MODEL_ATTRIBUTE_NAME_PRIMARY_AUTHENTICATION));
    assertNotNull(req.getAttribute(CasViewConstants.MODEL_ATTRIBUTE_NAME_PRINCIPAL));
    assertNotNull(req.getAttribute(CasProtocolConstants.VALIDATION_CAS_MODEL_PROXY_GRANTING_TICKET_IOU));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.MockServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultAuthenticationAttributeReleasePolicy(org.apereo.cas.authentication.DefaultAuthenticationAttributeReleasePolicy) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 94 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project cas by apereo.

the class Cas30ResponseViewTests method renderView.

protected Map<?, ?> renderView() throws Exception {
    final ModelAndView modelAndView = this.getModelAndViewUponServiceValidationWithSecurePgtUrl();
    LOGGER.debug("Retrieved model and view [{}]", modelAndView.getModel());
    final MockHttpServletRequest req = new MockHttpServletRequest(new MockServletContext());
    req.setAttribute(RequestContext.WEB_APPLICATION_CONTEXT_ATTRIBUTE, new GenericWebApplicationContext(req.getServletContext()));
    final ProtocolAttributeEncoder encoder = new DefaultCasProtocolAttributeEncoder(this.servicesManager, CipherExecutor.noOpOfStringToString());
    final View viewDelegated = new View() {

        @Override
        public String getContentType() {
            return MediaType.TEXT_HTML_VALUE;
        }

        @Override
        public void render(final Map<String, ?> map, final HttpServletRequest request, final HttpServletResponse response) {
            LOGGER.warn("Setting attribute [{}]", map.keySet());
            map.forEach(request::setAttribute);
        }
    };
    final AbstractCasView view = getCasViewToRender(encoder, viewDelegated);
    final MockHttpServletResponse resp = new MockHttpServletResponse();
    view.render(modelAndView.getModel(), req, resp);
    return getRenderedViewModelMap(req);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractCasView(org.apereo.cas.services.web.view.AbstractCasView) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.MockServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultCasProtocolAttributeEncoder(org.apereo.cas.authentication.support.DefaultCasProtocolAttributeEncoder) DefaultCasProtocolAttributeEncoder(org.apereo.cas.authentication.support.DefaultCasProtocolAttributeEncoder) ProtocolAttributeEncoder(org.apereo.cas.authentication.ProtocolAttributeEncoder) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AbstractCasView(org.apereo.cas.services.web.view.AbstractCasView)

Example 95 with MockServletContext

use of org.springframework.mock.web.MockServletContext in project cas by apereo.

the class OpenIdSingleSignOnActionTests method verifyNoService.

@Test
public void verifyNoService() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    final Event event = this.action.execute(context);
    assertNotNull(event);
    assertEquals("error", this.action.execute(context).getId());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Event(org.springframework.webflow.execution.Event) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.MockServletContext)186 Test (org.junit.Test)135 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)82 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)80 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)74 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)54 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Event (org.springframework.webflow.execution.Event)18 Before (org.junit.Before)16 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)14 MockMvc (org.springframework.test.web.servlet.MockMvc)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 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 Cookie (javax.servlet.http.Cookie)6 FilterChainProxy (org.springframework.security.web.FilterChainProxy)6