diff --git a/src/test/java/com/xit/core/controller/core/OAuth2LocalControllerTest.java b/src/test/java/com/xit/core/controller/core/OAuth2LocalControllerTest.java index fcca578..ebe4142 100644 --- a/src/test/java/com/xit/core/controller/core/OAuth2LocalControllerTest.java +++ b/src/test/java/com/xit/core/controller/core/OAuth2LocalControllerTest.java @@ -39,7 +39,12 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.mock.web.MockServletContext; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -72,20 +77,37 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. //@RunWith(SpringRunner.class) //@RunWith(MockitoJUnitRunner.class) -@ExtendWith(SpringExtension.class) +//@ExtendWith(SpringExtension.class) + + //@ContextConfiguration(classes = { ApplicationConfig.class }) -@WebAppConfiguration -@WebMvcTest(controllers = OAuth2LocalController.class) -//@SpringBootTest +//@WebAppConfiguration + +// @Controller, @ControllerAdvice 사용 가능 - @Service, @Component, @Repository 사용 불가 +//@WebMvcTest(controllers = OAuth2LocalController.class) + +//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) //@AutoConfigureMockMvc -@ActiveProfiles(value = {"test"}) + +//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +//@ActiveProfiles(value = {"test"}) + + +@RunWith( SpringRunner.class ) +@AutoConfigureMockMvc +@ActiveProfiles( "local" ) +@SpringBootTest +@WithMockUser class OAuth2LocalControllerTest { @Autowired MockMvc mockMvc; + //@Autowired + //TestRestTemplate testRestTemplate; + @Autowired - private WebApplicationContext webApplicationContext; + private WebApplicationContext ctx; private CmmUser user; @@ -111,8 +133,10 @@ class OAuth2LocalControllerTest { @BeforeEach void setUp() { - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build(); - + //this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build(); + this.mockMvc = MockMvcBuilders.webAppContextSetup( this.ctx ) + .addFilter( new CharacterEncodingFilter( "UTF-8", true ) ) + .build(); this.user = CmmUser.builder() .cmmUserId(CommUtil.getStringFromUUID()) @@ -135,13 +159,22 @@ class OAuth2LocalControllerTest { @Test - void login() throws Exception { + @PreAuthorize("isAuthenticated()") + public void print() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + User principal = (User)authentication.getPrincipal(); + System.out.println(principal); + } - ServletContext servletContext = webApplicationContext.getServletContext(); + @Test + @PreAuthorize("isAuthenticated()") + void login() throws Exception { - assertNotNull(servletContext); - assertTrue(servletContext instanceof MockServletContext); - assertNotNull(webApplicationContext.getBean("OAuth2LocalController")); +// ServletContext servletContext = webApplicationContext.getServletContext(); +// +// assertNotNull(servletContext); +// assertTrue(servletContext instanceof MockServletContext); +// assertNotNull(webApplicationContext.getBean("OAuth2LocalController")); // MultiValueMap params = new LinkedMultiValueMap<>(); @@ -159,7 +192,7 @@ class OAuth2LocalControllerTest { // given - userRepository.save(user); + userRepository.saveAndFlush(user); //given(userRepository.save(user)).willReturn(user); //when(userRepository.findByUserId(user.getUserId())).thenReturn(user); @@ -174,17 +207,19 @@ class OAuth2LocalControllerTest { loginRequestDto.setProviderType(ProviderType.LOCAL); loginRequestDto.setUserId(user.getUserId()); loginRequestDto.setPassword("Minuk926!@"); - MvcResult result = mockMvc.perform( - MockMvcRequestBuilders.post("/api/v1/auth/login") - .content(Objects.requireNonNull(JsonUtil.toJson(loginRequestDto))) - .contentType(MediaType.APPLICATION_JSON) - //.accept(MediaType.APPLICATION_JSON) - .characterEncoding("UTF-8") - ).andDo(print()) - .andExpect(status().isOk()) - .andReturn(); +// MvcResult result = mockMvc.perform( +// MockMvcRequestBuilders.post("/api/v1/auth/login") +// .content(Objects.requireNonNull(JsonUtil.toJson(loginRequestDto))) +// .contentType(MediaType.APPLICATION_JSON) +// //.accept(MediaType.APPLICATION_JSON) +// //.characterEncoding("UTF-8") +// ).andDo(print()) +// .andExpect(status().isOk()) +// .andReturn(); //.andExpect(jsonPath("$.success").value(true)) //.andExpect(jsonPath("$.data").exists()); + +// testRestTemplate.postForEntity("/api/v1/auth/login", loginRequestDto, ResponseEntity.class); } @Test