Search in sources :

Example 1 with AcceptableUsagePolicyProperties

use of org.apereo.cas.configuration.model.support.aup.AcceptableUsagePolicyProperties in project cas by apereo.

the class RestAcceptableUsagePolicyRepositoryTests method verify.

@Test
public void verify() {
    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());
    WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService());
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication("casuser"), context);
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    val props = new AcceptableUsagePolicyProperties();
    props.getRest().setUrl("http://localhost:9298");
    props.getCore().setAupAttributeName("givenName");
    val r = new RestAcceptableUsagePolicyRepository(ticketRegistrySupport, props);
    val data = StringUtils.EMPTY;
    try (val webServer = new MockWebServer(9298, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        assertFalse(r.isUsagePolicyAcceptedBy(CoreAuthenticationTestUtils.getPrincipal()));
        assertTrue(r.submit(context));
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) MockRequestContext(org.springframework.webflow.test.MockRequestContext) ByteArrayResource(org.springframework.core.io.ByteArrayResource) AcceptableUsagePolicyProperties(org.apereo.cas.configuration.model.support.aup.AcceptableUsagePolicyProperties) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 2 with AcceptableUsagePolicyProperties

use of org.apereo.cas.configuration.model.support.aup.AcceptableUsagePolicyProperties in project cas by apereo.

the class RestAcceptableUsagePolicyRepositoryTests method verifyFetch.

@Test
public void verifyFetch() throws Exception {
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    val props = new AcceptableUsagePolicyProperties();
    props.getRest().setUrl("http://localhost:9198");
    props.getCore().setAupAttributeName("givenName");
    val r = new RestAcceptableUsagePolicyRepository(ticketRegistrySupport, props);
    val input = AcceptableUsagePolicyTerms.builder().code("example").defaultText("hello world").build();
    val data = MAPPER.writeValueAsString(input);
    try (val webServer = new MockWebServer(9198, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val context = new MockRequestContext();
        WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication("casuser"), context);
        val request = new MockHttpServletRequest();
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
        val terms = r.fetchPolicy(context);
        assertTrue(terms.isPresent());
        assertEquals(terms.get(), input);
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) AcceptableUsagePolicyProperties(org.apereo.cas.configuration.model.support.aup.AcceptableUsagePolicyProperties) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with AcceptableUsagePolicyProperties

use of org.apereo.cas.configuration.model.support.aup.AcceptableUsagePolicyProperties in project cas by apereo.

the class RestAcceptableUsagePolicyRepositoryTests method verifyFails.

@Test
public void verifyFails() {
    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());
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication("casuser"), context);
    WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService());
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    val props = new AcceptableUsagePolicyProperties();
    props.getRest().setUrl("http://localhost:9299");
    props.getCore().setAupAttributeName("givenName");
    val r = new RestAcceptableUsagePolicyRepository(ticketRegistrySupport, props);
    val data = StringUtils.EMPTY;
    try (val webServer = new MockWebServer(9299, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), HttpStatus.SERVICE_UNAVAILABLE)) {
        webServer.start();
        assertFalse(r.fetchPolicy(context).isPresent());
        assertFalse(r.submit(context));
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) MockRequestContext(org.springframework.webflow.test.MockRequestContext) ByteArrayResource(org.springframework.core.io.ByteArrayResource) AcceptableUsagePolicyProperties(org.apereo.cas.configuration.model.support.aup.AcceptableUsagePolicyProperties) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)3 AcceptableUsagePolicyProperties (org.apereo.cas.configuration.model.support.aup.AcceptableUsagePolicyProperties)3 MockWebServer (org.apereo.cas.util.MockWebServer)3 Test (org.junit.jupiter.api.Test)3 ByteArrayResource (org.springframework.core.io.ByteArrayResource)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 MockServletContext (org.springframework.mock.web.MockServletContext)3 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)3 MockRequestContext (org.springframework.webflow.test.MockRequestContext)3