Skip to content

Commit 40fb4dd

Browse files
committed
Test: 테스트 작성
1 parent f2f3925 commit 40fb4dd

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,63 @@ void updatePost_fail_categoryNotFound() {
275275
.isInstanceOf(CustomException.class)
276276
.hasMessage(ErrorCode.CATEGORY_NOT_FOUND.getMessage());
277277
}
278+
279+
// ====================== 게시글 삭제 테스트 ======================
280+
281+
@Test
282+
@DisplayName("게시글 삭제 성공 - 작성자 본인")
283+
void deletePost_success() {
284+
// given
285+
User user = User.createUser("writer", "[email protected]", "encodedPwd");
286+
user.setUserProfile(new UserProfile(user, "작성자", null, null, null, 0));
287+
user.setUserStatus(UserStatus.ACTIVE);
288+
userRepository.save(user);
289+
290+
Post post = new Post(user, "삭제 대상 제목", "삭제 대상 내용");
291+
postRepository.save(post);
292+
293+
// when
294+
postService.deletePost(post.getId(), user.getId());
295+
296+
// then
297+
assertThat(postRepository.findById(post.getId())).isEmpty();
298+
}
299+
300+
@Test
301+
@DisplayName("게시글 삭제 실패 - 게시글 없음")
302+
void deletePost_fail_postNotFound() {
303+
// given
304+
User user = User.createUser("writer2", "[email protected]", "encodedPwd");
305+
user.setUserProfile(new UserProfile(user, "작성자2", null, null, null, 0));
306+
user.setUserStatus(UserStatus.ACTIVE);
307+
userRepository.save(user);
308+
309+
// when & then
310+
assertThatThrownBy(() -> postService.deletePost(999L, user.getId()))
311+
.isInstanceOf(CustomException.class)
312+
.hasMessage(ErrorCode.POST_NOT_FOUND.getMessage());
313+
}
314+
315+
@Test
316+
@DisplayName("게시글 삭제 실패 - 작성자 아님")
317+
void deletePost_fail_noPermission() {
318+
// given
319+
User writer = User.createUser("writer3", "[email protected]", "encodedPwd");
320+
writer.setUserProfile(new UserProfile(writer, "작성자3", null, null, null, 0));
321+
writer.setUserStatus(UserStatus.ACTIVE);
322+
userRepository.save(writer);
323+
324+
User another = User.createUser("other", "[email protected]", "encodedPwd");
325+
another.setUserProfile(new UserProfile(another, "다른사람", null, null, null, 0));
326+
another.setUserStatus(UserStatus.ACTIVE);
327+
userRepository.save(another);
328+
329+
Post post = new Post(writer, "원래 제목", "원래 내용");
330+
postRepository.save(post);
331+
332+
// when & then
333+
assertThatThrownBy(() -> postService.deletePost(post.getId(), another.getId()))
334+
.isInstanceOf(CustomException.class)
335+
.hasMessage(ErrorCode.POST_NO_PERMISSION.getMessage());
336+
}
278337
}

0 commit comments

Comments
 (0)