-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: [FN-67] 내 비밀번호 재설정 #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f960914
Feat: 내 비밀번호 재설정 기능
dungbik d877eb0
Test: 내 비밀번호 재설정 단위 테스트 작성
dungbik afe0257
Feat: 회원 탈퇴시 모든 인증이 풀리도록 수정
dungbik 069f203
Refactor: 이메일 인증 여부 검증 코드를 별도의 서비스로 분리
dungbik c5bced9
Refactor: UNVERIFIED_EMAIL 에러 코드 Auth로 이동
dungbik ef91f12
Test: 회원 단위 테스트 코드 실패하던 케이스 수정
dungbik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/project/flipnote/auth/service/EmailVerificationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package project.flipnote.auth.service; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import project.flipnote.auth.exception.AuthErrorCode; | ||
| import project.flipnote.auth.repository.EmailVerificationRedisRepository; | ||
| import project.flipnote.common.exception.BizException; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class EmailVerificationService { | ||
|
|
||
| private final EmailVerificationRedisRepository emailVerificationRedisRepository; | ||
|
|
||
| public void validateVerified(String email) { | ||
| if (!emailVerificationRedisRepository.isVerified(email)) { | ||
| throw new BizException(AuthErrorCode.UNVERIFIED_EMAIL); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/project/flipnote/user/model/ChangePasswordRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package project.flipnote.user.model; | ||
|
|
||
| import project.flipnote.common.validation.annotation.ValidPassword; | ||
|
|
||
| public record ChangePasswordRequest( | ||
|
|
||
| @ValidPassword | ||
| String currentPassword, | ||
|
|
||
| @ValidPassword | ||
| String newPassword | ||
| ) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,16 +15,21 @@ | |
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
|
||
| import project.flipnote.auth.exception.AuthErrorCode; | ||
| import project.flipnote.auth.repository.EmailVerificationRedisRepository; | ||
| import project.flipnote.auth.repository.TokenVersionRedisRepository; | ||
| import project.flipnote.auth.service.AuthService; | ||
| import project.flipnote.auth.service.EmailVerificationService; | ||
| import project.flipnote.auth.service.TokenVersionService; | ||
| import project.flipnote.common.exception.BizException; | ||
| import project.flipnote.fixture.UserFixture; | ||
| import project.flipnote.user.entity.User; | ||
| import project.flipnote.user.entity.UserStatus; | ||
| import project.flipnote.user.exception.UserErrorCode; | ||
| import project.flipnote.user.model.UserRegisterRequest; | ||
| import project.flipnote.user.model.UserInfoResponse; | ||
| import project.flipnote.user.model.MyInfoResponse; | ||
| import project.flipnote.user.model.ChangePasswordRequest; | ||
| import project.flipnote.user.model.UserInfoResponse; | ||
| import project.flipnote.user.model.UserRegisterRequest; | ||
| import project.flipnote.user.model.UserRegisterResponse; | ||
| import project.flipnote.user.model.UserUpdateRequest; | ||
| import project.flipnote.user.model.UserUpdateResponse; | ||
|
|
@@ -49,6 +54,15 @@ class UserServiceTest { | |
| @Mock | ||
| EmailVerificationRedisRepository emailVerificationRedisRepository; | ||
|
|
||
| @Mock | ||
| AuthService authService; | ||
|
|
||
| @Mock | ||
| TokenVersionService tokenVersionService; | ||
|
|
||
| @Mock | ||
| EmailVerificationService emailVerificationService; | ||
|
|
||
| @DisplayName("회원가입 테스트") | ||
| @Nested | ||
| class Register { | ||
|
|
@@ -63,11 +77,9 @@ void success() { | |
|
|
||
| given(userRepository.existsByEmail(any(String.class))).willReturn(false); | ||
| given(userRepository.existsByPhone(any(String.class))).willReturn(false); | ||
| given(emailVerificationRedisRepository.isVerified(anyString())).willReturn(true); | ||
| given(passwordEncoder.encode(any(String.class))).willReturn("encodedPass"); | ||
| given(userRepository.save(any(User.class))).willReturn(user); | ||
|
|
||
|
|
||
| UserRegisterResponse res = userService.register(req); | ||
|
|
||
| assertThat(res.userId()).isEqualTo(user.getId()); | ||
|
|
@@ -82,7 +94,6 @@ void success_ifPhoneIsNull() { | |
| ); | ||
|
|
||
| given(userRepository.existsByEmail(any(String.class))).willReturn(false); | ||
| given(emailVerificationRedisRepository.isVerified(anyString())).willReturn(true); | ||
| given(passwordEncoder.encode(any(String.class))).willReturn("encodedPass"); | ||
| given(userRepository.save(any(User.class))).willReturn(user); | ||
|
|
||
|
|
@@ -132,10 +143,12 @@ void fail_unverifiedEmail() { | |
|
|
||
| given(userRepository.existsByEmail(anyString())).willReturn(false); | ||
| given(userRepository.existsByPhone(anyString())).willReturn(false); | ||
| given(emailVerificationRedisRepository.isVerified(anyString())).willReturn(false); | ||
| doThrow(new BizException(AuthErrorCode.UNVERIFIED_EMAIL)) | ||
| .when(emailVerificationService) | ||
| .validateVerified(anyString()); | ||
|
|
||
| BizException exception = assertThrows(BizException.class, () -> userService.register(req)); | ||
| assertThat(exception.getErrorCode()).isEqualTo(UserErrorCode.UNVERIFIED_EMAIL); | ||
| assertThat(exception.getErrorCode()).isEqualTo(AuthErrorCode.UNVERIFIED_EMAIL); | ||
|
|
||
| verify(userRepository, never()).save(any(User.class)); | ||
| } | ||
|
|
@@ -154,11 +167,8 @@ void success() { | |
|
|
||
| userService.unregister(user.getId()); | ||
|
|
||
| assertThat(user.getStatus()).isEqualTo(UserStatus.INACTIVE); | ||
| assertThat(user.getDeletedAt()).isNotNull(); | ||
|
|
||
| verify(user, times(1)).softDelete(); | ||
| verify(tokenVersionRedisRepository, times(1)).deleteTokenVersion(anyLong()); | ||
| verify(user, times(1)).unregister(); | ||
| verify(tokenVersionService, times(1)).incrementTokenVersion(user.getId()); | ||
| } | ||
|
|
||
| @DisplayName("회원 id가 존재하지 않는 경우 예외 발생") | ||
|
|
@@ -317,4 +327,63 @@ void fail_userNotFound() { | |
| assertThat(exception.getErrorCode()).isEqualTo(UserErrorCode.USER_NOT_FOUND); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @DisplayName("비밀번호 변경 테스트") | ||
| @Nested | ||
| class ChangePassword { | ||
|
|
||
| @DisplayName("성공") | ||
| @Test | ||
| void success() { | ||
| User user = spy(UserFixture.createActiveUser()); | ||
| ChangePasswordRequest req = new ChangePasswordRequest("currentPassword123!", "newPassword123!"); | ||
| String encodedNewPassword = "encodedNewPassword"; | ||
|
|
||
| given(userRepository.findByIdAndStatus(user.getId(), UserStatus.ACTIVE)).willReturn(Optional.of(user)); | ||
| given(passwordEncoder.encode(req.newPassword())).willReturn(encodedNewPassword); | ||
|
|
||
| userService.changePassword(user.getId(), req); | ||
|
|
||
| verify(user, times(1)).changePassword(encodedNewPassword); | ||
| verify(tokenVersionService, times(1)).incrementTokenVersion(user.getId()); | ||
| } | ||
|
|
||
| @DisplayName("존재하지 않는 회원의 비밀번호 변경 시 예외 발생") | ||
| @Test | ||
| void fail_userNotFound() { | ||
| ChangePasswordRequest req = new ChangePasswordRequest("currentPassword123!", "newPassword123!"); | ||
| Long nonExistentUserId = 99L; | ||
|
|
||
| given(userRepository.findByIdAndStatus(nonExistentUserId, UserStatus.ACTIVE)).willReturn(Optional.empty()); | ||
|
|
||
| BizException exception = assertThrows(BizException.class, | ||
| () -> userService.changePassword(nonExistentUserId, req)); | ||
|
|
||
| assertThat(exception.getErrorCode()).isEqualTo(UserErrorCode.USER_NOT_FOUND); | ||
|
|
||
| verify(passwordEncoder, never()).matches(anyString(), anyString()); | ||
| verify(passwordEncoder, never()).encode(anyString()); | ||
| verify(tokenVersionRedisRepository, never()).deleteTokenVersion(anyLong()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 토큰 버전 서비스 사용으로 업데이트가 필요합니다. 테스트에서 다음과 같이 수정하세요: - verify(tokenVersionRedisRepository, never()).deleteTokenVersion(anyLong());
+ verify(tokenVersionService, never()).incrementTokenVersion(anyLong());Also applies to: 386-386 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| @DisplayName("현재 비밀번호가 일치하지 않을 경우 예외 발생") | ||
| @Test | ||
| void fail_incorrectCurrentPassword() { | ||
| User user = UserFixture.createActiveUser(); | ||
| ChangePasswordRequest req = new ChangePasswordRequest("wrongPassword", "newPassword123!"); | ||
|
|
||
| given(userRepository.findByIdAndStatus(user.getId(), UserStatus.ACTIVE)).willReturn(Optional.of(user)); | ||
| doThrow(new BizException(AuthErrorCode.INVALID_CREDENTIALS)) | ||
| .when(authService) | ||
| .validatePasswordMatch(req.currentPassword(), user.getPassword()); | ||
|
|
||
| BizException exception = assertThrows(BizException.class, | ||
| () -> userService.changePassword(user.getId(), req)); | ||
|
|
||
| assertThat(exception.getErrorCode()).isEqualTo(AuthErrorCode.INVALID_CREDENTIALS); | ||
| verify(authService, times(1)).validatePasswordMatch(req.currentPassword(), user.getPassword()); | ||
| verify(passwordEncoder, never()).encode(anyString()); | ||
| verify(tokenVersionRedisRepository, never()).deleteTokenVersion(anyLong()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
기존 코드에서 UNVERIFIED_EMAIL 사용 여부를 확인해 주세요.
UserErrorCode.UNVERIFIED_EMAIL을 사용하던 기존 코드가 모두AuthErrorCode.UNVERIFIED_EMAIL로 업데이트되었는지 확인이 필요합니다.다음 스크립트로 기존 사용처를 확인해 주세요:
🏁 Script executed:
Length of output: 1574
테스트 코드에서 UserErrorCode.UNVERIFIED_EMAIL 참조 수정 필요
UserServiceTest에서 여전히
UserErrorCode.UNVERIFIED_EMAIL을 사용하고 있으므로,AuthErrorCode.UNVERIFIED_EMAIL로 변경해 주세요.🤖 Prompt for AI Agents