Search in sources :

Example 96 with MockRequestContext

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

the class OneTimeTokenAuthenticationWebflowEventResolverTests method verifyResolver.

@Test
public void verifyResolver() {
    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());
    val authn = RegisteredServiceTestUtils.getAuthentication("casuser");
    val builder = mock(AuthenticationResultBuilder.class);
    when(builder.getInitialAuthentication()).thenReturn(Optional.of(authn));
    when(builder.collect(any(Authentication.class))).thenReturn(builder);
    WebUtils.putAuthenticationResultBuilder(builder, context);
    WebUtils.putAuthentication(authn, context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, resolver.resolveSingle(context).getId());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Authentication(org.apereo.cas.authentication.Authentication) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 97 with MockRequestContext

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

the class SwivelAuthenticationHandlerTests method verifyAuthnFails.

@Test
public void verifyAuthnFails() throws Exception {
    val data = "<?xml version=\"1.0\" ?>" + "<SASResponse secret=\"MyAdminAgent\" version=\"3.4\">" + "<Version>3.6</Version>\n" + "<Result>FAIL</Result>\n" + "<SessionID>c7379ef1b41f90a4900548a75e13f62a</SessionID>" + "</SASResponse>";
    try (val webServer = new MockWebServer(9191, new ByteArrayResource(data.getBytes(UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val c = new SwivelTokenCredential("123456");
        val context = new MockRequestContext();
        val request = new MockHttpServletRequest();
        val response = new MockHttpServletResponse();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
        setRequestContext(context);
        ExternalContextHolder.setExternalContext(context.getExternalContext());
        WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(), context);
        setRequestContext(context);
        assertThrows(FailedLoginException.class, () -> swivelAuthenticationHandler.authenticate(c));
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) 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 98 with MockRequestContext

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

the class SwivelAuthenticationHandlerTests method verifyAuthn.

@Test
public void verifyAuthn() throws Exception {
    val data = "<?xml version=\"1.0\" ?>" + "<SASResponse secret=\"MyAdminAgent\" version=\"3.4\">" + "<Version>3.6</Version>\n" + "<Result>PASS</Result>\n" + "<SessionID>c7379ef1b41f90a4900548a75e13f62a</SessionID>" + "</SASResponse>";
    try (val webServer = new MockWebServer(9191, new ByteArrayResource(data.getBytes(UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val c = new SwivelTokenCredential("123456");
        val context = new MockRequestContext();
        val request = new MockHttpServletRequest();
        val response = new MockHttpServletResponse();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
        setRequestContext(context);
        ExternalContextHolder.setExternalContext(context.getExternalContext());
        WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(), context);
        setRequestContext(context);
        assertNotNull(swivelAuthenticationHandler.authenticate(c));
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) 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 99 with MockRequestContext

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

the class LoadSurrogatesListActionTests method verifyGetListView.

@Test
public void verifyGetListView() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    WebUtils.putSurrogateAuthenticationRequest(context, true);
    WebUtils.putCredential(context, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword("casuser"));
    assertEquals(CasWebflowConstants.TRANSITION_ID_SURROGATE_VIEW, loadSurrogatesListAction.execute(context).getId());
    assertNotNull(WebUtils.getSurrogateAuthenticationAccounts(context));
}
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) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 100 with MockRequestContext

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

the class LoadSurrogatesListActionTests method verifySkipAuthenticate.

@Test
public void verifySkipAuthenticate() throws Exception {
    val context = new MockRequestContext();
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    WebUtils.putSurrogateAuthenticationRequest(context, Boolean.TRUE);
    val attributes = new LinkedHashMap<String, List<Object>>();
    attributes.put(SurrogateAuthenticationService.AUTHENTICATION_ATTR_SURROGATE_ENABLED, List.of(true));
    attributes.putAll(CoreAuthenticationTestUtils.getAttributeRepository().getBackingMap());
    val p = CoreAuthenticationTestUtils.getPrincipal("someuser", attributes);
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(p), context);
    val request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    val creds = new SurrogateUsernamePasswordCredential();
    creds.setPassword("Mellon");
    creds.setUsername("someuser");
    creds.setSurrogateUsername("others");
    WebUtils.putCredential(context, creds);
    val builder = mock(AuthenticationResultBuilder.class);
    when(builder.getInitialAuthentication()).thenReturn(Optional.of(CoreAuthenticationTestUtils.getAuthentication("casuser")));
    when(builder.collect(any(Authentication.class))).thenReturn(builder);
    WebUtils.putAuthenticationResultBuilder(builder, context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SKIP_SURROGATE, loadSurrogatesListAction.execute(context).getId());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Authentication(org.apereo.cas.authentication.Authentication) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) LinkedHashMap(java.util.LinkedHashMap) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) Test(org.junit.jupiter.api.Test)

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