ADD LoginAccountProcessor

- SessionUtils 삭제 - 이슈를 나눠 다른 PR에서 구현.
This commit is contained in:
JiwonDev
2021-09-07 18:34:09 +09:00
committed by Jiwon
parent a6f307becb
commit 1f954f92ad
7 changed files with 99 additions and 47 deletions

View File

@@ -19,12 +19,12 @@ final class AccountTranslator {
return new ConfirmRegisterAccountCommand(request.getToken(), request.getEmail());
}
public LoginAccountCommand toCommand(LoginAccountRequest request) {
return new LoginAccountCommand(request.getEmail(), request.getPassword());
}
public AccountResponse toResponse(Account entity) {
return new AccountResponse(entity.getId(), entity.getEmail(),
entity.getNickname());
}
public LoginAccountCommand toCommand(LoginAccountRequest request) {
return new LoginAccountCommand(request.getEmail(), request.getPassword());
}
}

View File

@@ -5,14 +5,27 @@ import com.yam.app.account.application.LoginAccountCommand;
public final class LoginAccountProcessor {
private final AccountReader accountReader;
private final PasswordEncrypter passwordEncrypter;
public LoginAccountProcessor(AccountReader accountReader) {
public LoginAccountProcessor(AccountReader accountReader,
PasswordEncrypter passwordEncrypter) {
this.accountReader = accountReader;
this.passwordEncrypter = passwordEncrypter;
}
/**
* 아직 구현되지 않은 메서드.
*/
public void login(LoginAccountCommand toCommand) {
var account = accountReader.findByEmail(toCommand.getEmail());
if (account.isEmpty()) {
throw new IllegalStateException();
}
final String password = account.get().getPassword();
boolean matches = passwordEncrypter.matches(toCommand.getPassword(),
passwordEncrypter.encode(password));
if (!matches) {
throw new IllegalStateException();
}
}
}

View File

@@ -64,7 +64,9 @@ public class AppConfiguration {
}
@Bean
public LoginAccountProcessor loginAccountProcessor(AccountReader accountReader){
return new LoginAccountProcessor(accountReader);
public LoginAccountProcessor loginAccountProcessor(AccountReader accountReader,
PasswordEncrypter passwordEncrypter) {
return new LoginAccountProcessor(accountReader, passwordEncrypter);
}
}

View File

@@ -34,7 +34,7 @@ public final class AccountQueryApi {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
SessionUtils.setLoginAccountEmail(session, request.getEmail());
// SessionUtils.setLoginAccountEmail(session, request.getEmail());
return ResponseEntity.ok().build();
}
}

View File

@@ -1,34 +0,0 @@
package com.yam.app.account.presentation;
import javax.servlet.http.HttpSession;
public final class SessionUtils {
private static final String LOGIN_MEMBER_ID = "LOGIN_MEMBER_ID";
private SessionUtils() {
}
/**
* 로그인한 회원 아이디를 세션에서 꺼낸다.
*
* @param session HttpSession
* @return 로그인한 회원의 id 또는 null
*/
public static String getLoginAccountEmail(HttpSession session) {
return (String) session.getAttribute(LOGIN_MEMBER_ID);
}
public static void setLoginAccountEmail(HttpSession session, String id) {
session.setAttribute(LOGIN_MEMBER_ID, id);
}
public static void clear(HttpSession session) {
session.invalidate();
}
public static void logoutMember(HttpSession session) {
session.removeAttribute(LOGIN_MEMBER_ID);
}
}

View File

@@ -0,0 +1,70 @@
package com.yam.app.account.domain;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import com.yam.app.account.application.LoginAccountCommand;
import com.yam.app.account.domain.PasswordEncrypterTest.PasswordEncrypterStub;
import java.util.Arrays;
import java.util.Collection;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
@DisplayName("회원 로그인 도메인 서비스")
class LoginAccountProcessorTest {
@TestFactory
@DisplayName("회원 로그인 시나리오")
Collection<DynamicTest> login_success() throws Exception {
//Arrange
var accountRepository = new FakeAccountRepository();
var accountReader = accountRepository;
var passwordEncrypter = new PasswordEncrypterStub();
var loginAccountProcessor = new LoginAccountProcessor(accountReader, passwordEncrypter);
var account = Account.of("hello@naver.com", "hello", "password!");
accountRepository.save(account);
return Arrays.asList(
dynamicTest("회원 로그인에 성공한다.", () -> {
// Arrange
var command = new LoginAccountCommand(account.getEmail(), account.getPassword());
// Act
var throwable = catchThrowable(
() -> loginAccountProcessor.login(command)
);
// Assert
assertThat(throwable).isNull();
}),
dynamicTest("이메일이 유효하지 않은 경우 예외를 리턴한다.", () -> {
// Arrange
var command = new LoginAccountCommand("dwqko@naver.com", account.getPassword());
// Act
var throwable = catchThrowable(
() -> loginAccountProcessor.login(command)
);
// Assert
assertThat(throwable).isInstanceOf(IllegalStateException.class);
}),
dynamicTest("비밀번호가 유효하지 않은 경우 예외를 리턴한다.", () -> {
// Arrange
var command = new LoginAccountCommand(account.getEmail(), "11111111!");
// Act
var throwable = catchThrowable(
() -> loginAccountProcessor.login(command)
);
// Assert
assertThat(throwable).isInstanceOf(IllegalStateException.class);
})
);
}
}

View File

@@ -32,10 +32,11 @@ class AccountQueryApiTest {
@Nested
@DisplayName("Login HTTP API")
class LoginAPI {
class LoginApi {
@Test
@DisplayName("정상적인 이메일과 비밀번호를 보내 로그인에 성공하고 200을 반환한다.")
void login_success() throws Exception{
void login_success() throws Exception {
//Arrange
LoginAccountRequest request = new LoginAccountRequest();
request.setEmail("jiwon@gmail.com");