diff --git a/app/src/test/java/com/techcourse/controller/LoginControllerTest.java b/app/src/test/java/com/techcourse/controller/LoginControllerTest.java new file mode 100644 index 0000000000..1a6bb79c06 --- /dev/null +++ b/app/src/test/java/com/techcourse/controller/LoginControllerTest.java @@ -0,0 +1,63 @@ +package com.techcourse.controller; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.techcourse.domain.User; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class LoginControllerTest { + + @Test + void 로그인_성공_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + HttpSession session = mock(HttpSession.class); + when(request.getParameter("account")).thenReturn("gugu"); + when(request.getParameter("password")).thenReturn("password"); + when(request.getSession()).thenReturn(session); + + LoginController loginController = new LoginController(); + String execute = loginController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); + } + + @Test + void 로그인_실패_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + HttpSession session = mock(HttpSession.class); + when(request.getParameter("account")).thenReturn("power"); + when(request.getParameter("password")).thenReturn("password"); + when(request.getSession()).thenReturn(session); + + LoginController loginController = new LoginController(); + String execute = loginController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("redirect:/401.jsp"); + } + + @Test + void 세션_존재_할때_로그인_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + final var session = mock(HttpSession.class); + + when(session.getAttribute("user")).thenReturn(new User("power","power","power@naver.com")); + when(request.getSession()).thenReturn(session); + + LoginController loginController = new LoginController(); + String execute = loginController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); + } +} diff --git a/app/src/test/java/com/techcourse/controller/LoginViewControllerTest.java b/app/src/test/java/com/techcourse/controller/LoginViewControllerTest.java new file mode 100644 index 0000000000..61b8edd832 --- /dev/null +++ b/app/src/test/java/com/techcourse/controller/LoginViewControllerTest.java @@ -0,0 +1,47 @@ +package com.techcourse.controller; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.techcourse.domain.User; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class LoginViewControllerTest { + + @Test + void 세션_존재_할때_로그인_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + final var session = mock(HttpSession.class); + + when(session.getAttribute("user")).thenReturn(new User("power","power","power@naver.com")); + when(request.getSession()).thenReturn(session); + + LoginViewController loginViewController = new LoginViewController(); + String execute = loginViewController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); + } + + @Test + void 세션_존재_하지_않을때_로그인_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + final var session = mock(HttpSession.class); + + when(request.getSession()).thenReturn(session); + + LoginViewController loginViewController = new LoginViewController(); + String execute = loginViewController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("/login.jsp"); + } +} diff --git a/app/src/test/java/com/techcourse/controller/LogoutControllerTest.java b/app/src/test/java/com/techcourse/controller/LogoutControllerTest.java new file mode 100644 index 0000000000..643d427500 --- /dev/null +++ b/app/src/test/java/com/techcourse/controller/LogoutControllerTest.java @@ -0,0 +1,32 @@ +package com.techcourse.controller; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class LogoutControllerTest { + + + @Test + void 로그인_아웃_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + final var session = mock(HttpSession.class); + + when(request.getSession()).thenReturn(session); + + LogoutController logoutController = new LogoutController(); + String execute = logoutController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("redirect:/"); + } +} diff --git a/app/src/test/java/com/techcourse/controller/RegisterControllerTest.java b/app/src/test/java/com/techcourse/controller/RegisterControllerTest.java new file mode 100644 index 0000000000..b331f30778 --- /dev/null +++ b/app/src/test/java/com/techcourse/controller/RegisterControllerTest.java @@ -0,0 +1,38 @@ +package com.techcourse.controller; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.techcourse.domain.User; +import com.techcourse.repository.InMemoryUserRepository; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class RegisterControllerTest { + + @Test + void 회원가입_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + HttpSession session = mock(HttpSession.class); + + when(request.getParameter("account")).thenReturn("power"); + when(request.getParameter("password")).thenReturn("password"); + when(request.getParameter("email")).thenReturn("power@naver.com"); + when(request.getSession()).thenReturn(session); + + RegisterController registerController = new RegisterController(); + String execute = registerController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("redirect:/index.jsp"); + User user = InMemoryUserRepository.findByAccount("power").get(); + Assertions.assertThat(user.checkPassword("password")).isTrue(); + } +} diff --git a/app/src/test/java/com/techcourse/controller/RegisterViewControllerTest.java b/app/src/test/java/com/techcourse/controller/RegisterViewControllerTest.java new file mode 100644 index 0000000000..9c94784039 --- /dev/null +++ b/app/src/test/java/com/techcourse/controller/RegisterViewControllerTest.java @@ -0,0 +1,30 @@ +package com.techcourse.controller; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class RegisterViewControllerTest { + + @Test + void 회원가입_페이지_출력_테스트() throws Exception { + final var request = mock(HttpServletRequest.class); + final var response = mock(HttpServletResponse.class); + HttpSession session = mock(HttpSession.class); + when(request.getSession()).thenReturn(session); + + RegisterViewController registerViewController = new RegisterViewController(); + String execute = registerViewController.execute(request, response); + + Assertions.assertThat(execute).isEqualTo("/register.jsp"); + } +} diff --git a/app/src/test/java/com/techcourse/repository/InMemoryUserRepositoryTest.java b/app/src/test/java/com/techcourse/repository/InMemoryUserRepositoryTest.java new file mode 100644 index 0000000000..bc9c0cd3eb --- /dev/null +++ b/app/src/test/java/com/techcourse/repository/InMemoryUserRepositoryTest.java @@ -0,0 +1,22 @@ +package com.techcourse.repository; + +import com.techcourse.domain.User; +import java.util.Optional; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class InMemoryUserRepositoryTest { + + @Test + void 유저_저장_테스트() { + User user = new User("power", "password", "power@naver.com"); + InMemoryUserRepository.save(user); + + Optional power = InMemoryUserRepository.findByAccount("power"); + Assertions.assertThat(power.get().getId()).isEqualTo(2L); + } +}