Search in sources :

Example 1 with MockFlowSession

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

the class AcceptPasswordlessAuthenticationActionTests method verifyMissingTokenAction.

@Test
public void verifyMissingTokenAction() throws Exception {
    val exec = new MockFlowExecutionContext(new MockFlowSession(new Flow(CasWebflowConfigurer.FLOW_ID_LOGIN)));
    val context = new MockRequestContext(exec);
    val request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    val account = PasswordlessUserAccount.builder().email("email").phone("phone").username("casuser").name("casuser").build();
    WebUtils.putPasswordlessAuthenticationAccount(context, account);
    assertEquals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, acceptPasswordlessAuthenticationAction.execute(context).getId());
}
Also used : lombok.val(lombok.val) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) MockFlowSession(org.springframework.webflow.test.MockFlowSession) 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) Flow(org.springframework.webflow.engine.Flow) Test(org.junit.jupiter.api.Test)

Example 2 with MockFlowSession

use of org.springframework.webflow.test.MockFlowSession 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 3 with MockFlowSession

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

the class PrepareMultifactorProviderSelectionActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    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);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val chain = new DefaultChainingMultifactorAuthenticationProvider(new DefaultMultifactorAuthenticationFailureModeEvaluator(casProperties));
    val provider = new TestMultifactorAuthenticationProvider();
    provider.setBypassEvaluator(new DefaultChainingMultifactorAuthenticationBypassProvider());
    chain.addMultifactorAuthenticationProvider(provider);
    val attributes = new LocalAttributeMap(RegisteredService.class.getName(), RegisteredServiceTestUtils.getRegisteredService());
    attributes.put(MultifactorAuthenticationProvider.class.getName(), chain);
    val event = new EventFactorySupport().event(this, ChainingMultifactorAuthenticationProvider.DEFAULT_IDENTIFIER, attributes);
    context.setCurrentEvent(event);
    assertNull(action.execute(context));
    assertNotNull(WebUtils.getSelectableMultifactorAuthenticationProviders(context));
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) RegisteredService(org.apereo.cas.services.RegisteredService) MockFlowSession(org.springframework.webflow.test.MockFlowSession) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ViewState(org.springframework.webflow.engine.ViewState) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) DefaultChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.DefaultChainingMultifactorAuthenticationProvider) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) ChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.ChainingMultifactorAuthenticationProvider) MockServletContext(org.springframework.mock.web.MockServletContext) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) Flow(org.springframework.webflow.engine.Flow) DefaultMultifactorAuthenticationFailureModeEvaluator(org.apereo.cas.authentication.DefaultMultifactorAuthenticationFailureModeEvaluator) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DefaultChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.DefaultChainingMultifactorAuthenticationProvider) DefaultChainingMultifactorAuthenticationBypassProvider(org.apereo.cas.authentication.bypass.DefaultChainingMultifactorAuthenticationBypassProvider) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockFlowSession

use of org.springframework.webflow.test.MockFlowSession 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 5 with MockFlowSession

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

the class RadiusAuthenticationWebflowEventResolverFailureTests method initialize.

@BeforeEach
public void initialize() {
    this.context = mock(RequestContext.class);
    when(context.getConversationScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getFlowScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getRequestScope()).thenReturn(new LocalAttributeMap<>());
    when(context.getMessageContext()).thenReturn(mock(MessageContext.class));
    when(context.getRequestParameters()).thenReturn(new MockParameterMap());
    when(context.getRequestParameters()).thenReturn(new MockParameterMap());
    val request = new MockHttpServletRequest();
    when(context.getExternalContext()).thenReturn(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    when(context.getFlowExecutionContext()).thenReturn(new MockFlowExecutionContext(new MockFlowSession(new Flow("mockFlow"))));
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    val authentication = CoreAuthenticationTestUtils.getAuthentication();
    WebUtils.putAuthentication(authentication, context);
    val builder = mock(AuthenticationResultBuilder.class);
    when(builder.getInitialAuthentication()).thenReturn(Optional.of(authentication));
    when(builder.collect(any(Authentication.class))).thenReturn(builder);
    WebUtils.putAuthenticationResultBuilder(builder, context);
}
Also used : lombok.val(lombok.val) MockFlowSession(org.springframework.webflow.test.MockFlowSession) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) Flow(org.springframework.webflow.engine.Flow) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Authentication(org.apereo.cas.authentication.Authentication) MockParameterMap(org.springframework.webflow.test.MockParameterMap) RequestContext(org.springframework.webflow.execution.RequestContext) MessageContext(org.springframework.binding.message.MessageContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

lombok.val (lombok.val)9 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)9 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)9 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)9 Flow (org.springframework.webflow.engine.Flow)9 MockFlowExecutionContext (org.springframework.webflow.test.MockFlowExecutionContext)9 MockFlowSession (org.springframework.webflow.test.MockFlowSession)9 Test (org.junit.jupiter.api.Test)7 MockRequestContext (org.springframework.webflow.test.MockRequestContext)7 MockServletContext (org.springframework.mock.web.MockServletContext)6 MockServletContext (org.apereo.cas.util.MockServletContext)3 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)2 MessageContext (org.springframework.binding.message.MessageContext)2 ViewState (org.springframework.webflow.engine.ViewState)2 MockParameterMap (org.springframework.webflow.test.MockParameterMap)2 StandardCharsets (java.nio.charset.StandardCharsets)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 HashMap (java.util.HashMap)1 List (java.util.List)1