|
3 | 3 | import com.back.domain.board.comment.entity.Comment; |
4 | 4 | import com.back.domain.board.comment.repository.CommentRepository; |
5 | 5 | 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; |
6 | 8 | import com.back.domain.board.post.repository.PostRepository; |
7 | 9 | import com.back.domain.user.dto.ChangePasswordRequest; |
8 | 10 | import com.back.domain.user.dto.UpdateUserProfileRequest; |
@@ -49,6 +51,9 @@ class UserControllerTest { |
49 | 51 | @Autowired |
50 | 52 | private PostRepository postRepository; |
51 | 53 |
|
| 54 | + @Autowired |
| 55 | + private PostBookmarkRepository postBookmarkRepository; |
| 56 | + |
52 | 57 | @Autowired |
53 | 58 | private CommentRepository commentRepository; |
54 | 59 |
|
@@ -937,4 +942,143 @@ void getMyComments_expiredAccessToken() throws Exception { |
937 | 942 | .andExpect(jsonPath("$.code").value("AUTH_004")) |
938 | 943 | .andExpect(jsonPath("$.message").value("만료된 액세스 토큰입니다.")); |
939 | 944 | } |
| 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 | + } |
940 | 1084 | } |
0 commit comments