Skip to content

Commit 7551941

Browse files
committed
Test: 테스트 작성
1 parent db320ee commit 7551941

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

src/test/java/com/back/domain/user/controller/AuthControllerTest.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,4 +881,106 @@ void recoverPassword_missingField() throws Exception {
881881
.andExpect(jsonPath("$.code").value("COMMON_400"))
882882
.andExpect(jsonPath("$.message").value("잘못된 요청입니다."));
883883
}
884+
885+
// ======================== 비밀번호 재설정 컨트롤러 테스트 ========================
886+
887+
@Test
888+
@DisplayName("정상 비밀번호 재설정 성공 → 200 OK")
889+
void resetPassword_success() throws Exception {
890+
// given: 가입된 사용자
891+
User user = User.createUser("resetuser", "reset@example.com", passwordEncoder.encode("OldPass123!"));
892+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
893+
userRepository.save(user);
894+
895+
String token = tokenService.createPasswordResetToken(user.getId());
896+
897+
String body = """
898+
{
899+
"token": "%s",
900+
"newPassword": "NewPass123!"
901+
}
902+
""".formatted(token);
903+
904+
// when & then
905+
mvc.perform(post("/api/auth/password/reset")
906+
.contentType(MediaType.APPLICATION_JSON)
907+
.content(body))
908+
.andDo(print())
909+
.andExpect(status().isOk())
910+
.andExpect(jsonPath("$.success").value(true))
911+
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
912+
.andExpect(jsonPath("$.message").value("비밀번호가 성공적으로 재설정되었습니다."))
913+
.andExpect(jsonPath("$.data").isEmpty());
914+
}
915+
916+
@Test
917+
@DisplayName("비밀번호 재설정 실패 - 유효하지 않은 토큰 → 401 Unauthorized")
918+
void resetPassword_invalidToken() throws Exception {
919+
// given: 가입된 사용자
920+
String body = """
921+
{
922+
"token": "fake-token",
923+
"newPassword": "NewPass123!"
924+
}
925+
""";
926+
927+
// when & then
928+
mvc.perform(post("/api/auth/password/reset")
929+
.contentType(MediaType.APPLICATION_JSON)
930+
.content(body))
931+
.andDo(print())
932+
.andExpect(status().isUnauthorized())
933+
.andExpect(jsonPath("$.success").value(false))
934+
.andExpect(jsonPath("$.code").value("TOKEN_003"))
935+
.andExpect(jsonPath("$.message").value("유효하지 않은 비밀번호 재설정 토큰입니다."));
936+
}
937+
938+
@Test
939+
@DisplayName("비밀번호 재설정 실패 - 비밀번호 정책 위반 → 400 Bad Request")
940+
void resetPassword_invalidPassword() throws Exception {
941+
// given: 가입된 사용자 + 토큰
942+
User user = User.createUser("resetuser2", "reset2@example.com", passwordEncoder.encode("OldPass123!"));
943+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
944+
userRepository.save(user);
945+
946+
String token = tokenService.createPasswordResetToken(user.getId());
947+
948+
String body = """
949+
{
950+
"token": "%s",
951+
"newPassword": "weakpw"
952+
}
953+
""".formatted(token);
954+
955+
// when & then
956+
mvc.perform(post("/api/auth/password/reset")
957+
.contentType(MediaType.APPLICATION_JSON)
958+
.content(body))
959+
.andDo(print())
960+
.andExpect(status().isBadRequest())
961+
.andExpect(jsonPath("$.success").value(false))
962+
.andExpect(jsonPath("$.code").value("USER_005"))
963+
.andExpect(jsonPath("$.message").value("비밀번호는 최소 8자 이상, 숫자/특수문자를 포함해야 합니다."));
964+
}
965+
966+
@Test
967+
@DisplayName("비밀번호 재설정 실패 - 요청 필드 누락 → 400 Bad Request")
968+
void resetPassword_missingField() throws Exception {
969+
// given: 잘못된 요청 (토큰 필드 누락)
970+
String body = """
971+
{
972+
"token": ""
973+
}
974+
""";
975+
976+
// when & then
977+
mvc.perform(post("/api/auth/password/reset")
978+
.contentType(MediaType.APPLICATION_JSON)
979+
.content(body))
980+
.andDo(print())
981+
.andExpect(status().isBadRequest())
982+
.andExpect(jsonPath("$.success").value(false))
983+
.andExpect(jsonPath("$.code").value("COMMON_400"))
984+
.andExpect(jsonPath("$.message").value("잘못된 요청입니다."));
985+
}
884986
}

src/test/java/com/back/domain/user/service/AuthServiceTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,4 +620,65 @@ void recoverPassword_userNotFound() {
620620
.isInstanceOf(CustomException.class)
621621
.hasMessage(ErrorCode.USER_NOT_FOUND.getMessage());
622622
}
623+
624+
// ======================== 비밀번호 재설정 테스트 ========================
625+
626+
@Test
627+
@DisplayName("정상 비밀번호 재설정 성공 → 비밀번호 변경 및 토큰 삭제")
628+
void resetPassword_success() {
629+
// given: 가입된 사용자
630+
User user = User.createUser("resetuser", "reset@example.com", passwordEncoder.encode("OldPass123!"));
631+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
632+
userRepository.save(user);
633+
634+
// 비밀번호 재설정 토큰 생성
635+
String token = tokenService.createPasswordResetToken(user.getId());
636+
637+
// when
638+
authService.resetPassword(token, "NewPass123!");
639+
640+
// then: 비밀번호 변경 확인
641+
User updated = userRepository.findById(user.getId()).orElseThrow();
642+
assertThat(passwordEncoder.matches("NewPass123!", updated.getPassword())).isTrue();
643+
644+
// 토큰이 삭제되었는지 확인
645+
Long result = tokenService.getUserIdByPasswordResetToken(token);
646+
assertThat(result).isNull();
647+
}
648+
649+
@Test
650+
@DisplayName("비밀번호 재설정 실패 - 유효하지 않은 토큰 → INVALID_PASSWORD_RESET_TOKEN")
651+
void resetPassword_invalidToken() {
652+
assertThatThrownBy(() -> authService.resetPassword("fake-token", "NewPass123!"))
653+
.isInstanceOf(CustomException.class)
654+
.hasMessage(ErrorCode.INVALID_PASSWORD_RESET_TOKEN.getMessage());
655+
}
656+
657+
@Test
658+
@DisplayName("비밀번호 재설정 실패 - 존재하지 않는 사용자 → USER_NOT_FOUND")
659+
void resetPassword_userNotFound() {
660+
// given: 토큰은 만들었지만 사용자 ID는 없는 값으로 설정
661+
String token = tokenService.createPasswordResetToken(99999L);
662+
663+
// when & then
664+
assertThatThrownBy(() -> authService.resetPassword(token, "NewPass123!"))
665+
.isInstanceOf(CustomException.class)
666+
.hasMessage(ErrorCode.USER_NOT_FOUND.getMessage());
667+
}
668+
669+
@Test
670+
@DisplayName("비밀번호 재설정 실패 - 비밀번호 정책 위반 → INVALID_PASSWORD")
671+
void resetPassword_invalidPassword() {
672+
// given: 가입된 사용자 + 토큰
673+
User user = User.createUser("resetuser2", "reset2@example.com", passwordEncoder.encode("OldPass123!"));
674+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
675+
userRepository.save(user);
676+
677+
String token = tokenService.createPasswordResetToken(user.getId());
678+
679+
// when & then
680+
assertThatThrownBy(() -> authService.resetPassword(token, "weakpw"))
681+
.isInstanceOf(CustomException.class)
682+
.hasMessage(ErrorCode.INVALID_PASSWORD.getMessage());
683+
}
623684
}

0 commit comments

Comments
 (0)