-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(validation): cleanup UserNameValidator #8531
- Reuse existing constraints instead of creating our own validator - Provide programmatic method to use the same from code like OAuth2 etc - Add much more tests, also for actual bean validation
- Loading branch information
1 parent
223d5a2
commit 85dcbbf
Showing
5 changed files
with
114 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 30 additions & 45 deletions
75
src/main/java/edu/harvard/iq/dataverse/validation/UserNameValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 65 additions & 59 deletions
124
src/test/java/edu/harvard/iq/dataverse/validation/UserNameValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,82 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package edu.harvard.iq.dataverse.validation; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import edu.harvard.iq.dataverse.validation.UserNameValidator; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
|
||
/** | ||
* | ||
* @author sarahferry | ||
* @author alexscheitlin | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class UserNameValidatorTest { | ||
|
||
public boolean isValid; | ||
public String userName; | ||
|
||
public UserNameValidatorTest(boolean isValid, String userName) { | ||
this.isValid = isValid; | ||
this.userName = userName; | ||
} | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Parameters | ||
public static Collection<Object[]> parameters() { | ||
return Arrays.asList(new Object[][] { | ||
public class UserNameValidatorTest { | ||
|
||
public static Stream<Arguments> usernameExamples() { | ||
return Stream.of( | ||
// good usernames | ||
{ true, "sarah" }, | ||
{ true, ".-_5Arah_-." }, | ||
|
||
Arguments.of(true, "sarah"), | ||
Arguments.of(true, ".-_5Arah_-."), | ||
// dont allow accents | ||
{ false, "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ" }, | ||
|
||
Arguments.of(false, "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ"), | ||
// dont allow chinese characters | ||
{ false, "谁日吧爸好" }, | ||
|
||
Arguments.of(false, "谁日吧爸好"), | ||
// dont allow middle white space | ||
{ false, "sarah f" }, | ||
|
||
Arguments.of(false, "sarah f"), | ||
// dont allow leading white space | ||
{ false, " sarah" }, | ||
|
||
Arguments.of(false, " sarah"), | ||
// dont allow trailing white space | ||
{ false, "sarah " }, | ||
|
||
Arguments.of(false, "sarah "), | ||
// dont allow symbols | ||
{ false, "sarah!" }, | ||
{ false, "sarah?" }, | ||
{ false, "sarah:(" }, | ||
{ false, "💲🅰️®️🅰️🚧" }, | ||
|
||
Arguments.of(false, "sarah!"), | ||
Arguments.of(false, "sarah?"), | ||
Arguments.of(false, "sarah:("), | ||
Arguments.of(false, "💲🅰️®️🅰️🚧"), | ||
// only allow between 2 and 60 characters | ||
{ false, "q" }, | ||
{ true, "q2" }, | ||
{ false, "q2jsalfhjopiwurtiosfhkdhasjkdhfgkfhkfrhnefcn4cqonroclmooi4oiqwhrfq4jrlqhaskdalwehrlwhflhklasdjfglq0kkajfelirhilwhakjgv" }, | ||
{ false, "" }, | ||
{ false, null } | ||
}); | ||
Arguments.of(false, "q"), | ||
Arguments.of(true, "q2"), | ||
Arguments.of(false, "q2jsalfhjopiwurtiosfhkdhasjkdhfgkfhkfrhnefcn4cqonroclmooi4oiqwhrfq4jrlqhaskdalwehrlwhflhklasdjfglq0kkajfelirhilwhakjgv"), | ||
Arguments.of(false, "q2jsalfhjopiwurtiosfhkdhasj!1! !#fhkfrhnefcn4cqonroclmooi4oiqwhrfq4jrlqhaskdalwehrlwhflhklasdjfglq0kkajfelirhilwhakjgv"), | ||
Arguments.of(false, ""), | ||
Arguments.of(false, null) | ||
); | ||
} | ||
|
||
@Test | ||
public void testIsUserNameValid() { | ||
assertEquals(isValid, UserNameValidator.isUserNameValid(userName)); | ||
|
||
@ParameterizedTest | ||
@MethodSource("usernameExamples") | ||
public void testUsernames(boolean expected, String username) { | ||
assertEquals(expected, UserNameValidator.isUserNameValid(username)); | ||
} | ||
|
||
/** | ||
* This is a simple test class, as we defined the annotation for fields only. | ||
*/ | ||
private final class SubjectClass { | ||
@ValidateUserName | ||
String username; | ||
|
||
SubjectClass(String username) { | ||
this.username = username; | ||
} | ||
} | ||
|
||
private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); | ||
|
||
@ParameterizedTest | ||
@MethodSource("usernameExamples") | ||
public void testConstraint(boolean expected, String username) { | ||
// given | ||
UserNameValidatorTest.SubjectClass sut = new UserNameValidatorTest.SubjectClass(username); | ||
|
||
//when | ||
Set<ConstraintViolation<UserNameValidatorTest.SubjectClass>> violations = validator.validate(sut); | ||
|
||
// then | ||
assertEquals(expected, violations.size() < 1); | ||
// TODO: one might add tests for the two violations (size and illegal chars) here... | ||
} | ||
|
||
} |