Add User Test추가

This commit is contained in:
Daeil Choi
2023-02-06 12:26:46 +09:00
parent 3f5f5cbc4b
commit b7d58486ac
3 changed files with 126 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ package com.example.springsecuritystudy.notice;
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.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@@ -10,7 +11,6 @@ import org.springframework.web.context.WebApplicationContext;
import com.example.springsecuritystudy.helper.TestConfig;
import com.example.springsecuritystudy.helper.WithMockAdmin;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
@@ -22,8 +22,6 @@ class NoticeControllerTest extends TestConfig {
@Autowired
private NoticeRepository noticeRepository;
@Autowired
private ObjectMapper objectMapper;
private MockMvc mvc;
@BeforeEach
@@ -49,30 +47,33 @@ class NoticeControllerTest extends TestConfig {
@Test
void postNotice_인증없음() throws Exception {
String content = objectMapper.writeValueAsString(new Notice("제목", "내용"));
mvc.perform(post("/notice")
.content(content))
.andExpect(status().is4xxClientError());
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("title", "제목")
.param("content", "내용")
).andExpect(status().is4xxClientError());
}
@Test
@WithMockUser(roles = "USER", username = "user", password = "user")
void postNotice_유저인증있음() throws Exception {
String content = objectMapper.writeValueAsString(new Notice("제목", "내용"));
mvc.perform(post("/notice")
.with(csrf())
.content(content))
.andExpect(status().is4xxClientError());
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("title", "제목")
.param("content", "내용")
).andExpect(status().is4xxClientError());
}
@Test
@WithMockAdmin
void postNotice_어드민인증있음() throws Exception {
String content = objectMapper.writeValueAsString(new Notice("제목", "내용"));
mvc.perform(post("/notice")
.with(csrf())
.content(content))
.andExpect(redirectedUrl("notice"))
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("title", "제목")
.param("content", "내용")
).andExpect(redirectedUrl("notice"))
.andExpect(status().is3xxRedirection());
}

View File

@@ -0,0 +1,41 @@
package com.example.springsecuritystudy.user;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.example.springsecuritystudy.helper.TestConfig;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
class UserControllerTest extends TestConfig {
private MockMvc mvc;
@BeforeEach
void setUp(@Autowired WebApplicationContext context) {
this.mvc = MockMvcBuilders.webAppContextSetup(context)
.apply(springSecurity())
.alwaysDo(print())
.build();
}
@Test
void signup() throws Exception {
mvc.perform(
post("/signup").with(csrf())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("username", "user123")
.param("password", "password")
).andExpect(redirectedUrl("login"))
.andExpect(status().is3xxRedirection());
}
}

View File

@@ -0,0 +1,72 @@
package com.example.springsecuritystudy.user;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.springsecuritystudy.helper.TestConfig;
import static org.assertj.core.api.BDDAssertions.*;
import static org.junit.jupiter.api.Assertions.*;
class UserServiceTest extends TestConfig {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Test
void signup() {
//given
String username = "user123";
String password = "password";
//when
User user = userService.signup(username, password);
//then
then(user.getId()).isNotNull();
then(user.getUsername()).isEqualTo(username);
then(user.getPassword()).startsWith("{bcrypt}");
then(user.getAuthorities()).hasSize(1);
then(user.getAuthorities().stream().findFirst().get().getAuthority()).isEqualTo("ROLE_USER");
then(user.isAdmin()).isFalse();
then(user.isAccountNonExpired()).isTrue();
then(user.isAccountNonLocked()).isTrue();
then(user.isEnabled()).isTrue();
then(user.isCredentialsNonExpired()).isTrue();
}
@Test
void signupAdmin() {
//given
String username = "admin123";
String password = "password";
//when
User user = userService.signupAdmin(username, password);
//then
then(user.getId()).isNotNull();
then(user.getUsername()).isEqualTo(username);
then(user.getPassword()).startsWith("{bcrypt}");
then(user.getAuthorities()).hasSize(1);
then(user.getAuthorities().stream().findFirst().get().getAuthority()).isEqualTo("ROLE_ADMIN");
then(user.isAdmin()).isTrue();
then(user.isAccountNonExpired()).isTrue();
then(user.isAccountNonLocked()).isTrue();
then(user.isEnabled()).isTrue();
then(user.isCredentialsNonExpired()).isTrue();
}
@Test
void findByUsername() {
//given
User user = User.builder()
.username("user123")
.password("password")
.authority("ROLE_USER")
.build();
userRepository.save(user);
//when
User savedUser = userService.findByUsername("user123");
//then
then(savedUser.getId()).isNotNull();
}
}