Search in sources :

Example 16 with MockServletContext

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

the class DelegatedClientAuthenticationActionTests method verifySsoAuthenticationUnauthz.

@Test
public void verifySsoAuthenticationUnauthz() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
    val service = CoreAuthenticationTestUtils.getService("https://delegated3.example.org");
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
    val client = builtClients.findClient("FacebookClient").get();
    val webContext = new JEEContext(request, new MockHttpServletResponse());
    val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
    request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
    val accessStrategy = new DefaultRegisteredServiceAccessStrategy();
    accessStrategy.setEnabled(false);
    val registeredService = RegisteredServiceTestUtils.getRegisteredService(service.getId());
    registeredService.setAccessStrategy(accessStrategy);
    servicesManager.save(registeredService);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    setRequestContext(context);
    setExternalContext(context.getExternalContext());
    val tgt = new MockTicketGrantingTicket("casuser", Map.of(), Map.of(ClientCredential.AUTHENTICATION_ATTRIBUTE_CLIENT_NAME, List.of("FacebookClient")));
    centralAuthenticationService.addTicket(tgt);
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    assertThrows(UnauthorizedServiceException.class, () -> delegatedAuthenticationAction.execute(context).getId());
    assertThrows(InvalidTicketException.class, () -> centralAuthenticationService.getTicket(tgt.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) JEEContext(org.pac4j.core.context.JEEContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) DefaultRegisteredServiceAccessStrategy(org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy) 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 17 with MockServletContext

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

the class DelegatedClientAuthenticationActionTests method verifyLogoutRequestWithOkAction.

@Test
public void verifyLogoutRequestWithOkAction() throws Exception {
    val request = new MockHttpServletRequest();
    request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "MockClientNoCredentials");
    request.addParameter(Pac4jConstants.LOGOUT_ENDPOINT_PARAMETER, "true");
    val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
    servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
    val context = new MockRequestContext();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    setRequestContext(context);
    setExternalContext(context.getExternalContext());
    val event = delegatedAuthenticationAction.execute(context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, event.getId());
}
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) 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 18 with MockServletContext

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

the class DelegatedAuthenticationClientLogoutActionTests method verifyOperationWithProfile.

@Test
public void verifyOperationWithProfile() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService());
    val profile = new CommonProfile();
    profile.setId("casuser");
    profile.setClientName("CasClient");
    request.setAttribute(Pac4jConstants.USER_PROFILES, CollectionUtils.wrapLinkedHashMap(profile.getClientName(), profile));
    val result = delegatedAuthenticationClientLogoutAction.execute(context);
    assertNull(result);
    assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatus());
    val tgt = new MockTicketGrantingTicket("casuser");
    logoutManager.performLogout(SingleLogoutExecutionRequest.builder().httpServletRequest(Optional.of(request)).httpServletResponse(Optional.of(response)).ticketGrantingTicket(tgt).build());
    assertNull(request.getSession(false));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) CommonProfile(org.pac4j.core.profile.CommonProfile) 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.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 19 with MockServletContext

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

the class DelegatedAuthenticationClientLogoutActionTests method verifyOperationWithNoProfile.

@Test
public void verifyOperationWithNoProfile() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val result = delegatedAuthenticationClientLogoutAction.execute(context);
    assertNull(result);
    assertNotEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatus());
}
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) 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 20 with MockServletContext

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

the class WSFederationMetadataUIActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    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 registeredService = new WSFederationRegisteredService();
    registeredService.setRealm("CAS");
    registeredService.setServiceId("http://app.example5.org/wsfed-idp");
    registeredService.setName("WSFED App");
    registeredService.setId(100);
    registeredService.setAppliesTo("CAS");
    registeredService.setWsdlLocation("classpath:wsdl/ws-trust-1.4-service.wsdl");
    servicesManager.save(registeredService);
    val service = RegisteredServiceTestUtils.getService("http://app.example5.org/wsfed-idp");
    service.getAttributes().put(WSFederationConstants.WREPLY, List.of(registeredService.getServiceId()));
    WebUtils.putServiceIntoFlowScope(context, service);
    val event = wsFederationMetadataUIAction.execute(context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, event.getId());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) WSFederationRegisteredService(org.apereo.cas.ws.idp.services.WSFederationRegisteredService) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test)

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