로그설정 완료

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.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockServletContext; 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.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.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension; 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(SpringRunner.class)
//@RunWith(MockitoJUnitRunner.class) //@RunWith(MockitoJUnitRunner.class)
@ExtendWith(SpringExtension.class) //@ExtendWith(SpringExtension.class)
//@ContextConfiguration(classes = { ApplicationConfig.class }) //@ContextConfiguration(classes = { ApplicationConfig.class })
@WebAppConfiguration //@WebAppConfiguration
@WebMvcTest(controllers = OAuth2LocalController.class)
//@SpringBootTest // @Controller, @ControllerAdvice 사용 가능 - @Service, @Component, @Repository 사용 불가
//@WebMvcTest(controllers = OAuth2LocalController.class)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
//@AutoConfigureMockMvc //@AutoConfigureMockMvc
@ActiveProfiles(value = {"test"})
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@ActiveProfiles(value = {"test"})
@RunWith( SpringRunner.class )
@AutoConfigureMockMvc
@ActiveProfiles( "local" )
@SpringBootTest
@WithMockUser
class OAuth2LocalControllerTest { class OAuth2LocalControllerTest {
@Autowired @Autowired
MockMvc mockMvc; MockMvc mockMvc;
//@Autowired
//TestRestTemplate testRestTemplate;
@Autowired @Autowired
private WebApplicationContext webApplicationContext; private WebApplicationContext ctx;
private CmmUser user; private CmmUser user;
@ -111,8 +133,10 @@ class OAuth2LocalControllerTest {
@BeforeEach @BeforeEach
void setUp() { 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() this.user = CmmUser.builder()
.cmmUserId(CommUtil.getStringFromUUID()) .cmmUserId(CommUtil.getStringFromUUID())
@ -135,13 +159,22 @@ class OAuth2LocalControllerTest {
@Test @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); // ServletContext servletContext = webApplicationContext.getServletContext();
assertTrue(servletContext instanceof MockServletContext); //
assertNotNull(webApplicationContext.getBean("OAuth2LocalController")); // assertNotNull(servletContext);
// assertTrue(servletContext instanceof MockServletContext);
// assertNotNull(webApplicationContext.getBean("OAuth2LocalController"));
// MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); // MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
@ -159,7 +192,7 @@ class OAuth2LocalControllerTest {
// given // given
userRepository.save(user); userRepository.saveAndFlush(user);
//given(userRepository.save(user)).willReturn(user); //given(userRepository.save(user)).willReturn(user);
//when(userRepository.findByUserId(user.getUserId())).thenReturn(user); //when(userRepository.findByUserId(user.getUserId())).thenReturn(user);
@ -174,17 +207,19 @@ class OAuth2LocalControllerTest {
loginRequestDto.setProviderType(ProviderType.LOCAL); loginRequestDto.setProviderType(ProviderType.LOCAL);
loginRequestDto.setUserId(user.getUserId()); loginRequestDto.setUserId(user.getUserId());
loginRequestDto.setPassword("Minuk926!@"); loginRequestDto.setPassword("Minuk926!@");
MvcResult result = mockMvc.perform( // MvcResult result = mockMvc.perform(
MockMvcRequestBuilders.post("/api/v1/auth/login") // MockMvcRequestBuilders.post("/api/v1/auth/login")
.content(Objects.requireNonNull(JsonUtil.toJson(loginRequestDto))) // .content(Objects.requireNonNull(JsonUtil.toJson(loginRequestDto)))
.contentType(MediaType.APPLICATION_JSON) // .contentType(MediaType.APPLICATION_JSON)
//.accept(MediaType.APPLICATION_JSON) // //.accept(MediaType.APPLICATION_JSON)
.characterEncoding("UTF-8") // //.characterEncoding("UTF-8")
).andDo(print()) // ).andDo(print())
.andExpect(status().isOk()) // .andExpect(status().isOk())
.andReturn(); // .andReturn();
//.andExpect(jsonPath("$.success").value(true)) //.andExpect(jsonPath("$.success").value(true))
//.andExpect(jsonPath("$.data").exists()); //.andExpect(jsonPath("$.data").exists());
// testRestTemplate.postForEntity("/api/v1/auth/login", loginRequestDto, ResponseEntity.class);
} }
@Test @Test

Loading…
Cancel
Save