Search in sources :

Example 6 with MockWebServer

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

the class BlackDotIPAddressIntelligenceServiceTests method verifyErrorStatusOperation.

@Test
public void verifyErrorStatusOperation() throws Exception {
    val data = MAPPER.writeValueAsString(Collections.singletonMap("status", "error"));
    try (val webServer = new MockWebServer(9356, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val props = new AdaptiveAuthenticationProperties();
        props.getIpIntel().getBlackDot().setUrl("http://localhost:9356?ip=%s");
        props.getIpIntel().getBlackDot().setEmailAddress("cas@apereo.org");
        val service = new BlackDotIPAddressIntelligenceService(props);
        val response = service.examine(new MockRequestContext(), "37.58.59.181");
        assertTrue(response.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 7 with MockWebServer

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

the class BlackDotIPAddressIntelligenceServiceTests method verifySuccessStatus.

@Test
public void verifySuccessStatus() throws Exception {
    val data = MAPPER.writeValueAsString(CollectionUtils.wrap("status", "success", "result", 0));
    try (val webServer = new MockWebServer(9358, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val props = new AdaptiveAuthenticationProperties();
        props.getIpIntel().getBlackDot().setUrl("http://localhost:9358?ip=%s");
        props.getIpIntel().getBlackDot().setEmailAddress("cas@apereo.org");
        val service = new BlackDotIPAddressIntelligenceService(props);
        val response = service.examine(new MockRequestContext(), "37.58.59.181");
        assertFalse(response.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 8 with MockWebServer

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

the class ValidateCaptchaActionTests method verifyCaptchaFails.

@Test
public void verifyCaptchaFails() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    try (val webServer = new MockWebServer(9305, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val props = new GoogleRecaptchaProperties().setVerifyUrl("http://localhost:9305");
        val validateAction = new ValidateCaptchaAction(new GoogleCaptchaV2Validator(props), captchaActivationStrategy);
        val result = validateAction.execute(context);
        assertNotNull(result);
        assertEquals(CasWebflowConstants.TRANSITION_ID_CAPTCHA_ERROR, result.getId());
    }
}
Also used : lombok.val(lombok.val) GoogleCaptchaV2Validator(org.apereo.cas.web.GoogleCaptchaV2Validator) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) MockWebServer(org.apereo.cas.util.MockWebServer) MockRequestContext(org.springframework.webflow.test.MockRequestContext) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with MockWebServer

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

the class ValidateCaptchaActionTests method verifyCaptchaValidated.

@Test
public void verifyCaptchaValidated() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val data = "{\"success\": true }";
    request.addParameter(GoogleCaptchaV2Validator.REQUEST_PARAM_RECAPTCHA_RESPONSE, data);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    try (val webServer = new MockWebServer(9294, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
        webServer.start();
        val result = validateCaptchaAction.execute(context);
        assertNull(result);
    }
}
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) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 10 with MockWebServer

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

the class CaptchaValidatorTests method verifyLowScore.

@Test
public void verifyLowScore() throws Exception {
    val secret = UUID.randomUUID().toString();
    val props = new GoogleRecaptchaProperties().setScore(1).setSecret(secret).setVerifyUrl("http://localhost:8812");
    val validator = new GoogleCaptchaV2Validator(props);
    val entity = MAPPER.writeValueAsString(Map.of("score", .5));
    try (val webServer = new MockWebServer(8812, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val response = UUID.randomUUID().toString();
        assertFalse(validator.validate(response, "Mozilla/5.0"));
    }
}
Also used : lombok.val(lombok.val) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) 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