Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import java.util.regex.Pattern;

public class PasswordValidator implements ConstraintValidator<Password, String> {
// 8 ~ 20자이고, 반드시 한 개의 알파벳, 한 개의 숫자, !@#$%^ 중 한 개가 모두 들어 갔는지 확인
private static final Pattern PASSWORD_PATTERN = Pattern.compile("^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[!@#$%^]).{8,20}$");
private static final String REGEX = "^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[!@#$%^])(?!.*\\s).{8,20}$";
private static final Pattern PASSWORD_PATTERN = Pattern.compile(REGEX);

@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
public boolean isValid(String s, ConstraintValidatorContext context) {
if (s == null || s.isEmpty()) {
return true;
return false;
}
return PASSWORD_PATTERN.matcher(s).matches();
}

}

40 changes: 40 additions & 0 deletions backend/src/test/java/moadong/unit/user/PasswordValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package moadong.unit.user;

import moadong.fixture.UserFixture;
import moadong.global.validator.PasswordValidator;
import moadong.util.annotations.UnitTest;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@UnitTest
public class PasswordValidatorTest {
private final PasswordValidator passwordValidator = new PasswordValidator();

@Test
void 유효한_비밀번호는_유효하다() {
boolean isValid = passwordValidator.isValid(UserFixture.collectPassword, null);
Assertions.assertThat(isValid).isTrue();
}

@ParameterizedTest
@ValueSource(strings = {
"short1!", // 7자 (길이 부족)
"longpassword1234567890!", // 21자 (길이 초과)
"abcdefgh", // 영문 소문자만 (숫자 없음)
"ABCDEFGH", // 영문 대문자만 (숫자 없음)
"12345678", // 숫자만 (영문 없음)
"Abcdefgh", // 영문만 (숫자 없음)
"abcd1234*", // 허용되지 않은 특수문자 `*`
"abc def123!", // 공백 포함
"passWord!", // 특수문자 있음, 숫자 없음
"1234!@#$", // 숫자 + 특수문자, 문자 없음
"Abcdef12()", // 괄호 포함 (허용되지 않은 특수문자)
"ABCD1234~", // `~` 특수문자 허용 안됨
})
void 유효하지_않은_비밀번호는_실패한다(String password) {
boolean isValid = passwordValidator.isValid(password, null);
Assertions.assertThat(isValid).isFalse();
}
Comment on lines +21 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

null과 빈 문자열에 대한 테스트 케이스를 추가하세요.

현재 테스트는 다양한 무효한 문자열 패턴을 다루고 있지만, 변경된 null/empty 처리 로직에 대한 명시적인 테스트가 누락되어 있습니다.

다음 테스트 케이스들을 추가하는 것을 권장합니다:

@ParameterizedTest
@ValueSource(strings = {
        "short1!",              // 7자 (길이 부족)
        "longpassword1234567890!", // 21자 (길이 초과)
        // ... 기존 케이스들
})
void 유효하지_않은_비밀번호는_실패한다(String password) {
    boolean isValid = passwordValidator.isValid(password, null);
    Assertions.assertThat(isValid).isFalse();
}

+@Test
+void null_또는_빈_비밀번호는_유효하지_않다() {
+    Assertions.assertThat(passwordValidator.isValid(null, null)).isFalse();
+    Assertions.assertThat(passwordValidator.isValid("", null)).isFalse();
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@ParameterizedTest
@ValueSource(strings = {
"short1!", // 7자 (길이 부족)
"longpassword1234567890!", // 21자 (길이 초과)
"abcdefgh", // 영문 소문자만 (숫자 없음)
"ABCDEFGH", // 영문 대문자만 (숫자 없음)
"12345678", // 숫자만 (영문 없음)
"Abcdefgh", // 영문만 (숫자 없음)
"abcd1234*", // 허용되지 않은 특수문자 `*`
"abc def123!", // 공백 포함
"passWord!", // 특수문자 있음, 숫자 없음
"1234!@#$", // 숫자 + 특수문자, 문자 없음
"Abcdef12()", // 괄호 포함 (허용되지 않은 특수문자)
"ABCD1234~", // `~` 특수문자 허용 안됨
})
void 유효하지_않은_비밀번호는_실패한다(String password) {
boolean isValid = passwordValidator.isValid(password, null);
Assertions.assertThat(isValid).isFalse();
}
@ParameterizedTest
@ValueSource(strings = {
"short1!", // 7자 (길이 부족)
"longpassword1234567890!", // 21자 (길이 초과)
"abcdefgh", // 영문 소문자만 (숫자 없음)
"ABCDEFGH", // 영문 대문자만 (숫자 없음)
"12345678", // 숫자만 (영문 없음)
"Abcdefgh", // 영문만 (숫자 없음)
"abcd1234*", // 허용되지 않은 특수문자 `*`
"abc def123!", // 공백 포함
"passWord!", // 특수문자 있음, 숫자 없음
"1234!@#$", // 숫자 + 특수문자, 문자 없음
"Abcdef12()", // 괄호 포함 (허용되지 않은 특수문자)
"ABCD1234~", // `~` 특수문자 허용 안됨
})
void 유효하지_않은_비밀번호는_실패한다(String password) {
boolean isValid = passwordValidator.isValid(password, null);
Assertions.assertThat(isValid).isFalse();
}
@Test
void null_또는_빈_비밀번호는_유효하지_않다() {
Assertions.assertThat(passwordValidator.isValid(null, null)).isFalse();
Assertions.assertThat(passwordValidator.isValid("", null)).isFalse();
}
🤖 Prompt for AI Agents
In backend/src/test/java/moadong/unit/user/PasswordValidatorTest.java around
lines 21 to 39, the test cases lack coverage for null and empty string inputs,
which are important for validating the updated null/empty handling logic. Add
parameterized test cases for null and empty strings to the existing invalid
password test method or create a separate test method to explicitly assert that
these inputs are considered invalid by the passwordValidator.isValid method.

}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ public static void setUp() {
"1234!@#$", // 숫자 + 특수문자, 문자 없음
"Abcdef12()", // 괄호 포함 (허용되지 않은 특수문자)
"ABCD1234~", // `~` 특수문자 허용 안됨
"abc1234^", // `^`는 허용되지만 문자 형식에 따라 위험성 있음
})
void 회원가입시_유저_비밀번호가_조건에_맞지_않으면_실패한다(String password) {
String userId = UserFixture.collectUserId;
Expand Down
Loading