Search in sources :

Example 21 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class WebUtilsTests method verifyOperation.

@Test
public void verifyOperation() {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val flow = new Flow("mockFlow");
    val flowSession = new MockFlowSession(flow);
    flowSession.setParent(new MockFlowSession(flow));
    val mockExecutionContext = new MockFlowExecutionContext(flowSession);
    context.setFlowExecutionContext(mockExecutionContext);
    WebUtils.putLogoutRedirectUrl(context, URL);
    assertNotNull(WebUtils.getLogoutRedirectUrl(context, String.class));
    WebUtils.removeLogoutRedirectUrl(context);
    assertNull(WebUtils.getLogoutRedirectUrl(context, String.class));
    assertNull(WebUtils.getHttpServletRequestUserAgentFromRequestContext(context));
    assertNull(WebUtils.getHttpServletRequestUserAgentFromRequestContext(request));
    assertNull(WebUtils.getAuthenticationResult(context));
    assertNull(WebUtils.getHttpServletRequestGeoLocationFromRequestContext());
    assertNull(WebUtils.getAcceptableUsagePolicyTermsFromFlowScope(context, Object.class));
    assertFalse(WebUtils.hasSurrogateAuthenticationRequest(context));
    assertNotNull(WebUtils.produceUnauthorizedErrorView(new RuntimeException()));
    assertNotNull(WebUtils.produceErrorView(new IllegalArgumentException()));
    assertNotNull(WebUtils.produceErrorView("error-view", new IllegalArgumentException()));
    assertNotNull(WebUtils.getHttpRequestFullUrl(context));
    request.setQueryString("param=value");
    assertNotNull(WebUtils.getHttpRequestFullUrl(request));
    assertFalse(WebUtils.isGraphicalUserAuthenticationEnabled(context));
    assertTrue(WebUtils.getDelegatedAuthenticationProviderConfigurations(context).isEmpty());
    assertNull(WebUtils.getAvailableAuthenticationHandleNames(context));
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() {
            WebUtils.putYubiKeyMultipleDeviceRegistrationEnabled(context, true);
            WebUtils.putInitialHttpRequestPostParameters(context);
            WebUtils.putExistingSingleSignOnSessionAvailable(context, true);
            WebUtils.putExistingSingleSignOnSessionPrincipal(context, CoreAuthenticationTestUtils.getPrincipal());
            WebUtils.putAvailableAuthenticationHandleNames(context, List.of());
            WebUtils.putPasswordManagementEnabled(context, true);
            WebUtils.putRecaptchaPropertiesFlowScope(context, new GoogleRecaptchaProperties().setEnabled(true));
            WebUtils.putLogoutUrls(context, Map.of());
            val ac = OneTimeTokenAccount.builder().validationCode(123456).username("casuser").name("Example").build();
            WebUtils.putOneTimeTokenAccount(context, ac);
            assertNotNull(WebUtils.getOneTimeTokenAccount(context, OneTimeTokenAccount.class));
            WebUtils.putOneTimeTokenAccounts(context, List.of(ac));
            WebUtils.putWarnCookieIfRequestParameterPresent(null, context);
            WebUtils.putTicketGrantingTicketInScopes(context, "ticket-id");
        }
    });
    WebUtils.putCredential(context, new UsernamePasswordCredential("casuser", "password"));
    assertThrows(ClassCastException.class, () -> WebUtils.getCredential(context, OneTimeTokenCredential.class));
    WebUtils.putTicketGrantingTicketInScopes(context, StringUtils.EMPTY);
    WebUtils.putTicketGrantingTicketInScopes(context, (TicketGrantingTicket) null);
    WebUtils.putTicketGrantingTicketInScopes(context, (String) null);
    assertNull(WebUtils.getTicketGrantingTicket(context));
    assertThrows(IllegalArgumentException.class, () -> WebUtils.getPrincipalFromRequestContext(context, null));
    request.addParameter(WebUtils.PUBLIC_WORKSTATION_ATTRIBUTE, "true");
    WebUtils.putPublicWorkstationToFlowIfRequestParameterPresent(context);
    assertTrue(WebUtils.isAuthenticatingAtPublicWorkstation(context));
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    WebUtils.putTicketGrantingTicketInScopes(context, "TGT-XYZ123");
    assertNull(WebUtils.getPrincipalFromRequestContext(context, ticketRegistrySupport));
    WebUtils.putLogoutPostUrl(context, URL);
    assertEquals(URL, WebUtils.getLogoutPostUrl(context));
    val data = new HashMap<String, Object>();
    data.put("SAMLResponse", "xxx");
    WebUtils.putLogoutPostData(context, data);
    assertEquals(data, WebUtils.getLogoutPostData(context));
}
Also used : lombok.val(lombok.val) MockFlowSession(org.springframework.webflow.test.MockFlowSession) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.apereo.cas.util.MockServletContext) OneTimeTokenCredential(org.apereo.cas.authentication.credential.OneTimeTokenCredential) Flow(org.springframework.webflow.engine.Flow) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) Executable(org.junit.jupiter.api.function.Executable) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 22 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class ClearWebflowCredentialActionTests method verifyAction.

private void verifyAction(final String currentEvent) 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 ClearWebflowCredentialAction();
    context.setCurrentEvent(null);
    assertNull(action.execute(context));
    context.setCurrentEvent(new Event(this, CasWebflowConstants.TRANSITION_ID_SUCCESS));
    assertNull(action.execute(context));
    WebUtils.putCredential(context, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword());
    val flow = (Flow) context.getActiveFlow();
    val factory = mock(VariableValueFactory.class);
    when(factory.createInitialValue(any())).thenReturn(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword());
    val variable = new FlowVariable(CasWebflowConstants.VAR_ID_CREDENTIAL, factory);
    flow.addVariable(variable);
    context.setCurrentEvent(new Event(this, currentEvent));
    assertNull(action.execute(context));
    assertNotNull(WebUtils.getCredential(context));
}
Also used : lombok.val(lombok.val) 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) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) Flow(org.springframework.webflow.engine.Flow) FlowVariable(org.springframework.webflow.engine.FlowVariable)

Example 23 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class DuoSecurityPrepareWebLoginFormActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    val flowSession = new MockFlowSession(new Flow(CasWebflowConfigurer.FLOW_ID_LOGIN));
    flowSession.setState(new ViewState(flowSession.getDefinitionInternal(), "viewState", mock(ViewFactory.class)));
    val exec = new MockFlowExecutionContext(flowSession);
    val context = new MockRequestContext(exec);
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    val authentication = CoreAuthenticationTestUtils.getAuthentication();
    WebUtils.putAuthentication(authentication, context);
    val duoService = mock(DuoSecurityAuthenticationService.class);
    when(duoService.getProperties()).thenReturn(casProperties.getAuthn().getMfa().getDuo().get(0));
    val provider = mock(DuoSecurityMultifactorAuthenticationProvider.class);
    when(provider.getId()).thenReturn(DuoSecurityMultifactorAuthenticationProperties.DEFAULT_IDENTIFIER);
    when(provider.getDuoAuthenticationService()).thenReturn(duoService);
    when(provider.matches(anyString())).thenReturn(Boolean.TRUE);
    WebUtils.putCredential(context, new DuoSecurityCredential(authentication.getPrincipal().getId(), UUID.randomUUID().toString(), provider.getId()));
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, provider);
    TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext, provider);
    val action = new DuoSecurityPrepareWebLoginFormAction();
    val event = action.execute(context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, event.getId());
}
Also used : lombok.val(lombok.val) MockFlowSession(org.springframework.webflow.test.MockFlowSession) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ViewState(org.springframework.webflow.engine.ViewState) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.apereo.cas.util.MockServletContext) Flow(org.springframework.webflow.engine.Flow) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DuoSecurityCredential(org.apereo.cas.adaptors.duo.authn.DuoSecurityCredential) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 24 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class CasSpringBootAdminServerServletInitializerTests method verifyInitializer.

@Test
public void verifyInitializer() {
    val servletContext = new MockServletContext();
    val servletInitializer = new CasSpringBootAdminServletInitializer();
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() throws Throwable {
            servletInitializer.onStartup(servletContext);
        }
    });
}
Also used : lombok.val(lombok.val) Executable(org.junit.jupiter.api.function.Executable) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 25 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class RenewAuthenticationRequestCheckActionTests method verifyProceed.

@Test
public void verifyProceed() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val strategy = new DefaultSingleSignOnParticipationStrategy(servicesManager, casProperties.getSso(), mock(TicketRegistrySupport.class), mock(AuthenticationServiceSelectionPlan.class));
    val action = new RenewAuthenticationRequestCheckAction(strategy);
    assertEquals(CasWebflowConstants.TRANSITION_ID_PROCEED, action.execute(context).getId());
}
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) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) MockRequestContext(org.springframework.webflow.test.MockRequestContext) DefaultSingleSignOnParticipationStrategy(org.apereo.cas.web.flow.DefaultSingleSignOnParticipationStrategy) 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)

Aggregations

lombok.val (lombok.val)50 MockServletContext (org.apereo.cas.util.MockServletContext)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)46 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Test (org.junit.jupiter.api.Test)45 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)44 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)30 JEEContext (org.pac4j.core.context.JEEContext)10 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)7 Executable (org.junit.jupiter.api.function.Executable)5 DefaultRegisteredServiceAccessStrategy (org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy)4 SAML2Client (org.pac4j.saml.client.SAML2Client)4 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)4 Flow (org.springframework.webflow.engine.Flow)3 Cookie (javax.servlet.http.Cookie)2 SneakyThrows (lombok.SneakyThrows)2 AuthenticationServiceSelectionPlan (org.apereo.cas.authentication.AuthenticationServiceSelectionPlan)2 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)2 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)2