로그설정 완료

dev
minuk926 3 years ago
parent 5647d83377
commit 31bd63b9ad

@ -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<String, String> 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

Loading…
Cancel
Save