Skip to content

Commit

Permalink
feat: TalkRoom Comment 삭제 API 테스트 코드 작성 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
AHNYUNKI committed Mar 25, 2024
1 parent d3c1444 commit 2cf58a6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jisungin.api.comment;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand Down Expand Up @@ -54,7 +55,7 @@ void writeCommentWithEmptyContent() throws Exception {
}

@Test
@DisplayName("의견을 작성한 유저가 자신의 의견을 수정할 수 있다.")
@DisplayName("의견을 작성한 유저가 자신의 의견을 수정한다.")
void editComment() throws Exception {
// given
CommentEditRequest request = CommentEditRequest.builder()
Expand All @@ -73,4 +74,18 @@ void editComment() throws Exception {
.andExpect(jsonPath("$.message").value("OK"));
}

@Test
@DisplayName("의견을 작성한 유저가 자신의 의견을 삭제한다.")
void deleteComment() throws Exception {
// when // then
mockMvc.perform(delete("/v1/talk-rooms/comments/1")
.contentType(APPLICATION_JSON)
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("200"))
.andExpect(jsonPath("$.status").value("OK"))
.andExpect(jsonPath("$.message").value("OK"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,68 @@ void editCommentWithUserB() {
.hasMessage("권한이 없는 사용자입니다.");
}

@Test
@DisplayName("의견을 작성한 유저가 의견을 삭제한다.")
void deleteComment() {
// given
User user = createUser();
userRepository.save(user);

Book book = createBook();
bookRepository.save(book);

TalkRoom talkRoom = createTalkRoom(book, user);
talkRoomRepository.save(talkRoom);

createTalkRoomRole(talkRoom);

Comment comment = createComment(user, talkRoom);
commentRepository.save(comment);

// when
commentService.deleteComment(comment.getId(), user.getId());

// then
List<Comment> comments = commentRepository.findAll();
assertThat(0).isEqualTo(comments.size());
}

@Test
@DisplayName("의견을 작성한 유저(userA)가 아닌 다른 유저(userB)가 의견을 삭제할 수 없다.")
void deleteCommentWithUserB() {
// given
User userA = createUser();
userRepository.save(userA);

User userB = User.builder()
.name("[email protected]")
.profileImage("image")
.oauthId(
OauthId.builder()
.oauthId("oauthId2")
.oauthType(OauthType.KAKAO)
.build()
)
.build();
userRepository.save(userB);

Book book = createBook();
bookRepository.save(book);

TalkRoom talkRoom = createTalkRoom(book, userA);
talkRoomRepository.save(talkRoom);

createTalkRoomRole(talkRoom);

Comment comment = createComment(userA, talkRoom);
commentRepository.save(comment);

// when // then
assertThatThrownBy(() -> commentService.deleteComment(comment.getId(), userB.getId()))
.isInstanceOf(BusinessException.class)
.hasMessage("권한이 없는 사용자입니다.");
}

private static Comment createComment(User user, TalkRoom talkRoom) {
return Comment.builder()
.content("의견")
Expand Down

0 comments on commit 2cf58a6

Please sign in to comment.