Search in sources :

Example 71 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class BaseCasSimpleMultifactorSendTokenActionTests method buildRequestContextFor.

protected MockRequestContext buildRequestContextFor(final String user) {
    val context = new MockRequestContext();
    val messageContext = (DefaultMessageContext) context.getMessageContext();
    messageContext.setMessageSource(mock(MessageSource.class));
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    WebUtils.putServiceIntoFlashScope(context, RegisteredServiceTestUtils.getService());
    val principal = RegisteredServiceTestUtils.getPrincipal(user, CollectionUtils.wrap("phone", List.of("123456789"), "mail", List.of("cas@example.org")));
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication(principal), context);
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, casSimpleMultifactorAuthenticationProvider);
    return context;
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DefaultMessageContext(org.springframework.binding.message.DefaultMessageContext) MessageSource(org.springframework.context.MessageSource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 72 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class CasSimpleMultifactorAuthenticationHandlerTests method verifyFailsToFindToken.

@Test
public void verifyFailsToFindToken() {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication(), context);
    val id = UUID.randomUUID().toString();
    val credential = new CasSimpleMultifactorTokenCredential(id);
    assertThrows(FailedLoginException.class, () -> casSimpleMultifactorAuthenticationHandler.authenticate(credential));
}
Also used : lombok.val(lombok.val) 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.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 73 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class ServiceWarningActionTests method verifyAction.

@Test
public void verifyAction() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.addParameter(ServiceWarningAction.PARAMETER_NAME_IGNORE_WARNING, "true");
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    assertThrows(InvalidTicketException.class, () -> action.execute(context).getId());
    val tgt = new MockTicketGrantingTicket("casuser");
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    assertThrows(InvalidTicketException.class, () -> action.execute(context).getId());
    val service = RegisteredServiceTestUtils.getService("https://google.com");
    getServicesManager().save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
    WebUtils.putServiceIntoFlowScope(context, service);
    WebUtils.putCredential(context, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword());
    getTicketRegistry().addTicket(tgt);
    assertEquals(CasWebflowConstants.STATE_ID_REDIRECT, action.execute(context).getId());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) 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.jupiter.api.Test)

Example 74 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class SetServiceUnauthorizedRedirectUrlActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val action = new SetServiceUnauthorizedRedirectUrlAction(getServicesManager());
    val service = getWebApplicationServiceFactory().createService("https://github.com/apereo/cas");
    WebUtils.putRegisteredService(context, getServicesManager().findServiceBy(service));
    action.execute(context);
    assertNotNull(WebUtils.getUnauthorizedRedirectUrlFromFlowScope(context));
}
Also used : lombok.val(lombok.val) SetServiceUnauthorizedRedirectUrlAction(org.apereo.cas.web.flow.login.SetServiceUnauthorizedRedirectUrlAction) 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.jupiter.api.Test)

Example 75 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class VerifyRequiredServiceActionTests method onSetUp.

@BeforeEach
public void onSetUp() {
    this.ticketRegistrySupport = mock(TicketRegistrySupport.class);
    this.verifyRequiredServiceAction = new VerifyRequiredServiceAction(getServicesManager(), getTicketGrantingTicketCookieGenerator(), casProperties, ticketRegistrySupport);
    val response = new MockHttpServletResponse();
    this.requestContext = new MockRequestContext();
    this.httpRequest = new MockHttpServletRequest();
    this.requestContext.setExternalContext(new ServletExternalContext(new MockServletContext(), this.httpRequest, response));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) TicketRegistrySupport(org.apereo.cas.ticket.registry.TicketRegistrySupport) VerifyRequiredServiceAction(org.apereo.cas.web.flow.login.VerifyRequiredServiceAction) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MockRequestContext (org.springframework.webflow.test.MockRequestContext)517 lombok.val (lombok.val)483 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)466 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)458 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)454 Test (org.junit.jupiter.api.Test)450 MockServletContext (org.springframework.mock.web.MockServletContext)404 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)132 MockServletContext (org.apereo.cas.util.MockServletContext)46 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)43 MockWebServer (org.apereo.cas.util.MockWebServer)36 ByteArrayResource (org.springframework.core.io.ByteArrayResource)36 ClientInfo (org.apereo.inspektr.common.web.ClientInfo)35 Test (org.junit.Test)29 Event (org.springframework.webflow.execution.Event)26 Transition (org.springframework.webflow.engine.Transition)25 LiteralExpression (org.springframework.binding.expression.support.LiteralExpression)23 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)23 DefaultTargetStateResolver (org.springframework.webflow.engine.support.DefaultTargetStateResolver)23 DefaultTransitionCriteria (org.springframework.webflow.engine.support.DefaultTransitionCriteria)23