Search in sources :

Example 11 with MockHttpServletRequest

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

the class AuthenticationViaFormActionTests method verifySuccessfulAuthenticationWithServiceAndWarn.

@Test
public void verifySuccessfulAuthenticationWithServiceAndWarn() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockRequestContext context = new MockRequestContext();
    request.addParameter(USERNAME_PARAM, TEST);
    request.addParameter(PASSWORD_PARAM, TEST);
    request.addParameter("warn", "true");
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, TEST);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    final Credential c = CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword();
    putCredentialInRequestScope(context, c);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, this.action.execute(context).getId());
    assertNotNull(response.getCookie(this.warnCookieGenerator.getCookieName()));
}
Also used : Credential(org.apereo.cas.authentication.Credential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 12 with MockHttpServletRequest

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

the class AuthenticationViaFormActionTests method verifyRenewWithServiceAndSameCredentials.

@Test
public void verifyRenewWithServiceAndSameCredentials() throws Exception {
    final Credential c = CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword();
    final Service service = RegisteredServiceTestUtils.getService(RegisteredServiceTestUtils.CONST_TEST_URL);
    final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), service, c);
    final TicketGrantingTicket ticketGrantingTicket = getCentralAuthenticationService().createTicketGrantingTicket(ctx);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockRequestContext context = new MockRequestContext();
    WebUtils.putTicketGrantingTicketInScopes(context, ticketGrantingTicket);
    request.addParameter(CasProtocolConstants.PARAMETER_RENEW, "true");
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, RegisteredServiceTestUtils.getService(RegisteredServiceTestUtils.CONST_TEST_URL).getId());
    putCredentialInRequestScope(context, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword());
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    context.getFlowScope().put(CasProtocolConstants.PARAMETER_SERVICE, RegisteredServiceTestUtils.getService());
    final Event ev = this.action.execute(context);
    assertEquals(CasWebflowConstants.STATE_ID_SUCCESS, ev.getId());
}
Also used : Credential(org.apereo.cas.authentication.Credential) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Service(org.apereo.cas.authentication.principal.Service) Event(org.springframework.webflow.execution.Event) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) Test(org.junit.Test)

Example 13 with MockHttpServletRequest

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

the class AuthenticationViaFormActionTests method verifyFailedAuthenticationWithNoService.

@Test
public void verifyFailedAuthenticationWithNoService() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockRequestContext context = new MockRequestContext();
    request.addParameter(USERNAME_PARAM, TEST);
    request.addParameter(PASSWORD_PARAM, "test2");
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    final Credential c = CoreAuthenticationTestUtils.getCredentialsWithDifferentUsernameAndPassword();
    putCredentialInRequestScope(context, c);
    context.getRequestScope().put("org.springframework.validation.BindException.credentials", new BindException(c, "credential"));
    assertEquals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, this.action.execute(context).getId());
}
Also used : Credential(org.apereo.cas.authentication.Credential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) BindException(org.springframework.validation.BindException) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 14 with MockHttpServletRequest

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

the class FrontChannelLogoutActionTests method onSetUp.

@Before
public void onSetUp() throws Exception {
    final DefaultSingleLogoutServiceMessageHandler handler = new DefaultSingleLogoutServiceMessageHandler(new SimpleHttpClientFactoryBean().getObject(), new SamlCompliantLogoutMessageCreator(), servicesManager, new DefaultSingleLogoutServiceLogoutUrlBuilder(), false, new DefaultAuthenticationServiceSelectionPlan(new DefaultAuthenticationServiceSelectionStrategy()));
    final LogoutManagerImpl logoutManager = new LogoutManagerImpl(new SamlCompliantLogoutMessageCreator(), handler, false);
    this.frontChannelLogoutAction = new FrontChannelLogoutAction(logoutManager);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.requestContext = mock(RequestContext.class);
    final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
    when(this.requestContext.getExternalContext()).thenReturn(servletExternalContext);
    when(servletExternalContext.getNativeRequest()).thenReturn(request);
    when(servletExternalContext.getNativeResponse()).thenReturn(response);
    final LocalAttributeMap flowScope = new LocalAttributeMap();
    when(this.requestContext.getFlowScope()).thenReturn(flowScope);
    final MockFlowExecutionKey mockFlowExecutionKey = new MockFlowExecutionKey(FLOW_EXECUTION_KEY);
    final MockFlowExecutionContext mockFlowExecutionContext = new MockFlowExecutionContext();
    mockFlowExecutionContext.setKey(mockFlowExecutionKey);
    when(this.requestContext.getFlowExecutionContext()).thenReturn(mockFlowExecutionContext);
}
Also used : DefaultAuthenticationServiceSelectionStrategy(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionStrategy) LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) MockFlowExecutionKey(org.springframework.webflow.test.MockFlowExecutionKey) DefaultSingleLogoutServiceMessageHandler(org.apereo.cas.logout.DefaultSingleLogoutServiceMessageHandler) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultSingleLogoutServiceLogoutUrlBuilder(org.apereo.cas.logout.DefaultSingleLogoutServiceLogoutUrlBuilder) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) SimpleHttpClientFactoryBean(org.apereo.cas.util.http.SimpleHttpClientFactoryBean) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) SamlCompliantLogoutMessageCreator(org.apereo.cas.logout.SamlCompliantLogoutMessageCreator) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) RequestContext(org.springframework.webflow.execution.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) LogoutManagerImpl(org.apereo.cas.logout.LogoutManagerImpl) Before(org.junit.Before)

Example 15 with MockHttpServletRequest

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

the class GenerateServiceTicketActionTests method verifyTicketGrantingTicketNotTgtButGateway.

@Test
public void verifyTicketGrantingTicketNotTgtButGateway() throws Exception {
    final MockRequestContext context = new MockRequestContext();
    context.getFlowScope().put(SERVICE_PARAM, RegisteredServiceTestUtils.getService());
    final MockHttpServletRequest request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, SERVICE_PARAM);
    request.addParameter(CasProtocolConstants.PARAMETER_GATEWAY, "true");
    final TicketGrantingTicket tgt = mock(TicketGrantingTicket.class);
    when(tgt.getId()).thenReturn("bleh");
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    assertEquals(CasWebflowConstants.STATE_ID_GATEWAY, this.action.execute(context).getId());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1056 Test (org.junit.Test)827 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)557 Before (org.junit.Before)92 MockFilterChain (org.springframework.mock.web.MockFilterChain)89 FilterChain (javax.servlet.FilterChain)79 MockServletContext (org.springframework.mock.web.MockServletContext)71 Authentication (org.springframework.security.core.Authentication)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)51 MockHttpSession (org.springframework.mock.web.MockHttpSession)49 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)47 Cookie (javax.servlet.http.Cookie)44 MockRequestContext (org.springframework.webflow.test.MockRequestContext)42 HttpServletResponse (javax.servlet.http.HttpServletResponse)39 HashMap (java.util.HashMap)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 ModelAndView (org.springframework.web.servlet.ModelAndView)32 MockPortletWindowId (org.apereo.portal.mock.portlet.om.MockPortletWindowId)30 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)30 RegisteredService (org.apereo.cas.services.RegisteredService)27