@@ -441,4 +441,93 @@ void updatePost_fail_unauthorized() throws Exception {
441441 .andExpect (jsonPath ("$.code" ).value ("AUTH_001" ))
442442 .andExpect (jsonPath ("$.message" ).value ("인증이 필요합니다." ));
443443 }
444+
445+ // ====================== 게시글 삭제 테스트 ======================
446+
447+ @ Test
448+ @ DisplayName ("게시글 삭제 성공 → 200 OK" )
449+ void deletePost_success () throws Exception {
450+ // given
451+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
452+ user .setUserProfile (new UserProfile (user , "홍길동" , null , null , null , 0 ));
453+ user .setUserStatus (UserStatus .ACTIVE );
454+ userRepository .save (user );
455+
456+ Post post = new Post (user , "삭제할 제목" , "삭제할 내용" );
457+ postRepository .save (post );
458+
459+ String accessToken = generateAccessToken (user );
460+
461+ // when & then
462+ mvc .perform (delete ("/api/posts/{postId}" , post .getId ())
463+ .header ("Authorization" , "Bearer " + accessToken ))
464+ .andDo (print ())
465+ .andExpect (status ().isOk ())
466+ .andExpect (jsonPath ("$.success" ).value (true ))
467+ .andExpect (jsonPath ("$.code" ).value ("SUCCESS_200" ))
468+ .andExpect (jsonPath ("$.message" ).value ("게시글이 삭제되었습니다." ))
469+ .andExpect (jsonPath ("$.data" ).doesNotExist ());
470+ }
471+
472+ @ Test
473+ @ DisplayName ("게시글 삭제 실패 - 게시글 없음 → 404 Not Found" )
474+ void deletePost_fail_postNotFound () throws Exception {
475+ // given
476+ User user =
User .
createUser (
"writer2" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
477+ user .setUserProfile (new UserProfile (user , "작성자" , null , null , null , 0 ));
478+ user .setUserStatus (UserStatus .ACTIVE );
479+ userRepository .save (user );
480+
481+ String accessToken = generateAccessToken (user );
482+
483+ // when & then
484+ mvc .perform (delete ("/api/posts/{postId}" , 999L )
485+ .header ("Authorization" , "Bearer " + accessToken ))
486+ .andDo (print ())
487+ .andExpect (status ().isNotFound ())
488+ .andExpect (jsonPath ("$.code" ).value ("POST_001" ))
489+ .andExpect (jsonPath ("$.message" ).value ("존재하지 않는 게시글입니다." ));
490+ }
491+
492+ @ Test
493+ @ DisplayName ("게시글 삭제 실패 - 작성자 아님 → 403 Forbidden" )
494+ void deletePost_fail_noPermission () throws Exception {
495+ // given
496+ User writer =
User .
createUser (
"writer3" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
497+ writer .setUserProfile (new UserProfile (writer , "작성자3" , null , null , null , 0 ));
498+ writer .setUserStatus (UserStatus .ACTIVE );
499+ userRepository .save (writer );
500+
501+ User another =
User .
createUser (
"other" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
502+ another .setUserProfile (new UserProfile (another , "다른사람" , null , null , null , 0 ));
503+ another .setUserStatus (UserStatus .ACTIVE );
504+ userRepository .save (another );
505+
506+ Post post = new Post (writer , "원래 제목" , "원래 내용" );
507+ postRepository .save (post );
508+
509+ String accessToken = generateAccessToken (another );
510+
511+ // when & then
512+ mvc .perform (delete ("/api/posts/{postId}" , post .getId ())
513+ .header ("Authorization" , "Bearer " + accessToken ))
514+ .andDo (print ())
515+ .andExpect (status ().isForbidden ())
516+ .andExpect (jsonPath ("$.code" ).value ("POST_002" ))
517+ .andExpect (jsonPath ("$.message" ).value ("게시글 작성자만 수정/삭제할 수 있습니다." ));
518+ }
519+
520+ @ Test
521+ @ DisplayName ("게시글 삭제 실패 - 인증 없음 → 401 Unauthorized" )
522+ void deletePost_fail_unauthorized () throws Exception {
523+ // given
524+ Post post = new Post (); // 굳이 저장 안 해도 됨, 그냥 요청만 보냄
525+
526+ // when & then
527+ mvc .perform (delete ("/api/posts/{postId}" , 1L ))
528+ .andDo (print ())
529+ .andExpect (status ().isUnauthorized ())
530+ .andExpect (jsonPath ("$.code" ).value ("AUTH_001" ))
531+ .andExpect (jsonPath ("$.message" ).value ("인증이 필요합니다." ));
532+ }
444533}
0 commit comments