Skip to content

Commit

Permalink
[BE] 비밀번호 검증 로직 변경 (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehBeak authored Sep 10, 2024
1 parent 97438f7 commit 138e397
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AttendeePassword {

private static final Pattern PASSWORD_PATTERN = Pattern.compile("^[a-zA-Z0-9!@*#$%]+$");
private static final Pattern PASSWORD_PATTERN = Pattern.compile("^\\d{4}+$");

@Column(nullable = false, length = 10)
@Column(nullable = false, length = 4)
private String password;

public AttendeePassword(String password) {
Expand All @@ -27,16 +27,9 @@ public AttendeePassword(String password) {
}

private void validatePassword(String password) {
validatePasswordLength(password);
validatePasswordFormat(password);
}

private void validatePasswordLength(String password) {
if (password.length() > 10) {
throw new MomoException(AttendeeErrorCode.INVALID_PASSWORD_LENGTH);
}
}

private void validatePasswordFormat(String password) {
if (!PASSWORD_PATTERN.matcher(password).matches()) {
throw new MomoException(AttendeeErrorCode.INVALID_PASSWORD_FORMAT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
public enum AttendeeErrorCode implements ErrorCodeType {

INVALID_NAME_LENGTH(HttpStatus.BAD_REQUEST, "이름 길이는 최대 5글자까지 가능합니다."),
INVALID_PASSWORD_LENGTH(HttpStatus.BAD_REQUEST, "비밀번호 길이는 최대 10글자까지 가능합니다."),
INVALID_PASSWORD_FORMAT(HttpStatus.BAD_REQUEST, "비밀번호 형식은 숫자, 문자, 특수문자만 가능합니다."),
INVALID_PASSWORD_FORMAT(HttpStatus.BAD_REQUEST, "비밀번호 형식은 4자리 숫자입니다."),
PASSWORD_MISMATCHED(HttpStatus.BAD_REQUEST, "비밀번호가 일치하지 않습니다."),
INVALID_ATTENDEE(HttpStatus.BAD_REQUEST, "해당 약속에 참여하는 참가자 정보가 없습니다."),
NOT_FOUND_ATTENDEE(HttpStatus.NOT_FOUND, "해당 약속에 참여하는 참가자 정보가 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public record AttendeeLoginRequest(

@NotEmpty
@Schema(description = "참가자 비밀번호", example = "1234")
@Length(max = 10, message = "비밀번호는 10자 이하입니다.")
@Pattern(regexp = "^[a-zA-Z0-9!@*#$%]+$", message = "비밀번호는 알파벳 대소문자와 숫자만 포함해야 합니다.")
@Length(max = 10, message = "비밀번호는 4자리 숫자입니다.")
@Pattern(regexp = "^\\d{4}+$", message = "비밀번호는 4자리 숫자여야 합니다.")
String password
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ void create() {
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
LocalDate dayAfterTomorrow = today.plusDays(2);
AttendeeFixture hostJazz = AttendeeFixture.HOST_JAZZ;
MeetingCreateRequest request = new MeetingCreateRequest(
"host",
"momo",
hostJazz.getName(),
hostJazz.getPassword(),
"momoMeeting",
List.of(tomorrow.toString(), dayAfterTomorrow.toString()),
"08:00",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ScheduleControllerTest {
void setUp() {
RestAssured.port = port;
meeting = meetingRepository.save(MeetingFixture.MOVIE.create());
attendee = attendeeRepository.save(new Attendee(meeting, "name", "password", Role.GUEST));
attendee = attendeeRepository.save(new Attendee(meeting, "name", "1234", Role.GUEST));
today = availableDateRepository.save(new AvailableDate(LocalDate.now(), meeting));
tomorrow = availableDateRepository.save(new AvailableDate(LocalDate.now().plusDays(1), meeting));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@

class AttendeePasswordTest {

@DisplayName("참가자 비밀번호가 20글자를 초과하면 예외를 발생시킨다.")
@DisplayName("참가자 비밀번호가 4글자를 초과하면 예외를 발생시킨다.")
@Test
void throwsExceptionIfAttendeePasswordIsTooLong() {
assertThatThrownBy(() -> new AttendeePassword("invalid_password_length_invalid_password_length"))
.isInstanceOf(MomoException.class)
.hasMessage(AttendeeErrorCode.INVALID_PASSWORD_LENGTH.message());
.hasMessage(AttendeeErrorCode.INVALID_PASSWORD_FORMAT.message());
}

@DisplayName("참가자 비밀번호 객체가 정상 생성된다.")
@Test
void createAttendeePasswordObjectSuccessfully() {
assertThatNoException()
.isThrownBy(() -> new AttendeePassword("momo"));
.isThrownBy(() -> new AttendeePassword("1234"));
}

@DisplayName("비밀번호가 서로 다르면 예외를 발생시킨다.")
@Test
void throwsExceptionForMismatchedPasswords() {
AttendeePassword password = new AttendeePassword("1234");
AttendeePassword other = new AttendeePassword("123456");
AttendeePassword other = new AttendeePassword("4321");

assertThatThrownBy(() -> password.verifyPassword(other))
.isInstanceOf(MomoException.class)
Expand Down
18 changes: 13 additions & 5 deletions backend/src/test/java/kr/momo/fixture/AttendeeFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

public enum AttendeeFixture {

HOST_JAZZ("jazz", "hostPw!12", Role.HOST),
GUEST_DAON("daon", "daonPw!12", Role.GUEST),
GUEST_BAKEY("bakey", "bakeyPw!12", Role.GUEST),
GUEST_PEDRO("pedro", "pedroPw!12", Role.GUEST),
GUEST_MARK("mark", "mark!12", Role.GUEST);
HOST_JAZZ("jazz", "1234", Role.HOST),
GUEST_DAON("daon", "4321", Role.GUEST),
GUEST_BAKEY("bakey", "3422", Role.GUEST),
GUEST_PEDRO("pedro", "4353", Role.GUEST),
GUEST_MARK("mark", "1234", Role.GUEST);

private final String name;
private final String password;
Expand All @@ -27,4 +27,12 @@ public enum AttendeeFixture {
public Attendee create(Meeting meeting) {
return new Attendee(meeting, new AttendeeName(name), new AttendeePassword(password), role);
}

public String getName() {
return name;
}

public String getPassword() {
return password;
}
}

0 comments on commit 138e397

Please sign in to comment.