Search in sources :

Example 81 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class DefaultSingleSignOnParticipationStrategyTests method verifyRegisteredServiceWithValidSso.

@Test
public void verifyRegisteredServiceWithValidSso() {
    val mgr = mock(ServicesManager.class);
    val registeredService = CoreAuthenticationTestUtils.getRegisteredService();
    when(registeredService.getAccessStrategy().isServiceAccessAllowedForSso()).thenReturn(true);
    when(registeredService.getSingleSignOnParticipationPolicy()).thenReturn(new DefaultRegisteredServiceSingleSignOnParticipationPolicy());
    when(mgr.findServiceBy(any(Service.class))).thenReturn(registeredService);
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val tgt = new MockTicketGrantingTicket("casuser");
    val sso = new SingleSignOnProperties();
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    when(ticketRegistrySupport.getTicket(anyString())).thenReturn(tgt);
    val strategy = new DefaultSingleSignOnParticipationStrategy(mgr, sso, ticketRegistrySupport, mock(AuthenticationServiceSelectionPlan.class));
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putRegisteredService(context, registeredService);
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication("casuser"), context);
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    val ssoRequest = SingleSignOnParticipationRequest.builder().httpServletRequest(request).requestContext(context).build();
    assertTrue(strategy.isParticipating(ssoRequest));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) DefaultRegisteredServiceSingleSignOnParticipationPolicy(org.apereo.cas.services.DefaultRegisteredServiceSingleSignOnParticipationPolicy) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Service(org.apereo.cas.authentication.principal.Service) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) SingleSignOnProperties(org.apereo.cas.configuration.model.core.sso.SingleSignOnProperties) Test(org.junit.jupiter.api.Test)

Example 82 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class DefaultSingleSignOnParticipationStrategyTests method verifyRegisteredServiceWithValidSsoAndServiceExpPolicy.

@Test
public void verifyRegisteredServiceWithValidSsoAndServiceExpPolicy() {
    val mgr = mock(ServicesManager.class);
    val registeredService = CoreAuthenticationTestUtils.getRegisteredService();
    when(registeredService.getAccessStrategy().isServiceAccessAllowedForSso()).thenReturn(true);
    when(registeredService.getTicketGrantingTicketExpirationPolicy()).thenReturn(new DefaultRegisteredServiceTicketGrantingTicketExpirationPolicy(1));
    when(mgr.findServiceBy(any(Service.class))).thenReturn(registeredService);
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val tgt = new MockTicketGrantingTicket("casuser");
    tgt.setCreated(ZonedDateTime.now(ZoneOffset.UTC).minusHours(1));
    val sso = new SingleSignOnProperties();
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    when(ticketRegistrySupport.getTicket(anyString())).thenReturn(tgt);
    val strategy = new DefaultSingleSignOnParticipationStrategy(mgr, sso, ticketRegistrySupport, mock(AuthenticationServiceSelectionPlan.class));
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putRegisteredService(context, registeredService);
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication("casuser"), context);
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    val ssoRequest = SingleSignOnParticipationRequest.builder().httpServletRequest(request).requestContext(context).build();
    assertFalse(strategy.isParticipating(ssoRequest));
}
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) DefaultRegisteredServiceTicketGrantingTicketExpirationPolicy(org.apereo.cas.services.DefaultRegisteredServiceTicketGrantingTicketExpirationPolicy) Service(org.apereo.cas.authentication.principal.Service) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) SingleSignOnProperties(org.apereo.cas.configuration.model.core.sso.SingleSignOnProperties) Test(org.junit.jupiter.api.Test)

Example 83 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class RankedMultifactorAuthenticationProviderWebflowEventResolverTests method verifyAuthnHandledWithRenew.

@Test
public void verifyAuthnHandledWithRenew() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.addParameter(CasProtocolConstants.PARAMETER_RENEW, "true");
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService());
    val service = RegisteredServiceTestUtils.getRegisteredService(Map.of());
    servicesManager.save(service);
    WebUtils.putRegisteredService(context, service);
    val tgt = new MockTicketGrantingTicket("casuser");
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    cas.addTicket(tgt);
    WebUtils.putCredential(context, RegisteredServiceTestUtils.getCredentialsWithDifferentUsernameAndPassword("casuser", "Mellon"));
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, resolver.resolve(context).iterator().next().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) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 84 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class RankedMultifactorAuthenticationProviderWebflowEventResolverTests method verifyAuthnResolvesMfaEvent.

@Test
public void verifyAuthnResolvesMfaEvent() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.addParameter(casProperties.getAuthn().getMfa().getTriggers().getHttp().getRequestParameter(), TestMultifactorAuthenticationProvider.ID);
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService());
    val service = RegisteredServiceTestUtils.getRegisteredService(Map.of());
    servicesManager.save(service);
    WebUtils.putRegisteredService(context, service);
    val tgt = new MockTicketGrantingTicket("casuser");
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    cas.addTicket(tgt);
    TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    WebUtils.putCredential(context, RegisteredServiceTestUtils.getCredentialsWithDifferentUsernameAndPassword("casuser", "Mellon"));
    val targetResolver = new DefaultTargetStateResolver(TestMultifactorAuthenticationProvider.ID);
    val transition = new Transition(new DefaultTransitionCriteria(new LiteralExpression(TestMultifactorAuthenticationProvider.ID)), targetResolver);
    context.getRootFlow().getGlobalTransitionSet().add(transition);
    assertEquals(TestMultifactorAuthenticationProvider.ID, resolver.resolveSingle(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) DefaultTransitionCriteria(org.springframework.webflow.engine.support.DefaultTransitionCriteria) LiteralExpression(org.springframework.binding.expression.support.LiteralExpression) Transition(org.springframework.webflow.engine.Transition) DefaultTargetStateResolver(org.springframework.webflow.engine.support.DefaultTargetStateResolver) 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 85 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class RankedMultifactorAuthenticationProviderWebflowEventResolverTests method verifyAuthnResolvesMfaContextValidated.

@Test
public void verifyAuthnResolvesMfaContextValidated() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.addParameter(casProperties.getAuthn().getMfa().getTriggers().getHttp().getRequestParameter(), TestMultifactorAuthenticationProvider.ID);
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val tgt = new MockTicketGrantingTicket("casuser", Map.of(), Map.of(casProperties.getAuthn().getMfa().getCore().getAuthenticationContextAttribute(), List.of(TestMultifactorAuthenticationProvider.ID)));
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    cas.addTicket(tgt);
    TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext);
    WebUtils.putCredential(context, RegisteredServiceTestUtils.getCredentialsWithDifferentUsernameAndPassword("casuser", "Mellon"));
    val targetResolver = new DefaultTargetStateResolver(TestMultifactorAuthenticationProvider.ID);
    val transition = new Transition(new DefaultTransitionCriteria(new LiteralExpression(TestMultifactorAuthenticationProvider.ID)), targetResolver);
    context.getRootFlow().getGlobalTransitionSet().add(transition);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, resolver.resolve(context).iterator().next().getId());
    val registeredService = RegisteredServiceTestUtils.getRegisteredService(Map.of());
    val multifactorPolicy = new DefaultRegisteredServiceMultifactorPolicy();
    multifactorPolicy.setForceExecution(true);
    registeredService.setMultifactorPolicy(multifactorPolicy);
    WebUtils.putRegisteredService(context, registeredService);
    assertEquals(TestMultifactorAuthenticationProvider.ID, resolver.resolveSingle(context).getId());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) DefaultRegisteredServiceMultifactorPolicy(org.apereo.cas.services.DefaultRegisteredServiceMultifactorPolicy) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DefaultTransitionCriteria(org.springframework.webflow.engine.support.DefaultTransitionCriteria) LiteralExpression(org.springframework.binding.expression.support.LiteralExpression) Transition(org.springframework.webflow.engine.Transition) DefaultTargetStateResolver(org.springframework.webflow.engine.support.DefaultTargetStateResolver) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)224 lombok.val (lombok.val)199 Test (org.junit.jupiter.api.Test)164 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)93 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)92 MockRequestContext (org.springframework.webflow.test.MockRequestContext)42 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)39 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)36 JEEContext (org.pac4j.core.context.JEEContext)33 MockServletContext (org.springframework.mock.web.MockServletContext)33 HashMap (java.util.HashMap)31 MockServiceTicket (org.apereo.cas.mock.MockServiceTicket)18 Test (org.junit.Test)18 WebApplicationServiceFactory (org.apereo.cas.authentication.principal.WebApplicationServiceFactory)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 CasProfile (org.pac4j.cas.profile.CasProfile)11 Authentication (org.apereo.cas.authentication.Authentication)10 HardTimeoutExpirationPolicy (org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy)10 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)10 RedirectView (org.springframework.web.servlet.view.RedirectView)10