Search in sources :

Example 1 with MockWebServer

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

the class RestSamlRegisteredServiceMetadataResolverTests method setup.

@Before
@SneakyThrows
public void setup() {
    final SamlMetadataDocument doc = new SamlMetadataDocument();
    doc.setId(1);
    doc.setName("SAML Document");
    doc.setSignature(null);
    doc.setValue(IOUtils.toString(new ClassPathResource("sp-metadata.xml").getInputStream(), StandardCharsets.UTF_8));
    final String data = MAPPER.writeValueAsString(doc);
    this.webServer = new MockWebServer(8078, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_XML_VALUE);
    this.webServer.start();
}
Also used : MockWebServer(org.apereo.cas.util.MockWebServer) SamlMetadataDocument(org.apereo.cas.support.saml.services.idp.metadata.SamlMetadataDocument) ByteArrayResource(org.springframework.core.io.ByteArrayResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before) SneakyThrows(lombok.SneakyThrows)

Example 2 with MockWebServer

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

the class RestfulPrincipalFactoryTests method verifyBadResponse.

@Test
public void verifyBadResponse() {
    try (val webServer = new MockWebServer(9157, new ByteArrayResource("abcde123456".getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val props = new RestEndpointProperties();
        props.setUrl("http://localhost:9157");
        val factory = PrincipalFactoryUtils.newRestfulPrincipalFactory(props);
        assertThrows(IllegalArgumentException.class, () -> factory.createPrincipal("casuser", CollectionUtils.wrap("name", List.of("CAS"))));
    }
}
Also used : lombok.val(lombok.val) RestEndpointProperties(org.apereo.cas.configuration.model.RestEndpointProperties) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 3 with MockWebServer

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

the class RestfulPrincipalFactoryTests method verifyAction.

@Test
public void verifyAction() throws Exception {
    val entity = MAPPER.writeValueAsString(CoreAuthenticationTestUtils.getPrincipal("casuser"));
    try (val webServer = new MockWebServer(9155, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val props = new RestEndpointProperties();
        props.setUrl("http://localhost:9155");
        val factory = PrincipalFactoryUtils.newRestfulPrincipalFactory(props);
        val p = factory.createPrincipal("casuser", CollectionUtils.wrap("name", List.of("CAS")));
        assertEquals("casuser", p.getId());
        assertEquals(5, p.getAttributes().size());
    }
}
Also used : lombok.val(lombok.val) RestEndpointProperties(org.apereo.cas.configuration.model.RestEndpointProperties) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

Example 4 with MockWebServer

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

the class RestfulIPAddressIntelligenceServiceTests method verifyBannedOperation.

@Test
public void verifyBannedOperation() {
    try (val webServer = new MockWebServer(9304, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.FORBIDDEN)) {
        webServer.start();
        val props = new AdaptiveAuthenticationProperties();
        props.getIpIntel().getRest().setUrl("http://localhost:9304");
        val service = new RestfulIPAddressIntelligenceService(props);
        val result = service.examine(new MockRequestContext(), "1.2.3.4");
        assertNotNull(result);
        assertTrue(result.isBanned());
    }
}
Also used : lombok.val(lombok.val) MockWebServer(org.apereo.cas.util.MockWebServer) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) Test(org.junit.jupiter.api.Test)

Example 5 with MockWebServer

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

the class RestfulIPAddressIntelligenceServiceTests method verifyRankedOperation.

@Test
public void verifyRankedOperation() {
    try (val webServer = new MockWebServer(9306, new ByteArrayResource("12.435".getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.PRECONDITION_REQUIRED)) {
        webServer.start();
        val props = new AdaptiveAuthenticationProperties();
        props.getIpIntel().getRest().setUrl("http://localhost:9306");
        var service = new RestfulIPAddressIntelligenceService(props);
        var result = service.examine(new MockRequestContext(), "1.2.3.4");
        assertNotNull(result);
        assertTrue(result.isRanked());
        props.getPolicy().setRejectIpAddresses("123\\..*");
        service = new RestfulIPAddressIntelligenceService(props);
        result = service.examine(new MockRequestContext(), "123.1.2.3");
        assertNotNull(result);
        assertTrue(result.isBanned());
    }
}
Also used : lombok.val(lombok.val) MockWebServer(org.apereo.cas.util.MockWebServer) AdaptiveAuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AdaptiveAuthenticationProperties) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) 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