From 4aace5f57164822582c3d3d182e20572dca41995 Mon Sep 17 00:00:00 2001 From: Rebwon Date: Mon, 27 Sep 2021 10:23:22 +0900 Subject: [PATCH] Refactor code - refactor test code - refactor sessionManager --- .../LoginAccountMethodArgumentResolver.java | 4 +-- .../infrastructure/SessionManager.java | 5 ++-- ...SessionBasedLoginAccountProcessorTest.java | 2 +- .../integration/AccountIntegrationTests.java | 23 +++++++++------- .../presentation/AccountCommandApiTests.java | 26 +++++++++++-------- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/yam/app/account/infrastructure/LoginAccountMethodArgumentResolver.java b/src/main/java/com/yam/app/account/infrastructure/LoginAccountMethodArgumentResolver.java index 5b2104f..a0a4171 100644 --- a/src/main/java/com/yam/app/account/infrastructure/LoginAccountMethodArgumentResolver.java +++ b/src/main/java/com/yam/app/account/infrastructure/LoginAccountMethodArgumentResolver.java @@ -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(); } } diff --git a/src/main/java/com/yam/app/account/infrastructure/SessionManager.java b/src/main/java/com/yam/app/account/infrastructure/SessionManager.java index 1cce1d9..42695a9 100644 --- a/src/main/java/com/yam/app/account/infrastructure/SessionManager.java +++ b/src/main/java/com/yam/app/account/infrastructure/SessionManager.java @@ -17,8 +17,8 @@ public final class SessionManager { this.httpSession.setAttribute(LOGIN_ACCOUNT, principal); } - public Optional 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(); } } diff --git a/src/test/java/com/yam/app/account/infrastructure/SessionBasedLoginAccountProcessorTest.java b/src/test/java/com/yam/app/account/infrastructure/SessionBasedLoginAccountProcessorTest.java index 7f8a52c..b11b748 100644 --- a/src/test/java/com/yam/app/account/infrastructure/SessionBasedLoginAccountProcessorTest.java +++ b/src/test/java/com/yam/app/account/infrastructure/SessionBasedLoginAccountProcessorTest.java @@ -42,7 +42,7 @@ final class SessionBasedLoginAccountProcessorTest { // Assert var result = sessionManager.fetchPrincipal(); - assertThat(result.isPresent()).isTrue(); + assertThat(result).isNotNull(); }), dynamicTest("이메일이 유효하지 않은 경우 예외를 리턴한다.", () -> { // Act diff --git a/src/test/java/com/yam/app/account/integration/AccountIntegrationTests.java b/src/test/java/com/yam/app/account/integration/AccountIntegrationTests.java index 5310b85..373f95c 100644 --- a/src/test/java/com/yam/app/account/integration/AccountIntegrationTests.java +++ b/src/test/java/com/yam/app/account/integration/AccountIntegrationTests.java @@ -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) ); diff --git a/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java b/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java index 81baeeb..c96bd34 100644 --- a/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java +++ b/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java @@ -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))