Search in sources :

Example 71 with MockWebServer

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

the class RestfulAuthenticationPolicyTests method assertPolicyFails.

private static void assertPolicyFails(final int port, final HttpStatus status, final Class<? extends Throwable> exceptionClass) {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    try (val webServer = new MockWebServer(port, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "Output"), status)) {
        webServer.start();
        val props = new RestAuthenticationPolicyProperties();
        props.setUrl("http://localhost:" + port);
        val policy = new RestfulAuthenticationPolicy(props);
        assertThrowsWithRootCause(GeneralSecurityException.class, exceptionClass, () -> policy.isSatisfiedBy(CoreAuthenticationTestUtils.getAuthentication("casuser"), new LinkedHashSet<>(), applicationContext, Optional.empty()));
    }
}
Also used : lombok.val(lombok.val) RestAuthenticationPolicyProperties(org.apereo.cas.configuration.model.core.authentication.RestAuthenticationPolicyProperties) LinkedHashSet(java.util.LinkedHashSet) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource)

Example 72 with MockWebServer

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

the class AzureActiveDirectoryAuthenticationHandlerTests method verifyOperationFailsLogin.

@Test
public void verifyOperationFailsLogin() throws Exception {
    val handler = getMockAzureActiveDirectoryAuthenticationHandler(7787);
    val entity = MAPPER.writeValueAsString(RegisteredServiceTestUtils.getTestAttributes());
    try (val webServer = new MockWebServer(7787, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.UNAUTHORIZED)) {
        webServer.start();
        val c = CoreAuthenticationTestUtils.getCredentialsWithDifferentUsernameAndPassword("castest@hotmail.onmicrosoft.com", "1234567890");
        assertThrows(FailedLoginException.class, () -> handler.authenticate(c));
    }
}
Also used : lombok.val(lombok.val) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 73 with MockWebServer

use of org.apereo.cas.util.MockWebServer 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 74 with MockWebServer

use of org.apereo.cas.util.MockWebServer 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 75 with MockWebServer

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

the class RestfulDelegatedClientFactoryTests method verifyAction.

@Test
public void verifyAction() throws Exception {
    val clients = new HashMap<String, Object>();
    clients.put("callbackUrl", "https://sso.example.org/cas/login");
    clients.put("properties", getProperties());
    val props = new CasConfigurationProperties();
    props.getAuthn().getPac4j().getRest().setUrl("http://localhost:9212");
    val factory = new RestfulDelegatedClientFactory(props);
    var clientsFound = factory.build();
    assertTrue(clientsFound.isEmpty());
    val entity = MAPPER.writeValueAsString(clients);
    try (val webServer = new MockWebServer(9212, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        clientsFound = factory.build();
        assertNotNull(clientsFound);
        assertEquals(3, clientsFound.size());
        /*
             * Try the cache once the list is retrieved...
             */
        clientsFound = factory.build();
        assertNotNull(clientsFound);
        assertEquals(3, clientsFound.size());
    }
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Aggregations

MockWebServer (org.apereo.cas.util.MockWebServer)175 lombok.val (lombok.val)173 Test (org.junit.jupiter.api.Test)157 ByteArrayResource (org.springframework.core.io.ByteArrayResource)151 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)82 MockRequestContext (org.springframework.webflow.test.MockRequestContext)36 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)31 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)26 MockServletContext (org.springframework.mock.web.MockServletContext)25 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)15 ClassPathResource (org.springframework.core.io.ClassPathResource)15 ClientInfo (org.apereo.inspektr.common.web.ClientInfo)13 GoogleAuthenticatorMultifactorProperties (org.apereo.cas.configuration.model.support.mfa.gauth.GoogleAuthenticatorMultifactorProperties)10 AdaptiveAuthenticationProperties (org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties)9 Executable (org.junit.jupiter.api.function.Executable)8 RestEndpointProperties (org.apereo.cas.configuration.model.RestEndpointProperties)7 SamlRegisteredService (org.apereo.cas.support.saml.services.SamlRegisteredService)7 Service (org.apereo.cas.authentication.principal.Service)4 GoogleRecaptchaProperties (org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties)4