Skip to content

Commit

Permalink
Merge pull request #52 from Chat-Your-Way/CHAT-169-bugfix-password-fi…
Browse files Browse the repository at this point in the history
…eld-accepted-cyrillic-and-prohibited-symbols

Chat 169 bugfix password field accepted cyrillic and prohibited symbols
  • Loading branch information
Troha7 authored Jan 8, 2024
2 parents 6353d7c + ec4b6b6 commit fad05b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class PasswordValidator implements ConstraintValidator<PasswordValidation
Pattern.compile(".*[!@#$%^&*_\\-+=~?].*");
private static final Pattern PASSWORD_UPPER_CASE_PATTERN = Pattern.compile(".*[A-Z].*");
private static final Pattern PASSWORD_DIGIT_PATTERN = Pattern.compile(".*\\d.*");
private static final Pattern PASSWORD_FORBIDEN_SYMBOLS_PATTERN =
Pattern.compile(".*[<>;/.:'(),\\[\\]\"].*");
private static final Pattern PASSWORD_FORBIDEN_CYRILLIC_LETTERS_PATTERN =
Pattern.compile(".*[а-яА-ЯІіЇї].*");

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
Expand All @@ -35,7 +39,17 @@ public boolean isValid(String value, ConstraintValidatorContext context) {
.buildConstraintViolationWithTemplate("The password must not be longer than 12 characters.")
.addConstraintViolation();
return false;
} else if (!PASSWORD_SPECIAL_SYMBOLS_PATTERN.matcher(value).matches()) {
} else if (PASSWORD_FORBIDEN_SYMBOLS_PATTERN.matcher(value).matches()) {
context
.buildConstraintViolationWithTemplate("Password should not include symbols [< > ; / . : ' [ ] ( ) , ]")
.addConstraintViolation();
return false;
} else if (PASSWORD_FORBIDEN_CYRILLIC_LETTERS_PATTERN.matcher(value).matches()) {
context
.buildConstraintViolationWithTemplate("Password should not include cyrillic letters")
.addConstraintViolation();
return false;
}else if (!PASSWORD_SPECIAL_SYMBOLS_PATTERN.matcher(value).matches()) {
context
.buildConstraintViolationWithTemplate("Password must include at least 1 special symbol: [! @ # $ % ^ & * _ - + = ~ ?]")
.addConstraintViolation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,24 @@ public void shouldFailValidator_whenUserInputPasswordWithSpace() {
@Test
public void shouldFailValidator_whenUserInputPasswordWithForbiddenSymbols() {
// Given
var password = "<Password!";
var password1 = "<Password!";
var password2 = "hjj;^33N";

// When
var isValid1 = passwordValidator.isValid(password1, context);
var isValid2 = passwordValidator.isValid(password2, context);

// Then
assertFalse(isValid1);
assertFalse(isValid2);
}

@DisplayName(
"PasswordValidator should fail validator when user input password with cyrillic letters")
@Test
public void shouldFailValidator_whenUserInputPasswordWithCyrillicLetters() {
// Given
var password = "Ася_77";

// When
var isValid = passwordValidator.isValid(password, context);
Expand Down

0 comments on commit fad05b3

Please sign in to comment.