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));
}
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));
}
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());
}
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);
}
});
}
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());
}
Aggregations