Skip to content

Commit 0a2ab8a

Browse files
committed
Test: 테스트 작성
1 parent 4fcbc68 commit 0a2ab8a

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

src/test/java/com/back/domain/user/controller/UserControllerTest.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.back.domain.board.comment.entity.Comment;
44
import com.back.domain.board.comment.repository.CommentRepository;
55
import com.back.domain.board.post.entity.Post;
6+
import com.back.domain.board.post.entity.PostBookmark;
7+
import com.back.domain.board.post.repository.PostBookmarkRepository;
68
import com.back.domain.board.post.repository.PostRepository;
79
import com.back.domain.user.dto.ChangePasswordRequest;
810
import com.back.domain.user.dto.UpdateUserProfileRequest;
@@ -49,6 +51,9 @@ class UserControllerTest {
4951
@Autowired
5052
private PostRepository postRepository;
5153

54+
@Autowired
55+
private PostBookmarkRepository postBookmarkRepository;
56+
5257
@Autowired
5358
private CommentRepository commentRepository;
5459

@@ -937,4 +942,143 @@ void getMyComments_expiredAccessToken() throws Exception {
937942
.andExpect(jsonPath("$.code").value("AUTH_004"))
938943
.andExpect(jsonPath("$.message").value("만료된 액세스 토큰입니다."));
939944
}
945+
946+
// ====================== 내 북마크 게시글 목록 조회 테스트 ======================
947+
948+
@Test
949+
@DisplayName("내 북마크 게시글 목록 조회 성공 → 200 OK")
950+
void getMyBookmarks_success() throws Exception {
951+
// given
952+
User user = User.createUser("bookmarkUser", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
953+
user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0));
954+
user.setUserStatus(UserStatus.ACTIVE);
955+
userRepository.save(user);
956+
957+
Post post1 = new Post(user, "JPA 영속성 전이 완벽 정리", "내용1", null);
958+
Post post2 = new Post(user, "테스트 코드 작성 가이드", "내용2", null);
959+
postRepository.saveAll(List.of(post1, post2));
960+
961+
PostBookmark bookmark1 = new PostBookmark(post1, user);
962+
PostBookmark bookmark2 = new PostBookmark(post2, user);
963+
postBookmarkRepository.saveAll(List.of(bookmark1, bookmark2));
964+
965+
String accessToken = generateAccessToken(user);
966+
967+
// when
968+
ResultActions resultActions = mvc.perform(
969+
get("/api/users/me/bookmarks")
970+
.header("Authorization", "Bearer " + accessToken)
971+
.param("page", "0")
972+
.param("size", "10")
973+
)
974+
.andDo(print());
975+
976+
// then
977+
resultActions
978+
.andExpect(status().isOk())
979+
.andExpect(jsonPath("$.success").value(true))
980+
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
981+
.andExpect(jsonPath("$.message").value("내 북마크 게시글 목록이 조회되었습니다."))
982+
.andExpect(jsonPath("$.data.items.length()").value(2))
983+
.andExpect(jsonPath("$.data.items[0].title").value("테스트 코드 작성 가이드"))
984+
.andExpect(jsonPath("$.data.items[1].title").value("JPA 영속성 전이 완벽 정리"));
985+
}
986+
987+
@Test
988+
@DisplayName("존재하지 않는 사용자 → 404 Not Found")
989+
void getMyBookmarks_userNotFound() throws Exception {
990+
// given
991+
String fakeToken = testJwtTokenProvider.createAccessToken(999L, "ghost", "USER");
992+
993+
// when & then
994+
mvc.perform(get("/api/users/me/bookmarks")
995+
.header("Authorization", "Bearer " + fakeToken))
996+
.andDo(print())
997+
.andExpect(status().isNotFound())
998+
.andExpect(jsonPath("$.code").value("USER_001"))
999+
.andExpect(jsonPath("$.message").value("존재하지 않는 사용자입니다."));
1000+
}
1001+
1002+
@Test
1003+
@DisplayName("탈퇴한 계정 → 410 Gone")
1004+
void getMyBookmarks_deletedUser() throws Exception {
1005+
// given
1006+
User user = User.createUser("deleted", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
1007+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
1008+
user.setUserStatus(UserStatus.DELETED);
1009+
userRepository.save(user);
1010+
1011+
String accessToken = generateAccessToken(user);
1012+
1013+
// when & then
1014+
mvc.perform(get("/api/users/me/bookmarks")
1015+
.header("Authorization", "Bearer " + accessToken))
1016+
.andDo(print())
1017+
.andExpect(status().isGone())
1018+
.andExpect(jsonPath("$.code").value("USER_009"))
1019+
.andExpect(jsonPath("$.message").value("탈퇴한 계정입니다."));
1020+
}
1021+
1022+
@Test
1023+
@DisplayName("정지된 계정 → 403 Forbidden")
1024+
void getMyBookmarks_suspendedUser() throws Exception {
1025+
// given
1026+
User user = User.createUser("suspended", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
1027+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
1028+
user.setUserStatus(UserStatus.SUSPENDED);
1029+
userRepository.save(user);
1030+
1031+
String accessToken = generateAccessToken(user);
1032+
1033+
// when & then
1034+
mvc.perform(get("/api/users/me/bookmarks")
1035+
.header("Authorization", "Bearer " + accessToken))
1036+
.andDo(print())
1037+
.andExpect(status().isForbidden())
1038+
.andExpect(jsonPath("$.code").value("USER_008"))
1039+
.andExpect(jsonPath("$.message").value("정지된 계정입니다. 관리자에게 문의하세요."));
1040+
}
1041+
1042+
@Test
1043+
@DisplayName("AccessToken 없음 → 401 Unauthorized")
1044+
void getMyBookmarks_noAccessToken() throws Exception {
1045+
// when & then
1046+
mvc.perform(get("/api/users/me/bookmarks"))
1047+
.andDo(print())
1048+
.andExpect(status().isUnauthorized())
1049+
.andExpect(jsonPath("$.code").value("AUTH_001"))
1050+
.andExpect(jsonPath("$.message").value("인증이 필요합니다."));
1051+
}
1052+
1053+
@Test
1054+
@DisplayName("잘못된 AccessToken → 401 Unauthorized")
1055+
void getMyBookmarks_invalidAccessToken() throws Exception {
1056+
// when & then
1057+
mvc.perform(get("/api/users/me/bookmarks")
1058+
.header("Authorization", "Bearer invalidToken"))
1059+
.andDo(print())
1060+
.andExpect(status().isUnauthorized())
1061+
.andExpect(jsonPath("$.code").value("AUTH_002"))
1062+
.andExpect(jsonPath("$.message").value("유효하지 않은 액세스 토큰입니다."));
1063+
}
1064+
1065+
@Test
1066+
@DisplayName("만료된 AccessToken → 401 Unauthorized")
1067+
void getMyBookmarks_expiredAccessToken() throws Exception {
1068+
// given
1069+
User user = User.createUser("expired", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
1070+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
1071+
user.setUserStatus(UserStatus.ACTIVE);
1072+
userRepository.save(user);
1073+
1074+
String expiredToken = testJwtTokenProvider.createExpiredAccessToken(user.getId(), user.getUsername(), user.getRole().name());
1075+
1076+
// when & then
1077+
mvc.perform(get("/api/users/me/bookmarks")
1078+
.header("Authorization", "Bearer " + expiredToken))
1079+
.andDo(print())
1080+
.andExpect(status().isUnauthorized())
1081+
.andExpect(jsonPath("$.code").value("AUTH_004"))
1082+
.andExpect(jsonPath("$.message").value("만료된 액세스 토큰입니다."));
1083+
}
9401084
}

src/test/java/com/back/domain/user/service/UserServiceTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.back.domain.board.common.dto.PageResponse;
77
import com.back.domain.board.post.dto.PostListResponse;
88
import com.back.domain.board.post.entity.Post;
9+
import com.back.domain.board.post.entity.PostBookmark;
10+
import com.back.domain.board.post.repository.PostBookmarkRepository;
911
import com.back.domain.board.post.repository.PostRepository;
1012
import com.back.domain.user.dto.ChangePasswordRequest;
1113
import com.back.domain.user.dto.UpdateUserProfileRequest;
@@ -48,6 +50,9 @@ class UserServiceTest {
4850
@Autowired
4951
private PostRepository postRepository;
5052

53+
@Autowired
54+
private PostBookmarkRepository postBookmarkRepository;
55+
5156
@Autowired
5257
private CommentRepository commentRepository;
5358

@@ -528,4 +533,69 @@ void getMyComments_suspendedUser() {
528533
.isInstanceOf(CustomException.class)
529534
.hasMessage(ErrorCode.USER_SUSPENDED.getMessage());
530535
}
536+
537+
// ====================== 내 북마크 게시글 목록 조회 테스트 ======================
538+
539+
@Test
540+
@DisplayName("내 북마크 게시글 목록 조회 성공")
541+
void getMyBookmarks_success() {
542+
// given
543+
User user = User.createUser("bookmarkUser", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
544+
user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0));
545+
user.setUserStatus(UserStatus.ACTIVE);
546+
userRepository.save(user);
547+
548+
Post post1 = new Post(user, "JPA 영속성 전이 완벽 정리", "내용1", null);
549+
Post post2 = new Post(user, "테스트 코드 작성 가이드", "내용2", null);
550+
postRepository.saveAll(List.of(post1, post2));
551+
552+
PostBookmark bookmark1 = new PostBookmark(post1, user);
553+
PostBookmark bookmark2 = new PostBookmark(post2, user);
554+
postBookmarkRepository.saveAll(List.of(bookmark1, bookmark2));
555+
556+
Pageable pageable = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "createdAt"));
557+
558+
// when
559+
PageResponse<PostListResponse> response = userService.getMyBookmarks(user.getId(), pageable);
560+
561+
// then
562+
assertThat(response.items()).hasSize(2);
563+
assertThat(response.items().get(0).getTitle()).isEqualTo("테스트 코드 작성 가이드"); // 최신순
564+
assertThat(response.items().get(1).getTitle()).isEqualTo("JPA 영속성 전이 완벽 정리");
565+
}
566+
567+
@Test
568+
@DisplayName("존재하지 않는 사용자 → USER_NOT_FOUND 예외 발생")
569+
void getMyBookmarks_userNotFound() {
570+
Pageable pageable = PageRequest.of(0, 10);
571+
assertThatThrownBy(() -> userService.getMyBookmarks(999L, pageable))
572+
.isInstanceOf(CustomException.class)
573+
.hasMessage(ErrorCode.USER_NOT_FOUND.getMessage());
574+
}
575+
576+
@Test
577+
@DisplayName("탈퇴된 사용자 → USER_DELETED 예외 발생")
578+
void getMyBookmarks_deletedUser() {
579+
User user = User.createUser("deleted", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
580+
user.setUserStatus(UserStatus.DELETED);
581+
userRepository.save(user);
582+
583+
Pageable pageable = PageRequest.of(0, 10);
584+
assertThatThrownBy(() -> userService.getMyBookmarks(user.getId(), pageable))
585+
.isInstanceOf(CustomException.class)
586+
.hasMessage(ErrorCode.USER_DELETED.getMessage());
587+
}
588+
589+
@Test
590+
@DisplayName("정지된 사용자 → USER_SUSPENDED 예외 발생")
591+
void getMyBookmarks_suspendedUser() {
592+
User user = User.createUser("suspended", "[email protected]", passwordEncoder.encode("P@ssw0rd!"));
593+
user.setUserStatus(UserStatus.SUSPENDED);
594+
userRepository.save(user);
595+
596+
Pageable pageable = PageRequest.of(0, 10);
597+
assertThatThrownBy(() -> userService.getMyBookmarks(user.getId(), pageable))
598+
.isInstanceOf(CustomException.class)
599+
.hasMessage(ErrorCode.USER_SUSPENDED.getMessage());
600+
}
531601
}

0 commit comments

Comments
 (0)