@@ -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}
0 commit comments