|
| 1 | +package com.back.domain.board.controller; |
| 2 | + |
| 3 | +import com.back.domain.board.post.entity.Post; |
| 4 | +import com.back.domain.user.entity.User; |
| 5 | +import com.back.domain.user.entity.UserProfile; |
| 6 | +import com.back.domain.user.entity.UserStatus; |
| 7 | +import com.back.domain.user.repository.UserRepository; |
| 8 | +import com.back.domain.board.post.repository.PostRepository; |
| 9 | +import com.back.fixture.TestJwtTokenProvider; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.http.MediaType; |
| 17 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 18 | +import org.springframework.test.context.ActiveProfiles; |
| 19 | +import org.springframework.test.web.servlet.MockMvc; |
| 20 | +import org.springframework.transaction.annotation.Transactional; |
| 21 | + |
| 22 | +import java.time.LocalDate; |
| 23 | + |
| 24 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 25 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 27 | + |
| 28 | +@SpringBootTest |
| 29 | +@AutoConfigureMockMvc |
| 30 | +@ActiveProfiles("test") |
| 31 | +@Transactional |
| 32 | +class PostBookmarkControllerTest { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private MockMvc mvc; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private UserRepository userRepository; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private PostRepository postRepository; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + private PasswordEncoder passwordEncoder; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private TestJwtTokenProvider testJwtTokenProvider; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private ObjectMapper objectMapper; |
| 51 | + |
| 52 | + private String generateAccessToken(User user) { |
| 53 | + return testJwtTokenProvider.createAccessToken(user.getId(), user.getUsername(), user.getRole().name()); |
| 54 | + } |
| 55 | + |
| 56 | + // ====================== 게시글 북마크 등록 ====================== |
| 57 | + |
| 58 | + @Test |
| 59 | + @DisplayName("게시글 북마크 등록 성공 → 200 OK") |
| 60 | + void bookmarkPost_success() throws Exception { |
| 61 | + User user = User. createUser( "bookmarkUser", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 62 | + user.setUserProfile(new UserProfile(user, "홍길동", null, "소개글", LocalDate.of(2000, 1, 1), 0)); |
| 63 | + user.setUserStatus(UserStatus.ACTIVE); |
| 64 | + userRepository.save(user); |
| 65 | + |
| 66 | + Post post = new Post(user, "북마크 테스트", "내용입니다"); |
| 67 | + postRepository.save(post); |
| 68 | + |
| 69 | + String accessToken = generateAccessToken(user); |
| 70 | + |
| 71 | + mvc.perform(post("/api/posts/{postId}/bookmark", post.getId()) |
| 72 | + .header("Authorization", "Bearer " + accessToken) |
| 73 | + .contentType(MediaType.APPLICATION_JSON)) |
| 74 | + .andDo(print()) |
| 75 | + .andExpect(status().isOk()) |
| 76 | + .andExpect(jsonPath("$.success").value(true)) |
| 77 | + .andExpect(jsonPath("$.code").value("SUCCESS_200")) |
| 78 | + .andExpect(jsonPath("$.message").value("게시글 북마크가 등록되었습니다.")) |
| 79 | + .andExpect(jsonPath("$.data.postId").value(post.getId())) |
| 80 | + .andExpect(jsonPath("$.data.bookmarkCount").value(1)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("게시글 북마크 실패 - 존재하지 않는 사용자 → 404 Not Found") |
| 85 | + void bookmarkPost_userNotFound() throws Exception { |
| 86 | + String fakeToken = testJwtTokenProvider.createAccessToken(999L, "ghost", "USER"); |
| 87 | + |
| 88 | + User writer = User. createUser( "writer", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 89 | + writer.setUserProfile(new UserProfile(writer, "작성자", null, null, null, 0)); |
| 90 | + writer.setUserStatus(UserStatus.ACTIVE); |
| 91 | + userRepository.save(writer); |
| 92 | + |
| 93 | + Post post = new Post(writer, "게시글", "내용"); |
| 94 | + postRepository.save(post); |
| 95 | + |
| 96 | + mvc.perform(post("/api/posts/{postId}/bookmark", post.getId()) |
| 97 | + .header("Authorization", "Bearer " + fakeToken) |
| 98 | + .contentType(MediaType.APPLICATION_JSON)) |
| 99 | + .andDo(print()) |
| 100 | + .andExpect(status().isNotFound()) |
| 101 | + .andExpect(jsonPath("$.code").value("USER_001")) |
| 102 | + .andExpect(jsonPath("$.message").value("존재하지 않는 사용자입니다.")); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + @DisplayName("게시글 북마크 실패 - 존재하지 않는 게시글 → 404 Not Found") |
| 107 | + void bookmarkPost_postNotFound() throws Exception { |
| 108 | + User user = User. createUser( "bookmarkUser2", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 109 | + user.setUserProfile(new UserProfile(user, "작성자", null, null, null, 0)); |
| 110 | + user.setUserStatus(UserStatus.ACTIVE); |
| 111 | + userRepository.save(user); |
| 112 | + String accessToken = generateAccessToken(user); |
| 113 | + |
| 114 | + mvc.perform(post("/api/posts/{postId}/bookmark", 999L) |
| 115 | + .header("Authorization", "Bearer " + accessToken) |
| 116 | + .contentType(MediaType.APPLICATION_JSON)) |
| 117 | + .andDo(print()) |
| 118 | + .andExpect(status().isNotFound()) |
| 119 | + .andExpect(jsonPath("$.code").value("POST_001")) |
| 120 | + .andExpect(jsonPath("$.message").value("존재하지 않는 게시글입니다.")); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @DisplayName("게시글 북마크 실패 - 이미 북마크 등록 → 409 Conflict") |
| 125 | + void bookmarkPost_alreadyBookmarked() throws Exception { |
| 126 | + User user = User. createUser( "bookmarkUser3", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 127 | + user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0)); |
| 128 | + user.setUserStatus(UserStatus.ACTIVE); |
| 129 | + userRepository.save(user); |
| 130 | + |
| 131 | + Post post = new Post(user, "테스트 게시글", "내용"); |
| 132 | + postRepository.save(post); |
| 133 | + |
| 134 | + String accessToken = generateAccessToken(user); |
| 135 | + |
| 136 | + mvc.perform(post("/api/posts/{postId}/bookmark", post.getId()) |
| 137 | + .header("Authorization", "Bearer " + accessToken)) |
| 138 | + .andExpect(status().isOk()); |
| 139 | + |
| 140 | + mvc.perform(post("/api/posts/{postId}/bookmark", post.getId()) |
| 141 | + .header("Authorization", "Bearer " + accessToken)) |
| 142 | + .andDo(print()) |
| 143 | + .andExpect(status().isConflict()) |
| 144 | + .andExpect(jsonPath("$.code").value("POST_007")) |
| 145 | + .andExpect(jsonPath("$.message").value("이미 북마크한 게시글입니다.")); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + @DisplayName("게시글 북마크 실패 - 토큰 없음 → 401 Unauthorized") |
| 150 | + void bookmarkPost_noToken() throws Exception { |
| 151 | + User writer = User. createUser( "writer", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 152 | + writer.setUserProfile(new UserProfile(writer, "작성자", null, null, null, 0)); |
| 153 | + writer.setUserStatus(UserStatus.ACTIVE); |
| 154 | + userRepository.save(writer); |
| 155 | + |
| 156 | + Post post = new Post(writer, "테스트", "내용"); |
| 157 | + postRepository.save(post); |
| 158 | + |
| 159 | + mvc.perform(post("/api/posts/{postId}/bookmark", post.getId()) |
| 160 | + .contentType(MediaType.APPLICATION_JSON)) |
| 161 | + .andDo(print()) |
| 162 | + .andExpect(status().isUnauthorized()) |
| 163 | + .andExpect(jsonPath("$.code").value("AUTH_001")) |
| 164 | + .andExpect(jsonPath("$.message").value("인증이 필요합니다.")); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + @DisplayName("게시글 북마크 실패 - 잘못된 요청 파라미터 → 400 Bad Request") |
| 169 | + void bookmarkPost_badRequest() throws Exception { |
| 170 | + User user = User. createUser( "bookmarkUser", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 171 | + user.setUserProfile(new UserProfile(user, "홍길동", null, "소개글", LocalDate.of(2000, 1, 1), 0)); |
| 172 | + user.setUserStatus(UserStatus.ACTIVE); |
| 173 | + userRepository.save(user); |
| 174 | + |
| 175 | + String accessToken = generateAccessToken(user); |
| 176 | + |
| 177 | + mvc.perform(post("/api/posts/{postId}/bookmark", "invalid") // invalid param |
| 178 | + .header("Authorization", "Bearer " + accessToken) |
| 179 | + .contentType(MediaType.APPLICATION_JSON)) |
| 180 | + .andDo(print()) |
| 181 | + .andExpect(status().isBadRequest()) |
| 182 | + .andExpect(jsonPath("$.code").value("COMMON_400")) |
| 183 | + .andExpect(jsonPath("$.message").value("잘못된 요청입니다.")); |
| 184 | + } |
| 185 | + |
| 186 | + // ====================== 게시글 북마크 취소 ====================== |
| 187 | + |
| 188 | + @Test |
| 189 | + @DisplayName("게시글 북마크 취소 성공 → 200 OK") |
| 190 | + void cancelBookmarkPost_success() throws Exception { |
| 191 | + User user = User. createUser( "cancelUser", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 192 | + user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0)); |
| 193 | + user.setUserStatus(UserStatus.ACTIVE); |
| 194 | + userRepository.save(user); |
| 195 | + |
| 196 | + Post post = new Post(user, "북마크 취소 테스트", "내용"); |
| 197 | + postRepository.save(post); |
| 198 | + String accessToken = generateAccessToken(user); |
| 199 | + |
| 200 | + mvc.perform(post("/api/posts/{postId}/bookmark", post.getId()) |
| 201 | + .header("Authorization", "Bearer " + accessToken)) |
| 202 | + .andExpect(status().isOk()); |
| 203 | + |
| 204 | + mvc.perform(delete("/api/posts/{postId}/bookmark", post.getId()) |
| 205 | + .header("Authorization", "Bearer " + accessToken)) |
| 206 | + .andDo(print()) |
| 207 | + .andExpect(status().isOk()) |
| 208 | + .andExpect(jsonPath("$.success").value(true)) |
| 209 | + .andExpect(jsonPath("$.code").value("SUCCESS_200")) |
| 210 | + .andExpect(jsonPath("$.message").value("게시글 북마크가 취소되었습니다.")) |
| 211 | + .andExpect(jsonPath("$.data.postId").value(post.getId())) |
| 212 | + .andExpect(jsonPath("$.data.bookmarkCount").value(0)); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + @DisplayName("게시글 북마크 취소 실패 - 존재하지 않는 사용자 → 404 Not Found") |
| 217 | + void cancelBookmarkPost_userNotFound() throws Exception { |
| 218 | + String fakeToken = testJwtTokenProvider.createAccessToken(999L, "ghost", "USER"); |
| 219 | + |
| 220 | + User writer = User. createUser( "writer", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 221 | + writer.setUserProfile(new UserProfile(writer, "작성자", null, null, null, 0)); |
| 222 | + writer.setUserStatus(UserStatus.ACTIVE); |
| 223 | + userRepository.save(writer); |
| 224 | + |
| 225 | + Post post = new Post(writer, "게시글", "내용"); |
| 226 | + postRepository.save(post); |
| 227 | + |
| 228 | + mvc.perform(delete("/api/posts/{postId}/bookmark", post.getId()) |
| 229 | + .header("Authorization", "Bearer " + fakeToken) |
| 230 | + .contentType(MediaType.APPLICATION_JSON)) |
| 231 | + .andDo(print()) |
| 232 | + .andExpect(status().isNotFound()) |
| 233 | + .andExpect(jsonPath("$.code").value("USER_001")) |
| 234 | + .andExpect(jsonPath("$.message").value("존재하지 않는 사용자입니다.")); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + @DisplayName("게시글 북마크 취소 실패 - 존재하지 않는 게시글 → 404 Not Found") |
| 239 | + void cancelBookmarkPost_postNotFound() throws Exception { |
| 240 | + User user = User. createUser( "cancelUser2", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 241 | + user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0)); |
| 242 | + user.setUserStatus(UserStatus.ACTIVE); |
| 243 | + userRepository.save(user); |
| 244 | + String accessToken = generateAccessToken(user); |
| 245 | + |
| 246 | + mvc.perform(delete("/api/posts/{postId}/bookmark", 999L) |
| 247 | + .header("Authorization", "Bearer " + accessToken)) |
| 248 | + .andDo(print()) |
| 249 | + .andExpect(status().isNotFound()) |
| 250 | + .andExpect(jsonPath("$.code").value("POST_001")) |
| 251 | + .andExpect(jsonPath("$.message").value("존재하지 않는 게시글입니다.")); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + @DisplayName("게시글 북마크 취소 실패 - 북마크 내역 없음 → 404 Not Found") |
| 256 | + void cancelBookmarkPost_notFound() throws Exception { |
| 257 | + User user = User. createUser( "cancelUser3", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 258 | + user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0)); |
| 259 | + user.setUserStatus(UserStatus.ACTIVE); |
| 260 | + userRepository.save(user); |
| 261 | + |
| 262 | + Post post = new Post(user, "테스트", "내용"); |
| 263 | + postRepository.save(post); |
| 264 | + String accessToken = generateAccessToken(user); |
| 265 | + |
| 266 | + mvc.perform(delete("/api/posts/{postId}/bookmark", post.getId()) |
| 267 | + .header("Authorization", "Bearer " + accessToken)) |
| 268 | + .andDo(print()) |
| 269 | + .andExpect(status().isNotFound()) |
| 270 | + .andExpect(jsonPath("$.code").value("POST_008")) |
| 271 | + .andExpect(jsonPath("$.message").value("해당 게시글에 대한 북마크 기록이 없습니다.")); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + @DisplayName("게시글 북마크 취소 실패 - 토큰 없음 → 401 Unauthorized") |
| 276 | + void cancelBookmarkPost_noToken() throws Exception { |
| 277 | + User writer = User. createUser( "writer", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 278 | + writer.setUserProfile(new UserProfile(writer, "작성자", null, null, null, 0)); |
| 279 | + writer.setUserStatus(UserStatus.ACTIVE); |
| 280 | + userRepository.save(writer); |
| 281 | + |
| 282 | + Post post = new Post(writer, "테스트", "내용"); |
| 283 | + postRepository.save(post); |
| 284 | + |
| 285 | + mvc.perform(delete("/api/posts/{postId}/bookmark", post.getId()) |
| 286 | + .contentType(MediaType.APPLICATION_JSON)) |
| 287 | + .andDo(print()) |
| 288 | + .andExpect(status().isUnauthorized()) |
| 289 | + .andExpect(jsonPath("$.code").value("AUTH_001")) |
| 290 | + .andExpect(jsonPath("$.message").value("인증이 필요합니다.")); |
| 291 | + } |
| 292 | + |
| 293 | + @Test |
| 294 | + @DisplayName("게시글 북마크 취소 실패 - 잘못된 요청 파라미터 → 400 Bad Request") |
| 295 | + void cancelBookmarkPost_badRequest() throws Exception { |
| 296 | + User user = User. createUser( "bookmarkUser", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 297 | + user.setUserProfile(new UserProfile(user, "홍길동", null, "소개글", LocalDate.of(2000, 1, 1), 0)); |
| 298 | + user.setUserStatus(UserStatus.ACTIVE); |
| 299 | + userRepository.save(user); |
| 300 | + |
| 301 | + String accessToken = generateAccessToken(user); |
| 302 | + |
| 303 | + mvc.perform(delete("/api/posts/{postId}/bookmark", "invalid") // invalid param |
| 304 | + .header("Authorization", "Bearer " + accessToken)) |
| 305 | + .andDo(print()) |
| 306 | + .andExpect(status().isBadRequest()) |
| 307 | + .andExpect(jsonPath("$.code").value("COMMON_400")) |
| 308 | + .andExpect(jsonPath("$.message").value("잘못된 요청입니다.")); |
| 309 | + } |
| 310 | +} |
0 commit comments