Skip to content

Commit 215721c

Browse files
committed
Test: 테스트 작성
1 parent 5c8dc60 commit 215721c

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

src/test/java/com/back/domain/board/controller/CommentControllerTest.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,135 @@ void updateComment_noToken() throws Exception {
390390
.andExpect(jsonPath("$.code").value("AUTH_001"))
391391
.andExpect(jsonPath("$.message").value("인증이 필요합니다."));
392392
}
393+
// ====================== 댓글 삭제 테스트 ======================
394+
395+
@Test
396+
@DisplayName("댓글 삭제 성공 → 200 OK")
397+
void deleteComment_success() throws Exception {
398+
// given
399+
User user = User.createUser("writer", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
400+
user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0));
401+
user.setUserStatus(UserStatus.ACTIVE);
402+
userRepository.save(user);
403+
404+
Post post = new Post(user, "제목", "내용");
405+
postRepository.save(post);
406+
407+
Comment comment = new Comment(post, user, "삭제할 댓글");
408+
commentRepository.save(comment);
409+
410+
String accessToken = generateAccessToken(user);
411+
412+
// when & then
413+
mvc.perform(delete("/api/posts/{postId}/comments/{commentId}", post.getId(), comment.getId())
414+
.header("Authorization", "Bearer " + accessToken))
415+
.andDo(print())
416+
.andExpect(status().isOk())
417+
.andExpect(jsonPath("$.success").value(true))
418+
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
419+
.andExpect(jsonPath("$.message").value("댓글이 삭제되었습니다."));
420+
}
421+
422+
@Test
423+
@DisplayName("댓글 삭제 실패 - 존재하지 않는 게시글 → 404 Not Found")
424+
void deleteComment_postNotFound() throws Exception {
425+
// given
426+
User user = User.createUser("writer2", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
427+
user.setUserProfile(new UserProfile(user, "작성자2", null, null, null, 0));
428+
user.setUserStatus(UserStatus.ACTIVE);
429+
userRepository.save(user);
430+
431+
Post post = new Post(user, "제목", "내용");
432+
postRepository.save(post);
433+
434+
Comment comment = new Comment(post, user, "댓글");
435+
commentRepository.save(comment);
436+
437+
String accessToken = generateAccessToken(user);
438+
439+
// when & then
440+
mvc.perform(delete("/api/posts/{postId}/comments/{commentId}", 999L, comment.getId())
441+
.header("Authorization", "Bearer " + accessToken))
442+
.andDo(print())
443+
.andExpect(status().isNotFound())
444+
.andExpect(jsonPath("$.code").value("POST_001"))
445+
.andExpect(jsonPath("$.message").value("존재하지 않는 게시글입니다."));
446+
}
447+
448+
@Test
449+
@DisplayName("댓글 삭제 실패 - 존재하지 않는 댓글 → 404 Not Found")
450+
void deleteComment_commentNotFound() throws Exception {
451+
// given
452+
User user = User.createUser("writer3", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
453+
user.setUserProfile(new UserProfile(user, "작성자3", null, null, null, 0));
454+
user.setUserStatus(UserStatus.ACTIVE);
455+
userRepository.save(user);
456+
457+
Post post = new Post(user, "제목", "내용");
458+
postRepository.save(post);
459+
460+
String accessToken = generateAccessToken(user);
461+
462+
// when & then
463+
mvc.perform(delete("/api/posts/{postId}/comments/{commentId}", post.getId(), 999L)
464+
.header("Authorization", "Bearer " + accessToken))
465+
.andDo(print())
466+
.andExpect(status().isNotFound())
467+
.andExpect(jsonPath("$.code").value("COMMENT_001"))
468+
.andExpect(jsonPath("$.message").value("존재하지 않는 댓글입니다."));
469+
}
470+
471+
@Test
472+
@DisplayName("댓글 삭제 실패 - 작성자가 아님 → 403 Forbidden")
473+
void deleteComment_noPermission() throws Exception {
474+
// given
475+
User writer = User.createUser("writer", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
476+
writer.setUserProfile(new UserProfile(writer, "작성자", null, null, null, 0));
477+
writer.setUserStatus(UserStatus.ACTIVE);
478+
userRepository.save(writer);
479+
480+
User other = User.createUser("other", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
481+
other.setUserProfile(new UserProfile(other, "다른사람", null, null, null, 0));
482+
other.setUserStatus(UserStatus.ACTIVE);
483+
userRepository.save(other);
484+
485+
Post post = new Post(writer, "제목", "내용");
486+
postRepository.save(post);
487+
488+
Comment comment = new Comment(post, writer, "원래 댓글");
489+
commentRepository.save(comment);
490+
491+
String accessToken = generateAccessToken(other);
492+
493+
// when & then
494+
mvc.perform(delete("/api/posts/{postId}/comments/{commentId}", post.getId(), comment.getId())
495+
.header("Authorization", "Bearer " + accessToken))
496+
.andDo(print())
497+
.andExpect(status().isForbidden())
498+
.andExpect(jsonPath("$.code").value("COMMENT_002"))
499+
.andExpect(jsonPath("$.message").value("댓글 작성자만 수정/삭제할 수 있습니다."));
500+
}
501+
502+
@Test
503+
@DisplayName("댓글 삭제 실패 - 토큰 없음 → 401 Unauthorized")
504+
void deleteComment_noToken() throws Exception {
505+
// given
506+
User user = User.createUser("writer4", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
507+
user.setUserProfile(new UserProfile(user, "작성자4", null, null, null, 0));
508+
user.setUserStatus(UserStatus.ACTIVE);
509+
userRepository.save(user);
510+
511+
Post post = new Post(user, "제목", "내용");
512+
postRepository.save(post);
513+
514+
Comment comment = new Comment(post, user, "댓글");
515+
commentRepository.save(comment);
516+
517+
// when & then
518+
mvc.perform(delete("/api/posts/{postId}/comments/{commentId}", post.getId(), comment.getId()))
519+
.andDo(print())
520+
.andExpect(status().isUnauthorized())
521+
.andExpect(jsonPath("$.code").value("AUTH_001"))
522+
.andExpect(jsonPath("$.message").value("인증이 필요합니다."));
523+
}
393524
}

src/test/java/com/back/domain/board/service/CommentServiceTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,95 @@ void updateComment_fail_noPermission() {
200200
).isInstanceOf(CustomException.class)
201201
.hasMessage(ErrorCode.COMMENT_NO_PERMISSION.getMessage());
202202
}
203+
// ====================== 댓글 삭제 테스트 ======================
204+
205+
@Test
206+
@DisplayName("댓글 삭제 성공")
207+
void deleteComment_success() {
208+
// given
209+
User user = User.createUser("writer", "[email protected]", "encodedPwd");
210+
user.setUserProfile(new UserProfile(user, "작성자", null, null, null, 0));
211+
user.setUserStatus(UserStatus.ACTIVE);
212+
userRepository.save(user);
213+
214+
Post post = new Post(user, "제목", "내용");
215+
postRepository.save(post);
216+
217+
Comment comment = new Comment(post, user, "삭제할 댓글");
218+
commentRepository.save(comment);
219+
220+
// when
221+
commentService.deleteComment(post.getId(), comment.getId(), user.getId());
222+
223+
// then
224+
assertThat(commentRepository.findById(comment.getId())).isEmpty();
225+
}
226+
227+
@Test
228+
@DisplayName("댓글 삭제 실패 - 존재하지 않는 게시글")
229+
void deleteComment_fail_postNotFound() {
230+
// given
231+
User user = User.createUser("writer2", "[email protected]", "encodedPwd");
232+
user.setUserProfile(new UserProfile(user, "작성자2", null, null, null, 0));
233+
user.setUserStatus(UserStatus.ACTIVE);
234+
userRepository.save(user);
235+
236+
Post post = new Post(user, "제목", "내용");
237+
postRepository.save(post);
238+
239+
Comment comment = new Comment(post, user, "댓글");
240+
commentRepository.save(comment);
241+
242+
// when & then
243+
assertThatThrownBy(() ->
244+
commentService.deleteComment(999L, comment.getId(), user.getId())
245+
).isInstanceOf(CustomException.class)
246+
.hasMessage(ErrorCode.POST_NOT_FOUND.getMessage());
247+
}
248+
249+
@Test
250+
@DisplayName("댓글 삭제 실패 - 존재하지 않는 댓글")
251+
void deleteComment_fail_commentNotFound() {
252+
// given
253+
User user = User.createUser("writer3", "[email protected]", "encodedPwd");
254+
user.setUserProfile(new UserProfile(user, "작성자3", null, null, null, 0));
255+
user.setUserStatus(UserStatus.ACTIVE);
256+
userRepository.save(user);
257+
258+
Post post = new Post(user, "제목", "내용");
259+
postRepository.save(post);
260+
261+
// when & then
262+
assertThatThrownBy(() ->
263+
commentService.deleteComment(post.getId(), 999L, user.getId())
264+
).isInstanceOf(CustomException.class)
265+
.hasMessage(ErrorCode.COMMENT_NOT_FOUND.getMessage());
266+
}
267+
268+
@Test
269+
@DisplayName("댓글 삭제 실패 - 작성자가 아님")
270+
void deleteComment_fail_noPermission() {
271+
// given: 작성자 + 다른 사용자
272+
User writer = User.createUser("writer", "[email protected]", "encodedPwd");
273+
writer.setUserProfile(new UserProfile(writer, "작성자", null, null, null, 0));
274+
writer.setUserStatus(UserStatus.ACTIVE);
275+
userRepository.save(writer);
276+
277+
User other = User.createUser("other", "[email protected]", "encodedPwd");
278+
other.setUserProfile(new UserProfile(other, "다른사람", null, null, null, 0));
279+
other.setUserStatus(UserStatus.ACTIVE);
280+
userRepository.save(other);
281+
282+
Post post = new Post(writer, "제목", "내용");
283+
postRepository.save(post);
284+
285+
Comment comment = new Comment(post, writer, "원래 댓글");
286+
commentRepository.save(comment);
287+
288+
// when & then
289+
assertThatThrownBy(() ->
290+
commentService.deleteComment(post.getId(), comment.getId(), other.getId())
291+
).isInstanceOf(CustomException.class)
292+
.hasMessage(ErrorCode.COMMENT_NO_PERMISSION.getMessage());
293+
}
203294
}

0 commit comments

Comments
 (0)