Modify NoteControllerTest WithUserDetails로 테스트하도록 수정
This commit is contained in:
@@ -4,6 +4,8 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.test.context.support.TestExecutionEvent;
|
||||
import org.springframework.security.test.context.support.WithUserDetails;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
@@ -35,12 +37,12 @@ class NoteControllerTest extends TestConfig {
|
||||
.alwaysDo(print())
|
||||
.build();
|
||||
User user2 = User.builder()
|
||||
.username("user")
|
||||
.username("user123")
|
||||
.password("user")
|
||||
.authority("ROLE_USER")
|
||||
.build();
|
||||
User admin2 = User.builder()
|
||||
.username("admin")
|
||||
.username("admin123")
|
||||
.password("admin")
|
||||
.authority("ROLE_ADMIN")
|
||||
.build();
|
||||
@@ -57,9 +59,15 @@ class NoteControllerTest extends TestConfig {
|
||||
}
|
||||
|
||||
@Test
|
||||
//WithUserDetails 로 테스트 하는 방법
|
||||
@WithUserDetails(
|
||||
value = "user123", // userDetailsService를 통해 가져올 수 있는 유저
|
||||
userDetailsServiceBeanName = "userDetailServiceImpl", // UserDetailsService 구현체의 Bean
|
||||
setupBefore = TestExecutionEvent.TEST_EXECUTION // 테스트 실행 직전에 유저를 가져온다.
|
||||
)
|
||||
void getNote_인증있음() throws Exception {
|
||||
mvc.perform(
|
||||
get("/note").with(user(user))
|
||||
get("/note")
|
||||
).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@@ -75,9 +83,14 @@ class NoteControllerTest extends TestConfig {
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithUserDetails(
|
||||
value = "admin123",
|
||||
userDetailsServiceBeanName = "userDetailServiceImpl",
|
||||
setupBefore = TestExecutionEvent.TEST_EXECUTION
|
||||
)
|
||||
void postNote_어드민인증있음() throws Exception {
|
||||
mvc.perform(
|
||||
post("/note").with(csrf()).with(user(admin))
|
||||
post("/note").with(csrf())
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.param("title", "제목")
|
||||
.param("content", "내용")
|
||||
@@ -85,9 +98,14 @@ class NoteControllerTest extends TestConfig {
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithUserDetails(
|
||||
value = "user123",
|
||||
userDetailsServiceBeanName = "userDetailServiceImpl",
|
||||
setupBefore = TestExecutionEvent.TEST_EXECUTION
|
||||
)
|
||||
void postNote_유저인증있음() throws Exception {
|
||||
mvc.perform(
|
||||
post("/note").with(csrf()).with(user(user))
|
||||
post("/note").with(csrf())
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.param("title", "제목")
|
||||
.param("content", "내용")
|
||||
@@ -105,19 +123,29 @@ class NoteControllerTest extends TestConfig {
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithUserDetails(
|
||||
value = "user123",
|
||||
userDetailsServiceBeanName = "userDetailServiceImpl",
|
||||
setupBefore = TestExecutionEvent.TEST_EXECUTION
|
||||
)
|
||||
void deleteNote_인증있음() throws Exception {
|
||||
Note note = noteRepository.save(new Note("제목", "내용", user));
|
||||
mvc.perform(
|
||||
delete("/note?id=" + note.getId()).with(csrf()).with(user(user))
|
||||
delete("/note?id=" + note.getId()).with(csrf())
|
||||
).andExpect(redirectedUrl("note"))
|
||||
.andExpect(status().is3xxRedirection());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithUserDetails(
|
||||
value = "admin123",
|
||||
userDetailsServiceBeanName = "userDetailServiceImpl",
|
||||
setupBefore = TestExecutionEvent.TEST_EXECUTION
|
||||
)
|
||||
void deleteNote_어드민계정있음() throws Exception {
|
||||
Note note = noteRepository.save(new Note("제목", "내용", user));
|
||||
mvc.perform(
|
||||
delete("/note?id=" + note.getId()).with(csrf()).with(user(admin))
|
||||
delete("/note?id=" + note.getId()).with(csrf())
|
||||
).andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user