use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties 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());
}
}
use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.
the class DefaultCaptchaActivationStrategyTests method verifyByProps.
@Test
public void verifyByProps() {
val strategy = new DefaultCaptchaActivationStrategy(mock(ServicesManager.class));
val context = getRequestContext(new MockHttpServletRequest());
val properties = new GoogleRecaptchaProperties().setEnabled(true);
assertTrue(strategy.shouldActivate(context, properties).isPresent());
properties.setEnabled(false);
assertTrue(strategy.shouldActivate(context, properties).isEmpty());
}
use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.
the class DefaultCaptchaActivationStrategyTests method verifyByIpPattern.
@Test
public void verifyByIpPattern() {
val strategy = new DefaultCaptchaActivationStrategy(mock(ServicesManager.class));
val request = new MockHttpServletRequest();
val context = getRequestContext(request);
val properties = new GoogleRecaptchaProperties().setEnabled(true).setActivateForIpAddressPattern("127.+");
request.setRemoteAddr("185.86.151.11");
request.setLocalAddr("195.88.151.11");
ClientInfoHolder.setClientInfo(new ClientInfo(request));
assertFalse(strategy.shouldActivate(context, properties).isPresent());
}
use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties in project cas by apereo.
the class DefaultCaptchaActivationStrategyTests method verifyByService.
@Test
public void verifyByService() {
val servicesManager = mock(ServicesManager.class);
val strategy = new DefaultCaptchaActivationStrategy(servicesManager);
val context = getRequestContext(new MockHttpServletRequest());
val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
val registeredService = RegisteredServiceTestUtils.getRegisteredService(service.getId());
registeredService.getProperties().put(RegisteredServiceProperty.RegisteredServiceProperties.CAPTCHA_ENABLED.getPropertyName(), new DefaultRegisteredServiceProperty("true"));
when(servicesManager.findServiceBy(any(Service.class))).thenReturn(registeredService);
WebUtils.putServiceIntoFlowScope(context, service);
val properties = new GoogleRecaptchaProperties().setEnabled(false);
assertTrue(strategy.shouldActivate(context, properties).isPresent());
}
use of org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties 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"));
}
}
Aggregations