Update per review
This commit is contained in:
parent
6f992915d7
commit
0bd2b342d7
6 changed files with 62 additions and 63 deletions
|
@ -17,7 +17,7 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.keycloak.testsuite.validation;
|
package org.keycloak.validate;
|
||||||
|
|
||||||
import org.keycloak.validate.validators.DoubleValidator;
|
import org.keycloak.validate.validators.DoubleValidator;
|
||||||
import org.keycloak.validate.validators.EmailValidator;
|
import org.keycloak.validate.validators.EmailValidator;
|
||||||
|
@ -34,7 +34,7 @@ import org.keycloak.validate.validators.ValidatorConfigValidator;
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||||
*/
|
*/
|
||||||
public class SimpleValidators {
|
public class BuiltinValidators {
|
||||||
|
|
||||||
public static NotBlankValidator notBlankValidator() {
|
public static NotBlankValidator notBlankValidator() {
|
||||||
return NotBlankValidator.INSTANCE;
|
return NotBlankValidator.INSTANCE;
|
|
@ -8,4 +8,3 @@ org.keycloak.validate.validators.DoubleValidator
|
||||||
org.keycloak.validate.validators.IntegerValidator
|
org.keycloak.validate.validators.IntegerValidator
|
||||||
org.keycloak.validate.validators.LocalDateValidator
|
org.keycloak.validate.validators.LocalDateValidator
|
||||||
org.keycloak.validate.validators.OptionsValidator
|
org.keycloak.validate.validators.OptionsValidator
|
||||||
org.keycloak.validate.validators.ValidatorConfigValidator
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package org.keycloak.userprofile.validator;
|
package org.keycloak.userprofile.validator;
|
||||||
|
|
||||||
import static org.keycloak.common.util.CollectionUtil.collectionEquals;
|
import static org.keycloak.common.util.CollectionUtil.collectionEquals;
|
||||||
|
import static org.keycloak.validate.BuiltinValidators.notBlankValidator;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -30,7 +31,6 @@ import org.keycloak.validate.SimpleValidator;
|
||||||
import org.keycloak.validate.ValidationContext;
|
import org.keycloak.validate.ValidationContext;
|
||||||
import org.keycloak.validate.ValidationError;
|
import org.keycloak.validate.ValidationError;
|
||||||
import org.keycloak.validate.ValidatorConfig;
|
import org.keycloak.validate.ValidatorConfig;
|
||||||
import org.keycloak.validate.validators.NotBlankValidator;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A validator that fails when the attribute is marked as read only and its value has changed.
|
* A validator that fails when the attribute is marked as read only and its value has changed.
|
||||||
|
@ -62,7 +62,7 @@ public class ImmutableAttributeValidator implements SimpleValidator {
|
||||||
List<String> values = (List<String>) input;
|
List<String> values = (List<String>) input;
|
||||||
|
|
||||||
if (!collectionEquals(currentValue, values) && isReadOnly(attributeContext)) {
|
if (!collectionEquals(currentValue, values) && isReadOnly(attributeContext)) {
|
||||||
if (currentValue.isEmpty() && !NotBlankValidator.INSTANCE.validate(values).isValid()) {
|
if (currentValue.isEmpty() && !notBlankValidator().validate(values).isValid()) {
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,6 @@ import org.junit.Test;
|
||||||
import org.keycloak.models.KeycloakSession;
|
import org.keycloak.models.KeycloakSession;
|
||||||
import org.keycloak.representations.idm.RealmRepresentation;
|
import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
import org.keycloak.testsuite.AbstractKeycloakTest;
|
import org.keycloak.testsuite.AbstractKeycloakTest;
|
||||||
import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
|
|
||||||
import org.keycloak.testsuite.arquillian.annotation.ModelTest;
|
import org.keycloak.testsuite.arquillian.annotation.ModelTest;
|
||||||
import org.keycloak.validate.AbstractSimpleValidator;
|
import org.keycloak.validate.AbstractSimpleValidator;
|
||||||
import org.keycloak.validate.ValidationContext;
|
import org.keycloak.validate.ValidationContext;
|
||||||
|
@ -47,6 +46,7 @@ import org.keycloak.validate.validators.IntegerValidator;
|
||||||
import org.keycloak.validate.validators.LengthValidator;
|
import org.keycloak.validate.validators.LengthValidator;
|
||||||
import org.keycloak.validate.validators.OptionsValidator;
|
import org.keycloak.validate.validators.OptionsValidator;
|
||||||
import org.keycloak.validate.validators.PatternValidator;
|
import org.keycloak.validate.validators.PatternValidator;
|
||||||
|
import org.keycloak.validate.BuiltinValidators;
|
||||||
import org.keycloak.validate.validators.UriValidator;
|
import org.keycloak.validate.validators.UriValidator;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
@ -62,7 +62,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
public void testLengthValidator() {
|
public void testLengthValidator() {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.lengthValidator();
|
Validator validator = BuiltinValidators.lengthValidator();
|
||||||
|
|
||||||
// null and empty values handling
|
// null and empty values handling
|
||||||
Assert.assertFalse(validator.validate(null, "name", configFromMap(ImmutableMap.of(LengthValidator.KEY_MIN, 1))).isValid());
|
Assert.assertFalse(validator.validate(null, "name", configFromMap(ImmutableMap.of(LengthValidator.KEY_MIN, 1))).isValid());
|
||||||
|
@ -124,7 +124,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
// invalid min and max config values
|
// invalid min and max config values
|
||||||
ValidatorConfig config = new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, new Object(), LengthValidator.KEY_MAX, "invalid"));
|
ValidatorConfig config = new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, new Object(), LengthValidator.KEY_MAX, "invalid"));
|
||||||
|
|
||||||
ValidationResult result = SimpleValidators.validatorConfigValidator().validate(config, LengthValidator.ID, new ValidationContext(session)).toResult();
|
ValidationResult result = BuiltinValidators.validatorConfigValidator().validate(config, LengthValidator.ID, new ValidationContext(session)).toResult();
|
||||||
|
|
||||||
Assert.assertFalse(result.isValid());
|
Assert.assertFalse(result.isValid());
|
||||||
ValidationError[] errors = result.getErrors().toArray(new ValidationError[0]);
|
ValidationError[] errors = result.getErrors().toArray(new ValidationError[0]);
|
||||||
|
@ -140,25 +140,25 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
Assert.assertEquals(LengthValidator.KEY_MAX, error1.getInputHint());
|
Assert.assertEquals(LengthValidator.KEY_MAX, error1.getInputHint());
|
||||||
|
|
||||||
// empty config
|
// empty config
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(null, LengthValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(null, LengthValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertEquals(2, result.getErrors().size());
|
Assert.assertEquals(2, result.getErrors().size());
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(ValidatorConfig.EMPTY, LengthValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(ValidatorConfig.EMPTY, LengthValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertEquals(2, result.getErrors().size());
|
Assert.assertEquals(2, result.getErrors().size());
|
||||||
|
|
||||||
// correct config
|
// correct config
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, "10")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, "10")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MAX, "10")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MAX, "10")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, "10", LengthValidator.KEY_MAX, "10")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, "10", LengthValidator.KEY_MAX, "10")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
|
|
||||||
// max is smaller than min
|
// max is smaller than min
|
||||||
Assert.assertFalse(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, "10", LengthValidator.KEY_MAX, "9")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertFalse(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(LengthValidator.KEY_MIN, "10", LengthValidator.KEY_MAX, "9")), LengthValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmailValidator() {
|
public void testEmailValidator() {
|
||||||
// this also validates StringFormatValidatorBase for simple values
|
// this also validates StringFormatValidatorBase for simple values
|
||||||
|
|
||||||
Validator validator = SimpleValidators.emailValidator();
|
Validator validator = BuiltinValidators.emailValidator();
|
||||||
|
|
||||||
Assert.assertFalse(validator.validate(null, "email").isValid());
|
Assert.assertFalse(validator.validate(null, "email").isValid());
|
||||||
Assert.assertFalse(validator.validate("", "email").isValid());
|
Assert.assertFalse(validator.validate("", "email").isValid());
|
||||||
|
@ -191,7 +191,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
public void testAbstractSimpleValidatorSupportForCollections() {
|
public void testAbstractSimpleValidatorSupportForCollections() {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.emailValidator();
|
Validator validator = BuiltinValidators.emailValidator();
|
||||||
|
|
||||||
List<String> valuesCollection = new ArrayList<>();
|
List<String> valuesCollection = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -215,7 +215,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
public void testNotBlankValidator() {
|
public void testNotBlankValidator() {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.notBlankValidator();
|
Validator validator = BuiltinValidators.notBlankValidator();
|
||||||
|
|
||||||
// simple String value
|
// simple String value
|
||||||
Assert.assertTrue(validator.validate("tester", "username").isValid());
|
Assert.assertTrue(validator.validate("tester", "username").isValid());
|
||||||
|
@ -238,7 +238,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
public void testNotEmptyValidator() {
|
public void testNotEmptyValidator() {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.notEmptyValidator();
|
Validator validator = BuiltinValidators.notEmptyValidator();
|
||||||
|
|
||||||
Assert.assertTrue(validator.validate("tester", "username").isValid());
|
Assert.assertTrue(validator.validate("tester", "username").isValid());
|
||||||
Assert.assertTrue(validator.validate(" ", "username").isValid());
|
Assert.assertTrue(validator.validate(" ", "username").isValid());
|
||||||
|
@ -256,7 +256,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
public void testDoubleValidator() {
|
public void testDoubleValidator() {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.doubleValidator();
|
Validator validator = BuiltinValidators.doubleValidator();
|
||||||
|
|
||||||
// null value and empty String
|
// null value and empty String
|
||||||
Assert.assertFalse(validator.validate(null, "null").isValid());
|
Assert.assertFalse(validator.validate(null, "null").isValid());
|
||||||
|
@ -326,7 +326,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
// invalid min and max config values
|
// invalid min and max config values
|
||||||
ValidatorConfig config = new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, new Object(), DoubleValidator.KEY_MAX, "invalid"));
|
ValidatorConfig config = new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, new Object(), DoubleValidator.KEY_MAX, "invalid"));
|
||||||
|
|
||||||
ValidationResult result = SimpleValidators.validatorConfigValidator().validate(config, DoubleValidator.ID, new ValidationContext(session)).toResult();
|
ValidationResult result = BuiltinValidators.validatorConfigValidator().validate(config, DoubleValidator.ID, new ValidationContext(session)).toResult();
|
||||||
|
|
||||||
Assert.assertFalse(result.isValid());
|
Assert.assertFalse(result.isValid());
|
||||||
ValidationError[] errors = result.getErrors().toArray(new ValidationError[0]);
|
ValidationError[] errors = result.getErrors().toArray(new ValidationError[0]);
|
||||||
|
@ -342,23 +342,23 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
Assert.assertEquals(DoubleValidator.KEY_MAX, error1.getInputHint());
|
Assert.assertEquals(DoubleValidator.KEY_MAX, error1.getInputHint());
|
||||||
|
|
||||||
// empty config
|
// empty config
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(null, DoubleValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(null, DoubleValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertEquals(0, result.getErrors().size());
|
Assert.assertEquals(0, result.getErrors().size());
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(ValidatorConfig.EMPTY, DoubleValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(ValidatorConfig.EMPTY, DoubleValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertEquals(0, result.getErrors().size());
|
Assert.assertEquals(0, result.getErrors().size());
|
||||||
|
|
||||||
// correct config
|
// correct config
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, "10.1")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, "10.1")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MAX, "10.1")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MAX, "10.1")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, "10.1", DoubleValidator.KEY_MAX, "11")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, "10.1", DoubleValidator.KEY_MAX, "11")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
|
|
||||||
// max is smaller than min
|
// max is smaller than min
|
||||||
Assert.assertFalse(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, "10.1", DoubleValidator.KEY_MAX, "10.1")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertFalse(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(DoubleValidator.KEY_MIN, "10.1", DoubleValidator.KEY_MAX, "10.1")), DoubleValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIntegerValidator() {
|
public void testIntegerValidator() {
|
||||||
Validator validator = SimpleValidators.integerValidator();
|
Validator validator = BuiltinValidators.integerValidator();
|
||||||
|
|
||||||
// null value and empty String
|
// null value and empty String
|
||||||
Assert.assertFalse(validator.validate(null, "null").isValid());
|
Assert.assertFalse(validator.validate(null, "null").isValid());
|
||||||
|
@ -429,7 +429,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
// invalid min and max config values
|
// invalid min and max config values
|
||||||
ValidatorConfig config = new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, new Object(), IntegerValidator.KEY_MAX, "invalid"));
|
ValidatorConfig config = new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, new Object(), IntegerValidator.KEY_MAX, "invalid"));
|
||||||
|
|
||||||
ValidationResult result = SimpleValidators.validatorConfigValidator().validate(config, IntegerValidator.ID, new ValidationContext(session)).toResult();
|
ValidationResult result = BuiltinValidators.validatorConfigValidator().validate(config, IntegerValidator.ID, new ValidationContext(session)).toResult();
|
||||||
|
|
||||||
Assert.assertFalse(result.isValid());
|
Assert.assertFalse(result.isValid());
|
||||||
ValidationError[] errors = result.getErrors().toArray(new ValidationError[0]);
|
ValidationError[] errors = result.getErrors().toArray(new ValidationError[0]);
|
||||||
|
@ -445,24 +445,24 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
Assert.assertEquals(IntegerValidator.KEY_MAX, error1.getInputHint());
|
Assert.assertEquals(IntegerValidator.KEY_MAX, error1.getInputHint());
|
||||||
|
|
||||||
// empty config
|
// empty config
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(null, IntegerValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(null, IntegerValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertEquals(0, result.getErrors().size());
|
Assert.assertEquals(0, result.getErrors().size());
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(ValidatorConfig.EMPTY, IntegerValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(ValidatorConfig.EMPTY, IntegerValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertEquals(0, result.getErrors().size());
|
Assert.assertEquals(0, result.getErrors().size());
|
||||||
|
|
||||||
// correct config
|
// correct config
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, "10")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, "10")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MAX, "10")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MAX, "10")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
Assert.assertTrue(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, "10", IntegerValidator.KEY_MAX, "11")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertTrue(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, "10", IntegerValidator.KEY_MAX, "11")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
|
|
||||||
// max is smaller than min
|
// max is smaller than min
|
||||||
Assert.assertFalse(SimpleValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, "10", IntegerValidator.KEY_MAX, "10")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
Assert.assertFalse(BuiltinValidators.validatorConfigValidator().validate(new ValidatorConfig(ImmutableMap.of(IntegerValidator.KEY_MIN, "10", IntegerValidator.KEY_MAX, "10")), IntegerValidator.ID, new ValidationContext(session)).toResult().isValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPatternValidator() {
|
public void testPatternValidator() {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.patternValidator();
|
Validator validator = BuiltinValidators.patternValidator();
|
||||||
|
|
||||||
// Pattern object in the configuration
|
// Pattern object in the configuration
|
||||||
ValidatorConfig config = configFromMap(Collections.singletonMap(PatternValidator.CFG_PATTERN, Pattern.compile("^start-.*-end$")));
|
ValidatorConfig config = configFromMap(Collections.singletonMap(PatternValidator.CFG_PATTERN, Pattern.compile("^start-.*-end$")));
|
||||||
|
@ -493,7 +493,7 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
public void testUriValidator() throws Exception {
|
public void testUriValidator() throws Exception {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.uriValidator();
|
Validator validator = BuiltinValidators.uriValidator();
|
||||||
|
|
||||||
Assert.assertTrue(validator.validate(null, "baseUrl").isValid());
|
Assert.assertTrue(validator.validate(null, "baseUrl").isValid());
|
||||||
Assert.assertTrue(validator.validate("", "baseUrl").isValid());
|
Assert.assertTrue(validator.validate("", "baseUrl").isValid());
|
||||||
|
@ -509,14 +509,14 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
Assert.assertFalse(validator.validate("https://localhost:3000/#someFragment", "baseUrl", config).isValid());
|
Assert.assertFalse(validator.validate("https://localhost:3000/#someFragment", "baseUrl", config).isValid());
|
||||||
|
|
||||||
// it is also possible to call dedicated validation methods on a built-in validator
|
// it is also possible to call dedicated validation methods on a built-in validator
|
||||||
Assert.assertTrue(SimpleValidators.uriValidator().validateUri(new URI("https://customurl"), Collections.singleton("https"), true, true));
|
Assert.assertTrue(BuiltinValidators.uriValidator().validateUri(new URI("https://customurl"), Collections.singleton("https"), true, true));
|
||||||
|
|
||||||
Assert.assertFalse(SimpleValidators.uriValidator().validateUri(new URI("http://customurl"), Collections.singleton("https"), true, true));
|
Assert.assertFalse(BuiltinValidators.uriValidator().validateUri(new URI("http://customurl"), Collections.singleton("https"), true, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOptionsValidator(){
|
public void testOptionsValidator(){
|
||||||
Validator validator = SimpleValidators.optionsValidator();
|
Validator validator = BuiltinValidators.optionsValidator();
|
||||||
|
|
||||||
// options not configured - always invalid
|
// options not configured - always invalid
|
||||||
Assert.assertFalse(validator.validate(null, "test", ValidatorConfig.builder().config(OptionsValidator.KEY_OPTIONS, null).build()).isValid());
|
Assert.assertFalse(validator.validate(null, "test", ValidatorConfig.builder().config(OptionsValidator.KEY_OPTIONS, null).build()).isValid());
|
||||||
|
@ -557,14 +557,14 @@ public class BuiltinValidatorsTest extends AbstractKeycloakTest {
|
||||||
@ModelTest
|
@ModelTest
|
||||||
public void testOptionsValidator_Config_Validation(KeycloakSession session) {
|
public void testOptionsValidator_Config_Validation(KeycloakSession session) {
|
||||||
|
|
||||||
ValidationResult result = SimpleValidators.validatorConfigValidator().validate(ValidatorConfig.builder().build(), OptionsValidator.ID, new ValidationContext(session)).toResult();
|
ValidationResult result = BuiltinValidators.validatorConfigValidator().validate(ValidatorConfig.builder().build(), OptionsValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertFalse(result.isValid());
|
Assert.assertFalse(result.isValid());
|
||||||
|
|
||||||
// invalid type of the config value
|
// invalid type of the config value
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(ValidatorConfig.builder().config(OptionsValidator.KEY_OPTIONS, "a").build(), OptionsValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(ValidatorConfig.builder().config(OptionsValidator.KEY_OPTIONS, "a").build(), OptionsValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertFalse(result.isValid());
|
Assert.assertFalse(result.isValid());
|
||||||
|
|
||||||
result = SimpleValidators.validatorConfigValidator().validate(ValidatorConfig.builder().config(OptionsValidator.KEY_OPTIONS, Arrays.asList("opt1")).build(), OptionsValidator.ID, new ValidationContext(session)).toResult();
|
result = BuiltinValidators.validatorConfigValidator().validate(ValidatorConfig.builder().config(OptionsValidator.KEY_OPTIONS, Arrays.asList("opt1")).build(), OptionsValidator.ID, new ValidationContext(session)).toResult();
|
||||||
Assert.assertTrue(result.isValid());
|
Assert.assertTrue(result.isValid());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@ import static org.junit.Assert.assertTrue;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.keycloak.models.KeycloakSession;
|
import org.keycloak.models.KeycloakSession;
|
||||||
import org.keycloak.models.RealmModel;
|
import org.keycloak.models.RealmModel;
|
||||||
|
@ -34,7 +33,7 @@ import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
|
import org.keycloak.testsuite.AbstractTestRealmKeycloakTest;
|
||||||
import org.keycloak.testsuite.runonserver.RunOnServer;
|
import org.keycloak.testsuite.runonserver.RunOnServer;
|
||||||
import org.keycloak.validate.ValidationContext;
|
import org.keycloak.validate.ValidationContext;
|
||||||
import org.keycloak.validate.Validators;
|
import org.keycloak.validate.BuiltinValidators;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
||||||
|
@ -51,21 +50,21 @@ public class ValidatorTest extends AbstractTestRealmKeycloakTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void testDateValidator(KeycloakSession session) {
|
private static void testDateValidator(KeycloakSession session) {
|
||||||
assertTrue(SimpleValidators.dateValidator().validate(null, new ValidationContext(session)).isValid());
|
assertTrue(BuiltinValidators.dateValidator().validate(null, new ValidationContext(session)).isValid());
|
||||||
assertTrue(SimpleValidators.dateValidator().validate("", new ValidationContext(session)).isValid());
|
assertTrue(BuiltinValidators.dateValidator().validate("", new ValidationContext(session)).isValid());
|
||||||
|
|
||||||
// defaults to Locale.ENGLISH as per default locale selector
|
// defaults to Locale.ENGLISH as per default locale selector
|
||||||
assertFalse(SimpleValidators.dateValidator().validate("13/12/2021", new ValidationContext(session)).isValid());
|
assertFalse(BuiltinValidators.dateValidator().validate("13/12/2021", new ValidationContext(session)).isValid());
|
||||||
assertFalse(SimpleValidators.dateValidator().validate("13/12/21", new ValidationContext(session)).isValid());
|
assertFalse(BuiltinValidators.dateValidator().validate("13/12/21", new ValidationContext(session)).isValid());
|
||||||
assertTrue(SimpleValidators.dateValidator().validate("12/13/2021", new ValidationContext(session)).isValid());
|
assertTrue(BuiltinValidators.dateValidator().validate("12/13/2021", new ValidationContext(session)).isValid());
|
||||||
RealmModel realm = session.getContext().getRealm();
|
RealmModel realm = session.getContext().getRealm();
|
||||||
|
|
||||||
realm.setInternationalizationEnabled(true);
|
realm.setInternationalizationEnabled(true);
|
||||||
realm.setDefaultLocale(Locale.FRANCE.getLanguage());
|
realm.setDefaultLocale(Locale.FRANCE.getLanguage());
|
||||||
|
|
||||||
assertTrue(SimpleValidators.dateValidator().validate("13/12/21", new ValidationContext(session)).isValid());
|
assertTrue(BuiltinValidators.dateValidator().validate("13/12/21", new ValidationContext(session)).isValid());
|
||||||
assertTrue(SimpleValidators.dateValidator().validate("13/12/2021", new ValidationContext(session)).isValid());
|
assertTrue(BuiltinValidators.dateValidator().validate("13/12/2021", new ValidationContext(session)).isValid());
|
||||||
assertFalse(SimpleValidators.dateValidator().validate("12/13/2021", new ValidationContext(session)).isValid());
|
assertFalse(BuiltinValidators.dateValidator().validate("12/13/2021", new ValidationContext(session)).isValid());
|
||||||
|
|
||||||
UserModel alice = session.users().getUserByUsername(realm, "alice");
|
UserModel alice = session.users().getUserByUsername(realm, "alice");
|
||||||
|
|
||||||
|
@ -75,6 +74,6 @@ public class ValidatorTest extends AbstractTestRealmKeycloakTest {
|
||||||
|
|
||||||
context.getAttributes().put(UserModel.class.getName(), alice);
|
context.getAttributes().put(UserModel.class.getName(), alice);
|
||||||
|
|
||||||
assertFalse(SimpleValidators.dateValidator().validate("13/12/2021", context).isValid());
|
assertFalse(BuiltinValidators.dateValidator().validate("13/12/2021", context).isValid());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ import org.keycloak.validate.Validators;
|
||||||
import org.keycloak.validate.validators.EmailValidator;
|
import org.keycloak.validate.validators.EmailValidator;
|
||||||
import org.keycloak.validate.validators.LengthValidator;
|
import org.keycloak.validate.validators.LengthValidator;
|
||||||
import org.keycloak.validate.validators.NotBlankValidator;
|
import org.keycloak.validate.validators.NotBlankValidator;
|
||||||
|
import org.keycloak.validate.BuiltinValidators;
|
||||||
import org.keycloak.validate.validators.ValidatorConfigValidator;
|
import org.keycloak.validate.validators.ValidatorConfigValidator;
|
||||||
|
|
||||||
public class ValidatorsTest extends AbstractKeycloakTest {
|
public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
|
@ -57,7 +58,7 @@ public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
public void simpleValidation() {
|
public void simpleValidation() {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.notEmptyValidator();
|
Validator validator = BuiltinValidators.notEmptyValidator();
|
||||||
|
|
||||||
Assert.assertTrue(validator.validate("a").isValid());
|
Assert.assertTrue(validator.validate("a").isValid());
|
||||||
Assert.assertFalse(validator.validate("").isValid());
|
Assert.assertFalse(validator.validate("").isValid());
|
||||||
|
@ -67,7 +68,7 @@ public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
@ModelTest
|
@ModelTest
|
||||||
public void simpleValidationWithContext(KeycloakSession session) {
|
public void simpleValidationWithContext(KeycloakSession session) {
|
||||||
|
|
||||||
Validator validator = SimpleValidators.lengthValidator();
|
Validator validator = BuiltinValidators.lengthValidator();
|
||||||
|
|
||||||
ValidationContext context = new ValidationContext(session);
|
ValidationContext context = new ValidationContext(session);
|
||||||
validator.validate("a", "username", context);
|
validator.validate("a", "username", context);
|
||||||
|
@ -82,7 +83,7 @@ public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
|
|
||||||
ValidationContext context = new ValidationContext(session);
|
ValidationContext context = new ValidationContext(session);
|
||||||
|
|
||||||
ValidationResult result = SimpleValidators.lengthValidator().validate("a", "username", context).toResult();
|
ValidationResult result = BuiltinValidators.lengthValidator().validate("a", "username", context).toResult();
|
||||||
|
|
||||||
Assert.assertTrue(result.isValid());
|
Assert.assertTrue(result.isValid());
|
||||||
}
|
}
|
||||||
|
@ -139,11 +140,11 @@ public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
public void acceptOnError() {
|
public void acceptOnError() {
|
||||||
|
|
||||||
AtomicBoolean bool1 = new AtomicBoolean();
|
AtomicBoolean bool1 = new AtomicBoolean();
|
||||||
SimpleValidators.notEmptyValidator().validate("a").toResult().ifNotValidAccept(r -> bool1.set(true));
|
BuiltinValidators.notEmptyValidator().validate("a").toResult().ifNotValidAccept(r -> bool1.set(true));
|
||||||
Assert.assertFalse(bool1.get());
|
Assert.assertFalse(bool1.get());
|
||||||
|
|
||||||
AtomicBoolean bool2 = new AtomicBoolean();
|
AtomicBoolean bool2 = new AtomicBoolean();
|
||||||
SimpleValidators.notEmptyValidator().validate("").toResult().ifNotValidAccept(r -> bool2.set(true));
|
BuiltinValidators.notEmptyValidator().validate("").toResult().ifNotValidAccept(r -> bool2.set(true));
|
||||||
Assert.assertTrue(bool2.get());
|
Assert.assertTrue(bool2.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,8 +187,8 @@ public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
String input = "aaa";
|
String input = "aaa";
|
||||||
String inputHint = "username";
|
String inputHint = "username";
|
||||||
|
|
||||||
SimpleValidators.lengthValidator().validate(input, inputHint, context);
|
BuiltinValidators.lengthValidator().validate(input, inputHint, context);
|
||||||
SimpleValidators.notEmptyValidator().validate(input, inputHint, context);
|
BuiltinValidators.notEmptyValidator().validate(input, inputHint, context);
|
||||||
|
|
||||||
ValidationResult result = context.toResult();
|
ValidationResult result = context.toResult();
|
||||||
|
|
||||||
|
@ -203,8 +204,8 @@ public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
String input = " ";
|
String input = " ";
|
||||||
String inputHint = "username";
|
String inputHint = "username";
|
||||||
|
|
||||||
SimpleValidators.lengthValidator().validate(input, inputHint, context, configFromMap(Collections.singletonMap(LengthValidator.KEY_MIN, 1)));
|
BuiltinValidators.lengthValidator().validate(input, inputHint, context, configFromMap(Collections.singletonMap(LengthValidator.KEY_MIN, 1)));
|
||||||
SimpleValidators.notBlankValidator().validate(input, inputHint, context);
|
BuiltinValidators.notBlankValidator().validate(input, inputHint, context);
|
||||||
|
|
||||||
ValidationResult result = context.toResult();
|
ValidationResult result = context.toResult();
|
||||||
|
|
||||||
|
@ -239,7 +240,7 @@ public class ValidatorsTest extends AbstractKeycloakTest {
|
||||||
@Test
|
@Test
|
||||||
@ModelTest
|
@ModelTest
|
||||||
public void validateEmailValidator(KeycloakSession session) {
|
public void validateEmailValidator(KeycloakSession session) {
|
||||||
SimpleValidator validator = SimpleValidators.emailValidator();
|
SimpleValidator validator = BuiltinValidators.emailValidator();
|
||||||
|
|
||||||
Assert.assertTrue(validator.validateConfig(session, null).isValid());
|
Assert.assertTrue(validator.validateConfig(session, null).isValid());
|
||||||
Assert.assertTrue(validator.validateConfig(session, ValidatorConfig.EMPTY).isValid());
|
Assert.assertTrue(validator.validateConfig(session, ValidatorConfig.EMPTY).isValid());
|
||||||
|
|
Loading…
Reference in a new issue