Refactor code
- refactor test code - refactor sessionManager
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package com.yam.app.account.infrastructure;
|
||||
|
||||
import com.yam.app.common.UnauthorizedRequestException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -23,7 +22,6 @@ public final class LoginAccountMethodArgumentResolver implements HandlerMethodAr
|
||||
.getSession(false);
|
||||
|
||||
var sessionManager = new SessionManager(session);
|
||||
return sessionManager.fetchPrincipal()
|
||||
.orElseThrow(() -> new UnauthorizedRequestException("Failed fetch principal"));
|
||||
return sessionManager.fetchPrincipal();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ public final class SessionManager {
|
||||
this.httpSession.setAttribute(LOGIN_ACCOUNT, principal);
|
||||
}
|
||||
|
||||
public Optional<AccountPrincipal> fetchPrincipal() {
|
||||
return Optional.ofNullable((AccountPrincipal) httpSession.getAttribute(LOGIN_ACCOUNT));
|
||||
public AccountPrincipal fetchPrincipal() {
|
||||
return (AccountPrincipal) httpSession.getAttribute(LOGIN_ACCOUNT);
|
||||
}
|
||||
|
||||
public boolean isExistPrincipal() {
|
||||
@@ -27,5 +27,6 @@ public final class SessionManager {
|
||||
|
||||
public void removePrincipal() {
|
||||
this.httpSession.removeAttribute(LOGIN_ACCOUNT);
|
||||
this.httpSession.invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ final class SessionBasedLoginAccountProcessorTest {
|
||||
|
||||
// Assert
|
||||
var result = sessionManager.fetchPrincipal();
|
||||
assertThat(result.isPresent()).isTrue();
|
||||
assertThat(result).isNotNull();
|
||||
}),
|
||||
dynamicTest("이메일이 유효하지 않은 경우 예외를 리턴한다.", () -> {
|
||||
// Act
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.yam.app.account.integration;
|
||||
|
||||
import static com.yam.app.account.presentation.AccountApiUri.EMAIL_CONFIRM;
|
||||
import static com.yam.app.account.presentation.AccountApiUri.FIND_INFO;
|
||||
import static com.yam.app.account.presentation.AccountApiUri.LOGIN;
|
||||
import static com.yam.app.account.presentation.AccountApiUri.LOGOUT;
|
||||
import static com.yam.app.account.presentation.AccountApiUri.REGISTER;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
@@ -9,7 +14,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
import static org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yam.app.account.presentation.AccountApiUri;
|
||||
import com.yam.app.account.presentation.LoginAccountCommand;
|
||||
import com.yam.app.account.presentation.RegisterAccountCommand;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -57,7 +61,7 @@ final class AccountIntegrationTests {
|
||||
command.setPassword("password!");
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.REGISTER)
|
||||
final var actions = mockMvc.perform(post(REGISTER)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
@@ -73,7 +77,7 @@ final class AccountIntegrationTests {
|
||||
@DisplayName("이메일 인증에 적절한 토큰과 이메일 정보가 입력되고, 이메일 인증 상태가 성공적으로 압데이트 된다.")
|
||||
void email_and_token_verify_request_in_correctly() throws Exception {
|
||||
// Act
|
||||
final var actions = mockMvc.perform(get(AccountApiUri.EMAIL_CONFIRM)
|
||||
final var actions = mockMvc.perform(get(EMAIL_CONFIRM)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.param("token", "emailchecktoken")
|
||||
@@ -97,7 +101,7 @@ final class AccountIntegrationTests {
|
||||
command.setPassword("password!");
|
||||
|
||||
// Act & Assert
|
||||
mockMvc.perform(post(AccountApiUri.LOGIN)
|
||||
mockMvc.perform(post(LOGIN)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
@@ -105,7 +109,7 @@ final class AccountIntegrationTests {
|
||||
.andExpect(status().isOk())
|
||||
.andDo(
|
||||
result -> {
|
||||
final var actions = mockMvc.perform(get(AccountApiUri.FIND_INFO)
|
||||
final var actions = mockMvc.perform(get(FIND_INFO)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
);
|
||||
@@ -123,15 +127,16 @@ final class AccountIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("로그인 이후 성공적으로 로그아웃 하는 시나리오")
|
||||
void logout() throws Exception {
|
||||
@DisplayName("로그인에 적절한 파라미터를 입력하여, 성공하고 "
|
||||
+ "로그아웃하는 시나리오 테스트.")
|
||||
void login_success_and_authentication_member_logout_scenarios() throws Exception {
|
||||
//Arrange
|
||||
var command = new LoginAccountCommand();
|
||||
command.setEmail("loginCheck@gmail.com");
|
||||
command.setPassword("password!");
|
||||
|
||||
// Act & Assert
|
||||
mockMvc.perform(post(AccountApiUri.LOGIN)
|
||||
mockMvc.perform(post(LOGIN)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
@@ -139,7 +144,7 @@ final class AccountIntegrationTests {
|
||||
.andExpect(status().isOk())
|
||||
.andDo(
|
||||
result -> {
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.LOGOUT)
|
||||
final var actions = mockMvc.perform(post(LOGOUT)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
);
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package com.yam.app.account.presentation;
|
||||
|
||||
import static com.yam.app.account.presentation.AccountApiUri.EMAIL_CONFIRM;
|
||||
import static com.yam.app.account.presentation.AccountApiUri.LOGIN;
|
||||
import static com.yam.app.account.presentation.AccountApiUri.LOGOUT;
|
||||
import static com.yam.app.account.presentation.AccountApiUri.REGISTER;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
@@ -50,7 +54,7 @@ final class AccountCommandApiTests {
|
||||
@DisplayName("세션이 없는 상태로 로그아웃을 요청하면 401 에러를 반환한다.")
|
||||
void logout() throws Exception {
|
||||
//Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.LOGOUT)
|
||||
final var actions = mockMvc.perform(post(LOGOUT)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON));
|
||||
|
||||
@@ -73,7 +77,7 @@ final class AccountCommandApiTests {
|
||||
// Act
|
||||
doThrow(IllegalStateException.class).when(accountFacade).login(request);
|
||||
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.LOGIN)
|
||||
final var actions = mockMvc.perform(post(LOGIN)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request))
|
||||
@@ -95,7 +99,7 @@ final class AccountCommandApiTests {
|
||||
request.setPassword(args);
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.LOGIN)
|
||||
final var actions = mockMvc.perform(post(LOGIN)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request))
|
||||
@@ -115,7 +119,7 @@ final class AccountCommandApiTests {
|
||||
command.setPassword("password!1");
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.LOGIN)
|
||||
final var actions = mockMvc.perform(post(LOGIN)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
@@ -135,7 +139,7 @@ final class AccountCommandApiTests {
|
||||
command.setPassword(args);
|
||||
|
||||
//Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.LOGIN)
|
||||
final var actions = mockMvc.perform(post(LOGIN)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
@@ -164,7 +168,7 @@ final class AccountCommandApiTests {
|
||||
command.setEmail(args);
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(get(AccountApiUri.EMAIL_CONFIRM)
|
||||
final var actions = mockMvc.perform(get(EMAIL_CONFIRM)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.param(TOKEN, args)
|
||||
@@ -185,7 +189,7 @@ final class AccountCommandApiTests {
|
||||
command.setEmail(arg);
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(get(AccountApiUri.EMAIL_CONFIRM)
|
||||
final var actions = mockMvc.perform(get(EMAIL_CONFIRM)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.param(TOKEN, arg)
|
||||
@@ -212,7 +216,7 @@ final class AccountCommandApiTests {
|
||||
command.setPassword(arg);
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.REGISTER)
|
||||
final var actions = mockMvc.perform(post(REGISTER)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
);
|
||||
|
||||
@@ -234,7 +238,7 @@ final class AccountCommandApiTests {
|
||||
command.setPassword(arg);
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.REGISTER)
|
||||
final var actions = mockMvc.perform(post(REGISTER)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
@@ -254,7 +258,7 @@ final class AccountCommandApiTests {
|
||||
command.setPassword("password1!");
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.REGISTER)
|
||||
final var actions = mockMvc.perform(post(REGISTER)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
@@ -274,7 +278,7 @@ final class AccountCommandApiTests {
|
||||
command.setPassword(args);
|
||||
|
||||
// Act
|
||||
final var actions = mockMvc.perform(post(AccountApiUri.REGISTER)
|
||||
final var actions = mockMvc.perform(post(REGISTER)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(command))
|
||||
|
||||
Reference in New Issue
Block a user