Skip to content

Commit 1a60660

Browse files
committed
Test: 인증 메일 재발송 테스트 작성
1 parent d78f3e6 commit 1a60660

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

src/main/java/com/back/domain/user/service/AuthService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public void resendVerificationEmail(String email) {
140140
throw new CustomException(ErrorCode.ALREADY_VERIFIED);
141141
}
142142

143+
// TODO: 기존 토큰이 남아있는 경우 삭제하는 로직 추가 고려
144+
143145
// 새로운 이메일 인증 토큰 생성
144146
String emailToken = tokenService.createEmailVerificationToken(user.getId());
145147

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,100 @@ void verifyEmail_missingToken() throws Exception {
295295
.andExpect(jsonPath("$.message").value("잘못된 요청입니다."));
296296
}
297297

298+
// ======================== 인증 메일 재발송 테스트 ========================
299+
300+
@Test
301+
@DisplayName("인증 메일 재발송 성공 → 200 OK")
302+
void resendVerificationEmail_success() throws Exception {
303+
// given: PENDING 상태 유저 생성
304+
User pending = User.createUser("resenduser", "[email protected]",
305+
passwordEncoder.encode("P@ssw0rd!"));
306+
pending.setUserProfile(new UserProfile(pending, "재발송닉", null, null, null, 0));
307+
pending.setUserStatus(UserStatus.PENDING);
308+
userRepository.save(pending);
309+
310+
String body = """
311+
{
312+
"email": "[email protected]"
313+
}
314+
""";
315+
316+
// when & then
317+
mvc.perform(post("/api/auth/email-verification/resend")
318+
.contentType(MediaType.APPLICATION_JSON)
319+
.content(body))
320+
.andDo(print())
321+
.andExpect(status().isOk())
322+
.andExpect(jsonPath("$.success").value(true))
323+
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
324+
.andExpect(jsonPath("$.message").value("인증 메일이 재발송되었습니다."))
325+
.andExpect(jsonPath("$.data").isEmpty());
326+
}
327+
328+
@Test
329+
@DisplayName("인증 메일 재발송 실패 - 존재하지 않는 사용자 → 404 Not Found")
330+
void resendVerificationEmail_userNotFound() throws Exception {
331+
String body = """
332+
{
333+
"email": "[email protected]"
334+
}
335+
""";
336+
337+
mvc.perform(post("/api/auth/email-verification/resend")
338+
.contentType(MediaType.APPLICATION_JSON)
339+
.content(body))
340+
.andDo(print())
341+
.andExpect(status().isNotFound())
342+
.andExpect(jsonPath("$.success").value(false))
343+
.andExpect(jsonPath("$.code").value("USER_001"))
344+
.andExpect(jsonPath("$.message").value("존재하지 않는 사용자입니다."));
345+
}
346+
347+
@Test
348+
@DisplayName("인증 메일 재발송 실패 - 이미 인증된 계정 → 409 Conflict")
349+
void resendVerificationEmail_alreadyVerified() throws Exception {
350+
// given: ACTIVE 상태 유저 생성
351+
User active = User.createUser("activeuser2", "[email protected]",
352+
passwordEncoder.encode("P@ssw0rd!"));
353+
active.setUserProfile(new UserProfile(active, "액티브닉", null, null, null, 0));
354+
active.setUserStatus(UserStatus.ACTIVE);
355+
userRepository.save(active);
356+
357+
String body = """
358+
{
359+
"email": "[email protected]"
360+
}
361+
""";
362+
363+
mvc.perform(post("/api/auth/email-verification/resend")
364+
.contentType(MediaType.APPLICATION_JSON)
365+
.content(body))
366+
.andDo(print())
367+
.andExpect(status().isConflict())
368+
.andExpect(jsonPath("$.success").value(false))
369+
.andExpect(jsonPath("$.code").value("TOKEN_002"))
370+
.andExpect(jsonPath("$.message").value("이미 인증된 계정입니다."));
371+
}
372+
373+
@Test
374+
@DisplayName("인증 메일 재발송 실패 - 이메일 필드 누락 → 400 Bad Request")
375+
void resendVerificationEmail_missingField() throws Exception {
376+
// given: 잘못된 요청 (이메일 누락)
377+
String body = """
378+
{
379+
}
380+
""";
381+
382+
mvc.perform(post("/api/auth/email-verification/resend")
383+
.contentType(MediaType.APPLICATION_JSON)
384+
.content(body))
385+
.andDo(print())
386+
.andExpect(status().isBadRequest())
387+
.andExpect(jsonPath("$.success").value(false))
388+
.andExpect(jsonPath("$.code").value("COMMON_400"))
389+
.andExpect(jsonPath("$.message").value("잘못된 요청입니다."));
390+
}
391+
298392
// ======================== 로그인 테스트 ========================
299393

300394
@Test

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,55 @@ void verifyEmail_alreadyVerified() {
275275
.hasMessage(ErrorCode.ALREADY_VERIFIED.getMessage());
276276
}
277277

278+
// ======================== 인증 메일 재발송 테스트 ========================
279+
280+
@Test
281+
@DisplayName("정상 인증 메일 재발송 성공 → PENDING 상태 유저")
282+
void resendVerificationEmail_success() {
283+
// given: 회원가입 후 기본 상태는 PENDING
284+
UserRegisterRequest request = new UserRegisterRequest(
285+
"resenduser", "[email protected]", "P@ssw0rd!", "닉네임"
286+
);
287+
UserResponse response = authService.register(request);
288+
User user = userRepository.findById(response.userId()).orElseThrow();
289+
290+
// when: 인증 메일 재발송 요청
291+
authService.resendVerificationEmail(user.getEmail());
292+
293+
// then: 이메일 발송이 호출되었는지 확인
294+
verify(emailService, times(2))
295+
.sendVerificationEmail(eq("[email protected]"), anyString());
296+
// (회원가입 시 1회 + 재발송 시 1회 → 총 2회)
297+
}
298+
299+
@Test
300+
@DisplayName("인증 메일 재발송 실패 - 존재하지 않는 사용자 → USER_NOT_FOUND")
301+
void resendVerificationEmail_userNotFound() {
302+
// when & then
303+
assertThatThrownBy(() ->
304+
authService.resendVerificationEmail("[email protected]")
305+
).isInstanceOf(CustomException.class)
306+
.hasMessage(ErrorCode.USER_NOT_FOUND.getMessage());
307+
}
308+
309+
@Test
310+
@DisplayName("인증 메일 재발송 실패 - 이미 ACTIVE 상태 → ALREADY_VERIFIED")
311+
void resendVerificationEmail_alreadyVerified() {
312+
// given: 회원가입 후 사용자 상태를 ACTIVE로 변경
313+
UserRegisterRequest request = new UserRegisterRequest(
314+
"activeuser", "[email protected]", "P@ssw0rd!", "닉네임"
315+
);
316+
UserResponse response = authService.register(request);
317+
User user = userRepository.findById(response.userId()).orElseThrow();
318+
user.setUserStatus(UserStatus.ACTIVE);
319+
320+
// when & then: 재발송 시도 시 ALREADY_VERIFIED 예외 발생
321+
assertThatThrownBy(() ->
322+
authService.resendVerificationEmail(user.getEmail())
323+
).isInstanceOf(CustomException.class)
324+
.hasMessage(ErrorCode.ALREADY_VERIFIED.getMessage());
325+
}
326+
278327
// ======================== 로그인 테스트 ========================
279328

280329
@Test

0 commit comments

Comments
 (0)