Skip to content

Commit 420fb32

Browse files
committed
test[member]: 이메일 전송 및 비밀번호 재설정 기능 테스트 코드 추가
1 parent f5ba176 commit 420fb32

File tree

2 files changed

+444
-6
lines changed

2 files changed

+444
-6
lines changed

backend/src/test/java/com/ai/lawyer/domain/member/controller/MemberControllerTest.java

Lines changed: 202 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.ai.lawyer.domain.member.controller;
22

3-
import com.ai.lawyer.domain.member.dto.MemberLoginRequest;
4-
import com.ai.lawyer.domain.member.dto.MemberResponse;
5-
import com.ai.lawyer.domain.member.dto.MemberSignupRequest;
3+
import com.ai.lawyer.domain.member.dto.*;
64
import com.ai.lawyer.domain.member.entity.Member;
75
import com.ai.lawyer.domain.member.service.MemberService;
86
import com.ai.lawyer.domain.member.exception.MemberAuthenticationException;
@@ -385,4 +383,205 @@ void getMyInfo_Fail_MemberNotFound() {
385383

386384
verify(memberService).getMemberById(1L);
387385
}
386+
387+
// ===== 이메일 인증 관련 테스트 =====
388+
389+
@Test
390+
@DisplayName("이메일 전송 성공 - 비로그인 사용자 (요청 바디에서 loginId 추출)")
391+
void sendEmail_Success_NonLoggedInUser() throws Exception {
392+
// given
393+
MemberEmailRequestDto requestDto = new MemberEmailRequestDto();
394+
requestDto.setLoginId("[email protected]");
395+
doNothing().when(memberService).sendCodeToEmailByLoginId("[email protected]");
396+
397+
// when and then
398+
mockMvc.perform(post("/api/auth/sendEmail")
399+
.with(csrf())
400+
.contentType(MediaType.APPLICATION_JSON)
401+
.content(objectMapper.writeValueAsString(requestDto)))
402+
.andDo(print())
403+
.andExpect(status().isOk())
404+
.andExpect(jsonPath("$.message").value("이메일 전송 성공"))
405+
.andExpect(jsonPath("$.email").value("[email protected]"))
406+
.andExpect(jsonPath("$.success").value(true));
407+
408+
verify(memberService).sendCodeToEmailByLoginId("[email protected]");
409+
}
410+
411+
@Test
412+
@DisplayName("이메일 전송 실패 - 존재하지 않는 회원")
413+
void sendEmail_Fail_MemberNotFound() throws Exception {
414+
// given
415+
MemberEmailRequestDto requestDto = new MemberEmailRequestDto();
416+
requestDto.setLoginId("[email protected]");
417+
doThrow(new IllegalArgumentException("해당 로그인 ID의 회원이 없습니다."))
418+
.when(memberService).sendCodeToEmailByLoginId("[email protected]");
419+
420+
// when and then
421+
mockMvc.perform(post("/api/auth/sendEmail")
422+
.with(csrf())
423+
.contentType(MediaType.APPLICATION_JSON)
424+
.content(objectMapper.writeValueAsString(requestDto)))
425+
.andDo(print())
426+
.andExpect(status().isBadRequest());
427+
428+
verify(memberService).sendCodeToEmailByLoginId("[email protected]");
429+
}
430+
431+
@Test
432+
@DisplayName("이메일 인증번호 검증 성공 - 비로그인 사용자")
433+
void verifyEmail_Success_NonLoggedInUser() throws Exception {
434+
// given
435+
given(memberService.verifyAuthCode("[email protected]", "123456")).willReturn(true);
436+
437+
EmailVerifyCodeRequestDto requestDto = new EmailVerifyCodeRequestDto();
438+
requestDto.setLoginId("[email protected]");
439+
requestDto.setVerificationCode("123456");
440+
441+
// when and then
442+
mockMvc.perform(post("/api/auth/verifyEmail")
443+
.with(csrf())
444+
.contentType(MediaType.APPLICATION_JSON)
445+
.content(objectMapper.writeValueAsString(requestDto)))
446+
.andDo(print())
447+
.andExpect(status().isOk())
448+
.andExpect(jsonPath("$.message").value("인증번호 검증 성공"))
449+
.andExpect(jsonPath("$.email").value("[email protected]"))
450+
.andExpect(jsonPath("$.success").value(true));
451+
452+
verify(memberService).verifyAuthCode("[email protected]", "123456");
453+
}
454+
455+
@Test
456+
@DisplayName("이메일 인증번호 검증 실패 - 잘못된 인증번호")
457+
void verifyEmail_Fail_InvalidCode() throws Exception {
458+
// given
459+
given(memberService.verifyAuthCode("[email protected]", "999999")).willReturn(false);
460+
461+
EmailVerifyCodeRequestDto requestDto = new EmailVerifyCodeRequestDto();
462+
requestDto.setLoginId("[email protected]");
463+
requestDto.setVerificationCode("999999");
464+
465+
// when and then
466+
mockMvc.perform(post("/api/auth/verifyEmail")
467+
.with(csrf())
468+
.contentType(MediaType.APPLICATION_JSON)
469+
.content(objectMapper.writeValueAsString(requestDto)))
470+
.andDo(print())
471+
.andExpect(status().isBadRequest());
472+
473+
verify(memberService).verifyAuthCode("[email protected]", "999999");
474+
}
475+
476+
@Test
477+
@DisplayName("이메일 인증번호 검증 실패 - 유효성 검증 실패")
478+
void verifyEmail_Fail_ValidationError() throws Exception {
479+
// given
480+
EmailVerifyCodeRequestDto invalidRequest = new EmailVerifyCodeRequestDto();
481+
invalidRequest.setLoginId("[email protected]");
482+
invalidRequest.setVerificationCode("12345"); // 6자리가 아님
483+
484+
// when and then
485+
mockMvc.perform(post("/api/auth/verifyEmail")
486+
.with(csrf())
487+
.contentType(MediaType.APPLICATION_JSON)
488+
.content(objectMapper.writeValueAsString(invalidRequest)))
489+
.andDo(print())
490+
.andExpect(status().isBadRequest());
491+
492+
verify(memberService, never()).verifyAuthCode(anyString(), anyString());
493+
}
494+
495+
// ===== 비밀번호 재설정 관련 테스트 =====
496+
497+
@Test
498+
@DisplayName("비밀번호 재설정 성공")
499+
void resetPassword_Success() throws Exception {
500+
// given
501+
ResetPasswordRequestDto requestDto = new ResetPasswordRequestDto();
502+
requestDto.setLoginId("[email protected]");
503+
requestDto.setNewPassword("newPassword123");
504+
requestDto.setSuccess(true);
505+
506+
doNothing().when(memberService).resetPassword("[email protected]", "newPassword123", true);
507+
508+
// when and then
509+
mockMvc.perform(post("/api/auth/password-reset/reset")
510+
.with(csrf())
511+
.contentType(MediaType.APPLICATION_JSON)
512+
.content(objectMapper.writeValueAsString(requestDto)))
513+
.andDo(print())
514+
.andExpect(status().isOk())
515+
.andExpect(jsonPath("$.message").value("비밀번호가 성공적으로 재설정되었습니다."))
516+
.andExpect(jsonPath("$.email").value("[email protected]"))
517+
.andExpect(jsonPath("$.success").value(true));
518+
519+
verify(memberService).resetPassword("[email protected]", "newPassword123", true);
520+
}
521+
522+
@Test
523+
@DisplayName("비밀번호 재설정 실패 - 인증되지 않음 (success = false)")
524+
void resetPassword_Fail_NotAuthenticated() throws Exception {
525+
// given
526+
ResetPasswordRequestDto requestDto = new ResetPasswordRequestDto();
527+
requestDto.setLoginId("[email protected]");
528+
requestDto.setNewPassword("newPassword123");
529+
requestDto.setSuccess(false);
530+
531+
doThrow(new IllegalArgumentException("이메일 인증을 완료해야 비밀번호를 재설정할 수 있습니다."))
532+
.when(memberService).resetPassword("[email protected]", "newPassword123", false);
533+
534+
// when and then
535+
mockMvc.perform(post("/api/auth/password-reset/reset")
536+
.with(csrf())
537+
.contentType(MediaType.APPLICATION_JSON)
538+
.content(objectMapper.writeValueAsString(requestDto)))
539+
.andDo(print())
540+
.andExpect(status().isBadRequest());
541+
542+
verify(memberService).resetPassword("[email protected]", "newPassword123", false);
543+
}
544+
545+
@Test
546+
@DisplayName("비밀번호 재설정 실패 - 존재하지 않는 회원")
547+
void resetPassword_Fail_MemberNotFound() throws Exception {
548+
// given
549+
ResetPasswordRequestDto requestDto = new ResetPasswordRequestDto();
550+
requestDto.setLoginId("[email protected]");
551+
requestDto.setNewPassword("newPassword123");
552+
requestDto.setSuccess(true);
553+
554+
doThrow(new IllegalArgumentException("존재하지 않는 회원입니다."))
555+
.when(memberService).resetPassword("[email protected]", "newPassword123", true);
556+
557+
// when and then
558+
mockMvc.perform(post("/api/auth/password-reset/reset")
559+
.with(csrf())
560+
.contentType(MediaType.APPLICATION_JSON)
561+
.content(objectMapper.writeValueAsString(requestDto)))
562+
.andDo(print())
563+
.andExpect(status().isUnauthorized());
564+
565+
verify(memberService).resetPassword("[email protected]", "newPassword123", true);
566+
}
567+
568+
@Test
569+
@DisplayName("비밀번호 재설정 실패 - 유효성 검증 실패")
570+
void resetPassword_Fail_ValidationError() throws Exception {
571+
// given
572+
ResetPasswordRequestDto invalidRequest = new ResetPasswordRequestDto();
573+
invalidRequest.setLoginId(""); // 빈 이메일
574+
invalidRequest.setNewPassword(""); // 빈 비밀번호
575+
invalidRequest.setSuccess(null); // null success
576+
577+
// when and then
578+
mockMvc.perform(post("/api/auth/password-reset/reset")
579+
.with(csrf())
580+
.contentType(MediaType.APPLICATION_JSON)
581+
.content(objectMapper.writeValueAsString(invalidRequest)))
582+
.andDo(print())
583+
.andExpect(status().isBadRequest());
584+
585+
verify(memberService, never()).resetPassword(anyString(), anyString(), any());
586+
}
388587
}

0 commit comments

Comments
 (0)