-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
import com.seniors.common.constant.OAuthProvider; | ||
import com.seniors.common.constant.ResultCode; | ||
import com.seniors.config.security.CustomUserDetails; | ||
import com.seniors.config.security.JwtUtil; | ||
import com.seniors.config.security.TokenService; | ||
import com.seniors.domain.post.dto.PostDto.PostCreateDto; | ||
import com.seniors.domain.post.dto.PostDto.SetPostDto; | ||
import com.seniors.domain.post.entity.Post; | ||
import com.seniors.domain.post.entity.PostLike; | ||
import com.seniors.domain.post.repository.post.PostRepository; | ||
|
@@ -17,20 +20,24 @@ | |
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON; | ||
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
|
@@ -44,6 +51,7 @@ | |
@Transactional | ||
class PostControllerTest { | ||
|
||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
@Autowired | ||
|
@@ -54,6 +62,11 @@ class PostControllerTest { | |
@Autowired | ||
private UsersRepository usersRepository; | ||
private Authentication authentication; | ||
private String accessToken; | ||
|
||
@Autowired | ||
private TokenService tokenService; | ||
|
||
private Users users; | ||
|
||
@BeforeEach | ||
|
@@ -71,6 +84,7 @@ void setUp() { | |
|
||
users = usersRepository.save(Users.builder() | ||
.snsId(String.valueOf(randomNumber)) | ||
.nickname("tester1") | ||
.email("[email protected]") | ||
.gender("male") | ||
.ageRange("20~29") | ||
|
@@ -86,7 +100,10 @@ void setUp() { | |
users.getProfileImageUrl()); | ||
userDetails.setUserId(users.getId()); | ||
|
||
accessToken = tokenService.generateToken(users, Long.valueOf(1000 * 60 * 60 * 24)); | ||
|
||
authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
|
||
|
@@ -96,13 +113,20 @@ void postAdd() throws Exception { | |
// given | ||
String title = "글 제목입니다."; | ||
String content = "글 내용입니다."; | ||
PostCreateDto createDto = PostCreateDto.builder().title(title).content(content).build(); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String createDtoJson = objectMapper.writeValueAsString(createDto); | ||
|
||
// MockMultipartFile file = new MockMultipartFile("files", "tooth.png", "multipart/form-data", "uploadFile".getBytes(StandardCharsets.UTF_8)); | ||
MockMultipartFile request = new MockMultipartFile("data", null, "application/json", createDtoJson.getBytes(StandardCharsets.UTF_8)); | ||
|
||
// expected | ||
mockMvc.perform(post("/api/posts") | ||
.contentType(APPLICATION_JSON) | ||
.param("title", title) | ||
.param("content", content) | ||
.principal(authentication) | ||
mockMvc.perform(multipart(HttpMethod.POST, "/api/posts") | ||
// .file(file) | ||
.file(request) | ||
.accept(APPLICATION_JSON) | ||
.contentType(MULTIPART_FORM_DATA) | ||
.header("Authorization", accessToken) | ||
) | ||
.andExpect(status().isOk()) | ||
.andDo(print()); | ||
|
@@ -112,16 +136,15 @@ void postAdd() throws Exception { | |
@DisplayName("생성 요청 시 title 값은 필수") | ||
void postAddNotExistTitle() throws Exception { | ||
// given | ||
PostCreateDto request = PostCreateDto.builder() | ||
.content("글 내용입니다.") | ||
.build(); | ||
String json = objectMapper.writeValueAsString(request); | ||
|
||
SetPostDto setPostDto = PostCreateDto.of("글 내용입니다.", ""); | ||
String json = objectMapper.writeValueAsString(setPostDto); | ||
MockMultipartFile request = new MockMultipartFile("data", null, "application/json", json.getBytes(StandardCharsets.UTF_8)); | ||
// expected | ||
mockMvc.perform(post("/api/posts") | ||
.contentType(APPLICATION_JSON) | ||
.content(json) | ||
.principal(authentication) | ||
mockMvc.perform(multipart(HttpMethod.POST, "/api/posts") | ||
.file(request) | ||
.accept(APPLICATION_JSON) | ||
.contentType(MULTIPART_FORM_DATA) | ||
.header("Authorization", accessToken) | ||
) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(jsonPath("$.success").value(false)) | ||
|
@@ -179,15 +202,18 @@ void modifyPost() throws Exception { | |
// given | ||
Post post = postRepository.save(Post.of("글 제목1", "글 내용1", users)); | ||
|
||
// given | ||
String title = "글 수정 제목1입니다."; | ||
String content = "글 수정 내용1입니다."; | ||
SetPostDto setPostDto = PostCreateDto.of(title, content); | ||
String json = objectMapper.writeValueAsString(setPostDto); | ||
MockMultipartFile request = new MockMultipartFile("data", null, "application/json", json.getBytes(StandardCharsets.UTF_8)); | ||
|
||
// expected | ||
mockMvc.perform(patch("/api/posts/{postId}", post.getId()) | ||
.contentType(APPLICATION_JSON) | ||
.param("title", title) | ||
.param("content", content) | ||
.principal(authentication) | ||
mockMvc.perform(multipart(HttpMethod.PATCH, "/api/posts/{postId}", post.getId()) | ||
.file(request) | ||
.accept(APPLICATION_JSON) | ||
.contentType(MULTIPART_FORM_DATA) | ||
.header("Authorization", accessToken) | ||
) | ||
.andExpect(status().isOk()) | ||
.andDo(print()); | ||
|
Oops, something went wrong.