-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [chore] CustomerFixture 주석 추가 - CustomerFixture 의 기능과 목적을 설명하는 주석 명시 * [test] SignInCustomerServiceTest 추가 - 로그인 성공, 로그인 실패 - 이메일 없음, 패스워드 불일치 추가 * [feat] SignInCustomerService 구현 - SignInCustomerService.signIn 구현 - 이메일 없음과 패스워드 불일치를 같이 처리하면서 보안 강화 * [feat] SignInCustomerCommand 구현 - 구매자 로그인에 필요한 email, password 필드 추가 * [feat] CustomerRepository.findByEmail 추가 - 구매자 로그인 시 구매자 검색에 필요한 findByEmail 구현 * [feat] AuthenticationException - 구매자 로그인 실패 시 인증 오류 구현 * [test] CustomerControllerTest.signIn 추가 - CustomerControllerTest.testSignInCustomer: 구매자 로그인 테스트 성공 - CustomerControllerTest.testSignInCustomerFail: 구매자 로그인 테스트 실패 * [feat] CustomerController.signIn 구현 - 구매자 로그인 성공 시 OK 반환 - 구매자 로그인 실패 시 BadRequest 반환 * [feat] ErrorCode.AUTH_INVALID_CREDENTIALS - 로그인 실패 시 ErrorCode 추가 * [feat] SignInCustomerRequest - 구매자 로그인을 위한 SignInCustomerRequest 이메일, 패스워드 필드 추가 * [feat] CustomerAuthenticationException 구현 - 구매자 로그인 실패 시 예외 추가 * [feat] CustomerErrorCode.AUTHENTICATION_FAILED 구현 - 로그인 실패 시 에러코드 구현 * [feat] SignInCustomerService.signIn 에러 분기 - 이메일이 존재하지 않는 지 비밀번호가 존재하지 않는 지 서버 시점에서 명확히 구분할 수 있도록 수정 * [feat] UnauthorizedException 생성자 추가 - UnauthorizedException(ErroCode, String) 생성자 추가 * [fix] AuthenticationException 수정에 따른 반영 * [docs] SignInCustomerService 예외처리 명시 * [feat] SignInCustomerService.signIn 반환 값 변경 - 로그인 된 구매자의 UUID 를 전달하도록 수정 * [refactor] SignInCustomerRequest 패키지 변경 * [feat] CustomerRepository.findByEmail 반환값 변경 * [feat] Customer 로그인 인증 주체 변경 * [test] CustomerApiController.login 테스트 추가 - CustomerApiControllerTest.testLoginCustomer: 로그인 성공 시 테스트 - CustomerApiControllerTest.testLoginFail: 로그인 실패 시 테스트 * [feat] CustomerApiController.login 추가 - 로그인 인증 후 session 에 LoginCustomer 저장 * [feat] CustomerExceptionHandler.handleCustomerAuthenticationException 추가 - 로그인 실패 시 발생하는 예외 처리 * [feat] SignInCustomerResponse 구현 - CustomerApiController.login 반환값 추가 * [refactor] CustomerErrorCode Status 처리 - 기존 400 로 관리하던 Status 를 HttpStatus 를 이용해 관리하도록 수정 * [fix] SignInCustomerService.signIn 로직 수정 - 비밀번호가 일치하지 않을 때 예외 던지도록 수정 * [fix] CustomerRepository.findByEmail 반환값 변경에 따른 수정 - 반환값을 Optional 로 랩핑 * [refactor] 중복 코드 추출 - ProblemDetail 를 생성하는 코드가 겹쳐서 private 메소드로 추출
- Loading branch information
Showing
15 changed files
with
272 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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
9 changes: 9 additions & 0 deletions
9
src/main/java/camp/woowak/lab/customer/exception/CustomerAuthenticationException.java
This file contains 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,9 @@ | ||
package camp.woowak.lab.customer.exception; | ||
|
||
import camp.woowak.lab.common.exception.UnauthorizedException; | ||
|
||
public class CustomerAuthenticationException extends UnauthorizedException { | ||
public CustomerAuthenticationException(String message) { | ||
super(CustomerErrorCode.AUTHENTICATION_FAILED, message); | ||
} | ||
} |
13 changes: 8 additions & 5 deletions
13
src/main/java/camp/woowak/lab/customer/exception/CustomerErrorCode.java
This file contains 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
3 changes: 3 additions & 0 deletions
3
src/main/java/camp/woowak/lab/customer/repository/CustomerRepository.java
This file contains 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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package camp.woowak.lab.customer.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import camp.woowak.lab.customer.domain.Customer; | ||
|
||
public interface CustomerRepository extends JpaRepository<Customer, Long> { | ||
Optional<Customer> findByEmail(String email); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/camp/woowak/lab/customer/service/SignInCustomerService.java
This file contains 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,35 @@ | ||
package camp.woowak.lab.customer.service; | ||
|
||
import java.util.UUID; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import camp.woowak.lab.customer.domain.Customer; | ||
import camp.woowak.lab.customer.exception.CustomerAuthenticationException; | ||
import camp.woowak.lab.customer.repository.CustomerRepository; | ||
import camp.woowak.lab.customer.service.command.SignInCustomerCommand; | ||
import camp.woowak.lab.web.authentication.PasswordEncoder; | ||
|
||
@Service | ||
public class SignInCustomerService { | ||
private final CustomerRepository customerRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
public SignInCustomerService(CustomerRepository customerRepository, PasswordEncoder passwordEncoder) { | ||
this.customerRepository = customerRepository; | ||
this.passwordEncoder = passwordEncoder; | ||
} | ||
|
||
/** | ||
* @throws CustomerAuthenticationException 이메일이 존재하지 않거나 비밀번호가 일치하지 않으면 | ||
*/ | ||
public UUID signIn(SignInCustomerCommand cmd) { | ||
Customer byEmail = customerRepository.findByEmail(cmd.email()) | ||
.orElseThrow(() -> new CustomerAuthenticationException("email not found")); | ||
if (!byEmail.validatePassword(cmd.password(), passwordEncoder)) { | ||
throw new CustomerAuthenticationException("password not matched"); | ||
} | ||
|
||
return byEmail.getId(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/camp/woowak/lab/customer/service/command/SignInCustomerCommand.java
This file contains 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,7 @@ | ||
package camp.woowak.lab.customer.service.command; | ||
|
||
public record SignInCustomerCommand( | ||
String email, | ||
String password | ||
) { | ||
} |
This file contains 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 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
10 changes: 10 additions & 0 deletions
10
src/main/java/camp/woowak/lab/web/dto/request/customer/SignInCustomerRequest.java
This file contains 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,10 @@ | ||
package camp.woowak.lab.web.dto.request.customer; | ||
|
||
/** | ||
* 이메일 비밀번호 조건을 알 수 없도록 모든 요청을 받을 수 있도록 구현 | ||
*/ | ||
public record SignInCustomerRequest( | ||
String email, | ||
String password | ||
) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/camp/woowak/lab/web/dto/response/customer/SignInCustomerResponse.java
This file contains 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,4 @@ | ||
package camp.woowak.lab.web.dto.response.customer; | ||
|
||
public record SignInCustomerResponse() { | ||
} |
This file contains 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
83 changes: 83 additions & 0 deletions
83
src/test/java/camp/woowak/lab/customer/service/SignInCustomerServiceTest.java
This file contains 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,83 @@ | ||
package camp.woowak.lab.customer.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import camp.woowak.lab.customer.domain.Customer; | ||
import camp.woowak.lab.customer.exception.CustomerAuthenticationException; | ||
import camp.woowak.lab.customer.repository.CustomerRepository; | ||
import camp.woowak.lab.customer.service.command.SignInCustomerCommand; | ||
import camp.woowak.lab.fixture.CustomerFixture; | ||
import camp.woowak.lab.payaccount.domain.PayAccount; | ||
import camp.woowak.lab.payaccount.domain.TestPayAccount; | ||
import camp.woowak.lab.web.authentication.PasswordEncoder; | ||
|
||
/** | ||
* | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
public class SignInCustomerServiceTest implements CustomerFixture { | ||
@InjectMocks | ||
private SignInCustomerService signInCustomerService; | ||
|
||
@Mock | ||
private CustomerRepository customerRepository; | ||
|
||
@Mock | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@Test | ||
@DisplayName("로그인 성공") | ||
void testSignIn() { | ||
// given | ||
PayAccount newPayAccount = new TestPayAccount(1L); | ||
Customer customer = createCustomer(newPayAccount, passwordEncoder); | ||
SignInCustomerCommand cmd = new SignInCustomerCommand(customer.getEmail(), customer.getPassword()); | ||
given(customerRepository.findByEmail(customer.getEmail())).willReturn(Optional.of(customer)); | ||
given(passwordEncoder.matches(cmd.password(), customer.getPassword())).willReturn(true); | ||
|
||
// when & then | ||
assertDoesNotThrow(() -> signInCustomerService.signIn(cmd)); | ||
verify(customerRepository).findByEmail(customer.getEmail()); | ||
verify(passwordEncoder).matches(cmd.password(), customer.getPassword()); | ||
} | ||
|
||
@Test | ||
@DisplayName("로그인 실패 - 이메일 없음") | ||
void testSignInFailEmailNotFound() { | ||
// given | ||
PayAccount newPayAccount = new TestPayAccount(1L); | ||
Customer customer = createCustomer(newPayAccount, passwordEncoder); | ||
SignInCustomerCommand cmd = new SignInCustomerCommand("[email protected]", customer.getPassword()); | ||
given(customerRepository.findByEmail(cmd.email())).willReturn(Optional.empty()); | ||
|
||
// when & then | ||
assertThrows(CustomerAuthenticationException.class, () -> signInCustomerService.signIn(cmd)); | ||
verify(customerRepository).findByEmail(cmd.email()); | ||
} | ||
|
||
@Test | ||
@DisplayName("로그인 실패 - 패스워드 불일치") | ||
void testSignInFail() { | ||
// given | ||
PayAccount newPayAccount = new TestPayAccount(1L); | ||
Customer customer = createCustomer(newPayAccount, passwordEncoder); | ||
SignInCustomerCommand cmd = new SignInCustomerCommand(customer.getEmail(), customer.getPassword()); | ||
given(customerRepository.findByEmail(customer.getEmail())).willReturn(Optional.of(customer)); | ||
given(passwordEncoder.matches(cmd.password(), customer.getPassword())).willReturn(false); | ||
|
||
// when & then | ||
assertThrows(CustomerAuthenticationException.class, () -> signInCustomerService.signIn(cmd)); | ||
verify(customerRepository).findByEmail(customer.getEmail()); | ||
verify(passwordEncoder).matches(cmd.password(), customer.getPassword()); | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -5,14 +5,18 @@ | |
import camp.woowak.lab.payaccount.domain.PayAccount; | ||
import camp.woowak.lab.web.authentication.PasswordEncoder; | ||
|
||
/** | ||
* CustomerFixture는 Customer와 관련된 테스트에서 공통적으로 사용되는 객체를 생성하는 인터페이스입니다. | ||
*/ | ||
public interface CustomerFixture { | ||
default PayAccount createPayAccount() { | ||
return new PayAccount(); | ||
} | ||
|
||
default Customer createCustomer(PayAccount payAccount, PasswordEncoder passwordEncoder) { | ||
try { | ||
return new Customer("vendorName", "[email protected]", "vendorPassword", "010-0000-0000", payAccount, | ||
return new Customer("customerName", "[email protected]", "customerPassword", "010-0000-0000", | ||
payAccount, | ||
passwordEncoder); | ||
} catch (InvalidCreationException e) { | ||
throw new RuntimeException(e); | ||
|
Oops, something went wrong.