From a67e446f4355cbc983f6533b61b7fc99e164f267 Mon Sep 17 00:00:00 2001 From: dongHyo Date: Wed, 1 Jun 2022 01:22:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20UserController=20login=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/application/UserControllerTest.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 server/src/test/java/com/ticketing/server/user/application/UserControllerTest.java diff --git a/server/src/test/java/com/ticketing/server/user/application/UserControllerTest.java b/server/src/test/java/com/ticketing/server/user/application/UserControllerTest.java new file mode 100644 index 0000000..866c2c9 --- /dev/null +++ b/server/src/test/java/com/ticketing/server/user/application/UserControllerTest.java @@ -0,0 +1,94 @@ +package com.ticketing.server.user.application; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.ticketing.server.user.service.interfaces.UserService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + +@SpringBootTest +@Transactional +class UserControllerTest { + + @Autowired + WebApplicationContext context; + + @Autowired + UserService userService; + + @Autowired + PasswordEncoder passwordEncoder; + + MockMvc mvc; + + @BeforeEach + void init() throws Exception { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + + mvc.perform(post("/user") + .content("{\n" + + " \"email\": \"ticketing@gmail.com\",\n" + + " \"password\": \"qwe123\",\n" + + " \"name\": \"ticketing\",\n" + + " \"phone\": \"010-2240-7920\"\n" + + "}") + .contentType(MediaType.APPLICATION_JSON)); + } + + @Test + @DisplayName("로그인 인증 성공") + void loginSuccess() throws Exception { + // given + String requestJson = "{\n" + + " \"email\": \"ticketing@gmail.com\",\n" + + " \"password\": \"qwe123\"\n" + + "}"; + + // when + ResultActions actions = mvc.perform(post("/user/login") + .content(requestJson) + .contentType(MediaType.APPLICATION_JSON)); + + // then + actions.andDo(print()) + .andExpect(status().isOk()) + .andExpect(header().exists("ACCESS_TOKEN")); + } + + @Test + @DisplayName("로그인 패스워드 인증 실패") + void loginPasswordFail() throws Exception { + // given + String requestJson = "{\n" + + " \"email\": \"ticketing@gmail.com\",\n" + + " \"password\": \"qwe1234\"\n" + + "}"; + + // when + ResultActions actions = mvc.perform(post("/user/login") + .content(requestJson) + .contentType(MediaType.APPLICATION_JSON)); + + // then + actions.andDo(print()) + .andExpect(status().isUnauthorized()); + } + +} \ No newline at end of file