Skip to content

Commit 02ef78a

Browse files
committed
Test: 테스트 작성
1 parent 4388712 commit 02ef78a

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,4 +812,73 @@ void recoverUsername_missingField() throws Exception {
812812
.andExpect(jsonPath("$.code").value("COMMON_400"))
813813
.andExpect(jsonPath("$.message").value("잘못된 요청입니다."));
814814
}
815+
816+
// ======================== 비밀번호 재설정 요청 컨트롤러 테스트 ========================
817+
818+
@Test
819+
@DisplayName("정상 비밀번호 재설정 요청 → 200 OK")
820+
void recoverPassword_success() throws Exception {
821+
// given: 가입된 사용자 생성
822+
User user = User.createUser("pwuser", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
823+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
824+
userRepository.save(user);
825+
826+
String body = """
827+
{
828+
"email": "[email protected]"
829+
}
830+
""";
831+
832+
// when & then
833+
mvc.perform(post("/api/auth/password/recover")
834+
.contentType(MediaType.APPLICATION_JSON)
835+
.content(body))
836+
.andDo(print())
837+
.andExpect(status().isOk())
838+
.andExpect(jsonPath("$.success").value(true))
839+
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
840+
.andExpect(jsonPath("$.message").value("비밀번호 재설정 링크를 이메일로 전송했습니다."))
841+
.andExpect(jsonPath("$.data").isEmpty());
842+
}
843+
844+
@Test
845+
@DisplayName("비밀번호 재설정 요청 실패 - 존재하지 않는 사용자 → 404 Not Found")
846+
void recoverPassword_userNotFound() throws Exception {
847+
// given: 존재하지 않는 이메일 사용
848+
String body = """
849+
{
850+
"email": "[email protected]"
851+
}
852+
""";
853+
854+
// when & then
855+
mvc.perform(post("/api/auth/password/recover")
856+
.contentType(MediaType.APPLICATION_JSON)
857+
.content(body))
858+
.andDo(print())
859+
.andExpect(status().isNotFound())
860+
.andExpect(jsonPath("$.success").value(false))
861+
.andExpect(jsonPath("$.code").value("USER_001"))
862+
.andExpect(jsonPath("$.message").value("존재하지 않는 사용자입니다."));
863+
}
864+
865+
@Test
866+
@DisplayName("비밀번호 재설정 요청 실패 - 이메일 필드 누락 → 400 Bad Request")
867+
void recoverPassword_missingField() throws Exception {
868+
// given: 잘못된 요청 (이메일 필드 없음)
869+
String body = """
870+
{
871+
}
872+
""";
873+
874+
// when & then
875+
mvc.perform(post("/api/auth/password/recover")
876+
.contentType(MediaType.APPLICATION_JSON)
877+
.content(body))
878+
.andDo(print())
879+
.andExpect(status().isBadRequest())
880+
.andExpect(jsonPath("$.success").value(false))
881+
.andExpect(jsonPath("$.code").value("COMMON_400"))
882+
.andExpect(jsonPath("$.message").value("잘못된 요청입니다."));
883+
}
815884
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,31 @@ void recoverUsername_maskingRules() {
593593
verify(emailService).sendUsernameEmail(eq("[email protected]"), eq("a*c"));
594594
verify(emailService).sendUsernameEmail(eq("[email protected]"), eq("ab**ef"));
595595
}
596+
597+
// ======================== 비밀번호 재설정 요청 테스트 ========================
598+
599+
@Test
600+
@DisplayName("정상 비밀번호 재설정 요청 → 이메일 발송 성공")
601+
void recoverPassword_success() {
602+
// given: 가입된 사용자 생성
603+
User user = User.createUser("pwuser", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
604+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
605+
userRepository.save(user);
606+
607+
// when
608+
authService.recoverPassword("[email protected]");
609+
610+
// then: 이메일 발송 호출 확인
611+
verify(emailService, times(1))
612+
.sendPasswordResetEmail(eq("[email protected]"), anyString());
613+
}
614+
615+
@Test
616+
@DisplayName("비밀번호 재설정 요청 실패 - 존재하지 않는 사용자 → USER_NOT_FOUND")
617+
void recoverPassword_userNotFound() {
618+
// when & then
619+
assertThatThrownBy(() -> authService.recoverPassword("[email protected]"))
620+
.isInstanceOf(CustomException.class)
621+
.hasMessage(ErrorCode.USER_NOT_FOUND.getMessage());
622+
}
596623
}

0 commit comments

Comments
 (0)