Skip to content

Commit

Permalink
refactor: 비밀번호 재설정 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
5uhwann committed Sep 23, 2023
1 parent 0ec2694 commit 4787fb1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import com.plzgraduate.myongjigraduatebe.core.meta.WebAdapter;
import com.plzgraduate.myongjigraduatebe.core.meta.UseCase;
import com.plzgraduate.myongjigraduatebe.user.application.port.in.find.FindUserUseCase;
import com.plzgraduate.myongjigraduatebe.user.application.port.in.resetpassword.ResetPasswordCommand;
import com.plzgraduate.myongjigraduatebe.user.application.port.in.resetpassword.ResetPasswordUseCase;
import com.plzgraduate.myongjigraduatebe.user.application.port.out.FindUserPort;
import com.plzgraduate.myongjigraduatebe.user.application.port.out.UpdateUserPort;
import com.plzgraduate.myongjigraduatebe.user.domain.model.User;

import lombok.RequiredArgsConstructor;

@WebAdapter
@UseCase
@Transactional
@RequiredArgsConstructor
public class ResetPasswordService implements ResetPasswordUseCase {

private final FindUserPort findUserPort;
private final FindUserUseCase findUserUseCase;
private final UpdateUserPort updateUserPort;
private final PasswordEncoder passwordEncoder;

@Override
public void resetPassword(ResetPasswordCommand resetPasswordCommand) {
checkMatchPassword(resetPasswordCommand);
User user = findUserUseCase.findUserByAuthId(resetPasswordCommand.getAuthId());
user.resetPassword(passwordEncoder.encode(resetPasswordCommand.getNewPassword()));
updateUserPort.updateUser(user);
}

private static void checkMatchPassword(ResetPasswordCommand resetPasswordCommand) {
if (!resetPasswordCommand.getNewPassword().equals(resetPasswordCommand.getPasswordCheck())) {
throw new IllegalArgumentException("비밀번호와 비밀번호 확인이 일치하지 않습니다.");
}
User user = findUserPort.findUserByAuthId(resetPasswordCommand.getAuthId())
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 아이디입니다."));
user.resetPassword(passwordEncoder.encode(resetPasswordCommand.getNewPassword()));
updateUserPort.updateUser(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;

import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -14,16 +12,16 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.plzgraduate.myongjigraduatebe.user.application.port.in.find.FindUserUseCase;
import com.plzgraduate.myongjigraduatebe.user.application.port.in.resetpassword.ResetPasswordCommand;
import com.plzgraduate.myongjigraduatebe.user.application.port.out.FindUserPort;
import com.plzgraduate.myongjigraduatebe.user.application.port.out.UpdateUserPort;
import com.plzgraduate.myongjigraduatebe.user.domain.model.User;

@ExtendWith(MockitoExtension.class)
class ResetPasswordServiceTest {

@Mock
private FindUserPort findUserPort;
private FindUserUseCase findUserUseCase;
@Mock
private UpdateUserPort updateUserPort;
@Mock
Expand All @@ -41,8 +39,7 @@ void resetPassword() {
.newPassword("testPassword")
.passwordCheck("testPassword").build();
User user = User.builder().build();
given(findUserPort.findUserByAuthId(resetPasswordCommand.getAuthId())).willReturn(
Optional.of(user));
given(findUserUseCase.findUserByAuthId(resetPasswordCommand.getAuthId())).willReturn(user);

//when
resetPasswordService.resetPassword(resetPasswordCommand);
Expand Down Expand Up @@ -74,8 +71,8 @@ void resetPasswordNonUser() {
.authId("test")
.newPassword("testPassword")
.passwordCheck("testPassword").build();
given(findUserPort.findUserByAuthId(resetPasswordCommand.getAuthId())).willReturn(
Optional.empty());
given(findUserUseCase.findUserByAuthId(resetPasswordCommand.getAuthId())).willThrow(
new IllegalArgumentException("존재하지 않는 아이디입니다."));

//when //then
assertThatThrownBy(() -> resetPasswordService.resetPassword(resetPasswordCommand)).isInstanceOf(
Expand Down

0 comments on commit 4787fb1

Please sign in to comment.