@@ -102,7 +102,7 @@ void createPost_success() throws Exception {
102102 }
103103
104104 @ Test
105- @ DisplayName ("존재하지 않는 사용자 → 404 Not Found" )
105+ @ DisplayName ("게시글 생성 실패 - 존재하지 않는 사용자 → 404 Not Found" )
106106 void createPost_userNotFound () throws Exception {
107107 // given: 토큰만 발급(실제 DB엔 없음)
108108 String fakeToken = testJwtTokenProvider .createAccessToken (999L , "ghost" , "USER" );
@@ -121,7 +121,7 @@ void createPost_userNotFound() throws Exception {
121121 }
122122
123123 @ Test
124- @ DisplayName ("존재하지 않는 카테고리 → 404 Not Found" )
124+ @ DisplayName ("게시글 생성 실패 - 존재하지 않는 카테고리 → 404 Not Found" )
125125 void createPost_categoryNotFound () throws Exception {
126126 // given: 정상 유저
127127 User user =
User .
createUser (
"writer2" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
@@ -146,7 +146,7 @@ void createPost_categoryNotFound() throws Exception {
146146 }
147147
148148 @ Test
149- @ DisplayName ("잘못된 요청(필드 누락) → 400 Bad Request" )
149+ @ DisplayName ("게시글 생성 실패 - 잘못된 요청(필드 누락) → 400 Bad Request" )
150150 void createPost_badRequest () throws Exception {
151151 // given: 정상 유저 생성
152152 User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
@@ -174,6 +174,22 @@ void createPost_badRequest() throws Exception {
174174 .andExpect (jsonPath ("$.message" ).value ("잘못된 요청입니다." ));
175175 }
176176
177+ @ Test
178+ @ DisplayName ("게시글 생성 실패 - 토큰 없음 → 401 Unauthorized" )
179+ void createPost_noToken () throws Exception {
180+ // given
181+ PostRequest request = new PostRequest ("제목" , "내용" , null );
182+
183+ // when & then
184+ mvc .perform (post ("/api/posts" )
185+ .contentType (MediaType .APPLICATION_JSON )
186+ .content (objectMapper .writeValueAsString (request )))
187+ .andDo (print ())
188+ .andExpect (status ().isUnauthorized ())
189+ .andExpect (jsonPath ("$.code" ).value ("AUTH_001" ))
190+ .andExpect (jsonPath ("$.message" ).value ("인증이 필요합니다." ));
191+ }
192+
177193 // ====================== 게시글 조회 테스트 ======================
178194
179195 @ Test
@@ -251,4 +267,178 @@ void getPost_fail_notFound() throws Exception {
251267 .andExpect (jsonPath ("$.code" ).value ("POST_001" ))
252268 .andExpect (jsonPath ("$.message" ).value ("존재하지 않는 게시글입니다." ));
253269 }
270+
271+ // ====================== 게시글 수정 테스트 ======================
272+
273+ @ Test
274+ @ DisplayName ("게시글 수정 성공 → 200 OK" )
275+ void updatePost_success () throws Exception {
276+ // given
277+ User user =
User .
createUser (
"writer" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
278+ user .setUserProfile (new UserProfile (user , "홍길동" , null , null , null , 0 ));
279+ user .setUserStatus (UserStatus .ACTIVE );
280+ userRepository .save (user );
281+
282+ PostCategory c1 = new PostCategory ("공지사항" );
283+ postCategoryRepository .save (c1 );
284+
285+ Post post = new Post (user , "원래 제목" , "원래 내용" );
286+ post .updateCategories (List .of (c1 ));
287+ postRepository .save (post );
288+
289+ String accessToken = generateAccessToken (user );
290+
291+ PostCategory c2 = new PostCategory ("자유게시판" );
292+ postCategoryRepository .save (c2 );
293+
294+ PostRequest request = new PostRequest ("수정된 게시글" , "안녕하세요, 수정했습니다!" , List .of (c1 .getId (), c2 .getId ()));
295+
296+ // when & then
297+ mvc .perform (put ("/api/posts/{postId}" , post .getId ())
298+ .header ("Authorization" , "Bearer " + accessToken )
299+ .contentType (MediaType .APPLICATION_JSON )
300+ .content (objectMapper .writeValueAsString (request )))
301+ .andDo (print ())
302+ .andExpect (status ().isOk ())
303+ .andExpect (jsonPath ("$.success" ).value (true ))
304+ .andExpect (jsonPath ("$.code" ).value ("SUCCESS_200" ))
305+ .andExpect (jsonPath ("$.data.title" ).value ("수정된 게시글" ))
306+ .andExpect (jsonPath ("$.data.categories.length()" ).value (2 ));
307+ }
308+
309+ @ Test
310+ @ DisplayName ("게시글 수정 실패 - 게시글 없음 → 404 Not Found" )
311+ void updatePost_fail_notFound () throws Exception {
312+ // given
313+ User user =
User .
createUser (
"writer2" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
314+ user .setUserProfile (new UserProfile (user , "작성자2" , null , null , null , 0 ));
315+ user .setUserStatus (UserStatus .ACTIVE );
316+ userRepository .save (user );
317+
318+ String accessToken = generateAccessToken (user );
319+
320+ PostRequest request = new PostRequest ("수정된 제목" , "내용" , List .of ());
321+
322+ // when & then
323+ mvc .perform (put ("/api/posts/{postId}" , 999L )
324+ .header ("Authorization" , "Bearer " + accessToken )
325+ .contentType (MediaType .APPLICATION_JSON )
326+ .content (objectMapper .writeValueAsString (request )))
327+ .andDo (print ())
328+ .andExpect (status ().isNotFound ())
329+ .andExpect (jsonPath ("$.code" ).value ("POST_001" ))
330+ .andExpect (jsonPath ("$.message" ).value ("존재하지 않는 게시글입니다." ));
331+ }
332+
333+ @ Test
334+ @ DisplayName ("게시글 수정 실패 - 작성자 아님 → 403 Forbidden" )
335+ void updatePost_fail_noPermission () throws Exception {
336+ // given
337+ User writer =
User .
createUser (
"writer3" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
338+ writer .setUserProfile (new UserProfile (writer , "작성자3" , null , null , null , 0 ));
339+ writer .setUserStatus (UserStatus .ACTIVE );
340+ userRepository .save (writer );
341+
342+ User another =
User .
createUser (
"other" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
343+ another .setUserProfile (new UserProfile (another , "다른사람" , null , null , null , 0 ));
344+ another .setUserStatus (UserStatus .ACTIVE );
345+ userRepository .save (another );
346+
347+ PostCategory c1 = new PostCategory ("공지사항" );
348+ postCategoryRepository .save (c1 );
349+
350+ Post post = new Post (writer , "원래 제목" , "원래 내용" );
351+ post .updateCategories (List .of (c1 ));
352+ postRepository .save (post );
353+
354+ String accessToken = generateAccessToken (another );
355+
356+ PostRequest request = new PostRequest ("수정된 제목" , "수정된 내용" , List .of (c1 .getId ()));
357+
358+ // when & then
359+ mvc .perform (put ("/api/posts/{postId}" , post .getId ())
360+ .header ("Authorization" , "Bearer " + accessToken )
361+ .contentType (MediaType .APPLICATION_JSON )
362+ .content (objectMapper .writeValueAsString (request )))
363+ .andDo (print ())
364+ .andExpect (status ().isForbidden ())
365+ .andExpect (jsonPath ("$.code" ).value ("POST_002" ))
366+ .andExpect (jsonPath ("$.message" ).value ("게시글 작성자만 수정/삭제할 수 있습니다." ));
367+ }
368+
369+ @ Test
370+ @ DisplayName ("게시글 수정 실패 - 존재하지 않는 카테고리 → 404 Not Found" )
371+ void updatePost_fail_categoryNotFound () throws Exception {
372+ // given
373+ User user =
User .
createUser (
"writer4" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
374+ user .setUserProfile (new UserProfile (user , "작성자4" , null , null , null , 0 ));
375+ user .setUserStatus (UserStatus .ACTIVE );
376+ userRepository .save (user );
377+
378+ PostCategory c1 = new PostCategory ("공지사항" );
379+ postCategoryRepository .save (c1 );
380+
381+ Post post = new Post (user , "원래 제목" , "원래 내용" );
382+ post .updateCategories (List .of (c1 ));
383+ postRepository .save (post );
384+
385+ String accessToken = generateAccessToken (user );
386+
387+ // 존재하지 않는 카테고리 ID
388+ PostRequest request = new PostRequest ("수정된 제목" , "수정된 내용" , List .of (999L ));
389+
390+ // when & then
391+ mvc .perform (put ("/api/posts/{postId}" , post .getId ())
392+ .header ("Authorization" , "Bearer " + accessToken )
393+ .contentType (MediaType .APPLICATION_JSON )
394+ .content (objectMapper .writeValueAsString (request )))
395+ .andDo (print ())
396+ .andExpect (status ().isNotFound ())
397+ .andExpect (jsonPath ("$.code" ).value ("POST_003" ))
398+ .andExpect (jsonPath ("$.message" ).value ("존재하지 않는 카테고리입니다." ));
399+ }
400+
401+ @ Test
402+ @ DisplayName ("게시글 수정 실패 - 잘못된 요청(필드 누락) → 400 Bad Request" )
403+ void updatePost_fail_badRequest () throws Exception {
404+ // given
405+ User user =
User .
createUser (
"writer5" ,
"[email protected] " ,
passwordEncoder .
encode (
"P@ssw0rd!" ));
406+ user .setUserProfile (new UserProfile (user , "작성자5" , null , null , null , 0 ));
407+ user .setUserStatus (UserStatus .ACTIVE );
408+ userRepository .save (user );
409+
410+ String accessToken = generateAccessToken (user );
411+
412+ String invalidJson = """
413+ {
414+ "content": "본문만 있음"
415+ }
416+ """ ;
417+
418+ // when & then
419+ mvc .perform (put ("/api/posts/{postId}" , 1L )
420+ .header ("Authorization" , "Bearer " + accessToken )
421+ .contentType (MediaType .APPLICATION_JSON )
422+ .content (invalidJson ))
423+ .andDo (print ())
424+ .andExpect (status ().isBadRequest ())
425+ .andExpect (jsonPath ("$.code" ).value ("COMMON_400" ))
426+ .andExpect (jsonPath ("$.message" ).value ("잘못된 요청입니다." ));
427+ }
428+
429+ @ Test
430+ @ DisplayName ("게시글 수정 실패 - 인증 없음 → 401 Unauthorized" )
431+ void updatePost_fail_unauthorized () throws Exception {
432+ // given
433+ PostRequest request = new PostRequest ("제목" , "내용" , List .of ());
434+
435+ // when & then
436+ mvc .perform (put ("/api/posts/{postId}" , 1L )
437+ .contentType (MediaType .APPLICATION_JSON )
438+ .content (objectMapper .writeValueAsString (request )))
439+ .andDo (print ())
440+ .andExpect (status ().isUnauthorized ())
441+ .andExpect (jsonPath ("$.code" ).value ("AUTH_001" ))
442+ .andExpect (jsonPath ("$.message" ).value ("인증이 필요합니다." ));
443+ }
254444}
0 commit comments