|
| 1 | +package com.back.domain.board.controller; |
| 2 | + |
| 3 | +import com.back.domain.board.dto.CommentRequest; |
| 4 | +import com.back.domain.board.entity.Post; |
| 5 | +import com.back.domain.board.repository.PostRepository; |
| 6 | +import com.back.domain.user.entity.User; |
| 7 | +import com.back.domain.user.entity.UserProfile; |
| 8 | +import com.back.domain.user.entity.UserStatus; |
| 9 | +import com.back.domain.user.repository.UserRepository; |
| 10 | +import com.back.fixture.TestJwtTokenProvider; |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.http.MediaType; |
| 18 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 19 | +import org.springframework.test.context.ActiveProfiles; |
| 20 | +import org.springframework.test.web.servlet.MockMvc; |
| 21 | +import org.springframework.test.web.servlet.ResultActions; |
| 22 | +import org.springframework.transaction.annotation.Transactional; |
| 23 | + |
| 24 | +import java.time.LocalDate; |
| 25 | + |
| 26 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 27 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 28 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 29 | + |
| 30 | +@SpringBootTest |
| 31 | +@AutoConfigureMockMvc |
| 32 | +@ActiveProfiles("test") |
| 33 | +@Transactional |
| 34 | +class CommentControllerTest { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private MockMvc mvc; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private UserRepository userRepository; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private PostRepository postRepository; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private TestJwtTokenProvider testJwtTokenProvider; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private PasswordEncoder passwordEncoder; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private ObjectMapper objectMapper; |
| 53 | + |
| 54 | + private String generateAccessToken(User user) { |
| 55 | + return testJwtTokenProvider.createAccessToken( |
| 56 | + user.getId(), |
| 57 | + user.getUsername(), |
| 58 | + user.getRole().name() |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + // ====================== 댓글 생성 테스트 ====================== |
| 63 | + |
| 64 | + @Test |
| 65 | + @DisplayName("댓글 생성 성공 → 201 Created") |
| 66 | + void createComment_success() throws Exception { |
| 67 | + // given: 정상 유저 + 게시글 |
| 68 | + User user = User. createUser( "writer", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 69 | + user.setUserProfile(new UserProfile(user, "홍길동", null, "소개글", LocalDate.of(2000, 1, 1), 1000)); |
| 70 | + user.setUserStatus(UserStatus.ACTIVE); |
| 71 | + userRepository.save(user); |
| 72 | + |
| 73 | + Post post = new Post(user, "첫 글", "내용"); |
| 74 | + postRepository.save(post); |
| 75 | + |
| 76 | + String accessToken = generateAccessToken(user); |
| 77 | + |
| 78 | + CommentRequest request = new CommentRequest("좋은 글 감사합니다!"); |
| 79 | + |
| 80 | + // when |
| 81 | + ResultActions resultActions = mvc.perform( |
| 82 | + post("/api/posts/{postId}/comments", post.getId()) |
| 83 | + .header("Authorization", "Bearer " + accessToken) |
| 84 | + .contentType(MediaType.APPLICATION_JSON) |
| 85 | + .content(objectMapper.writeValueAsString(request)) |
| 86 | + ).andDo(print()); |
| 87 | + |
| 88 | + // then |
| 89 | + resultActions |
| 90 | + .andExpect(status().isCreated()) |
| 91 | + .andExpect(jsonPath("$.success").value(true)) |
| 92 | + .andExpect(jsonPath("$.code").value("SUCCESS_200")) |
| 93 | + .andExpect(jsonPath("$.data.content").value("좋은 글 감사합니다!")) |
| 94 | + .andExpect(jsonPath("$.data.author.nickname").value("홍길동")) |
| 95 | + .andExpect(jsonPath("$.data.postId").value(post.getId())); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @DisplayName("댓글 생성 실패 - 존재하지 않는 사용자 → 404 Not Found") |
| 100 | + void createComment_userNotFound() throws Exception { |
| 101 | + // given: 게시글 저장 |
| 102 | + User user = User. createUser( "temp", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 103 | + user.setUserProfile(new UserProfile(user, "임시", null, null, null, 0)); |
| 104 | + user.setUserStatus(UserStatus.ACTIVE); |
| 105 | + userRepository.save(user); |
| 106 | + |
| 107 | + Post post = new Post(user, "제목", "내용"); |
| 108 | + postRepository.save(post); |
| 109 | + |
| 110 | + // DB에 없는 userId 기반 토큰 |
| 111 | + String fakeToken = testJwtTokenProvider.createAccessToken(999L, "ghost", "USER"); |
| 112 | + |
| 113 | + CommentRequest request = new CommentRequest("댓글 내용"); |
| 114 | + |
| 115 | + // when & then |
| 116 | + mvc.perform(post("/api/posts/{postId}/comments", post.getId()) |
| 117 | + .header("Authorization", "Bearer " + fakeToken) |
| 118 | + .contentType(MediaType.APPLICATION_JSON) |
| 119 | + .content(objectMapper.writeValueAsString(request))) |
| 120 | + .andDo(print()) |
| 121 | + .andExpect(status().isNotFound()) |
| 122 | + .andExpect(jsonPath("$.code").value("USER_001")) |
| 123 | + .andExpect(jsonPath("$.message").value("존재하지 않는 사용자입니다.")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + @DisplayName("댓글 생성 실패 - 존재하지 않는 게시글 → 404 Not Found") |
| 128 | + void createComment_postNotFound() throws Exception { |
| 129 | + // given: 정상 유저 |
| 130 | + User user = User. createUser( "writer2", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 131 | + user.setUserProfile(new UserProfile(user, "작성자", null, null, null, 0)); |
| 132 | + user.setUserStatus(UserStatus.ACTIVE); |
| 133 | + userRepository.save(user); |
| 134 | + |
| 135 | + String accessToken = generateAccessToken(user); |
| 136 | + |
| 137 | + CommentRequest request = new CommentRequest("댓글 내용"); |
| 138 | + |
| 139 | + // when & then |
| 140 | + mvc.perform(post("/api/posts/{postId}/comments", 999L) |
| 141 | + .header("Authorization", "Bearer " + accessToken) |
| 142 | + .contentType(MediaType.APPLICATION_JSON) |
| 143 | + .content(objectMapper.writeValueAsString(request))) |
| 144 | + .andDo(print()) |
| 145 | + .andExpect(status().isNotFound()) |
| 146 | + .andExpect(jsonPath("$.code").value("POST_001")) |
| 147 | + .andExpect(jsonPath("$.message").value("존재하지 않는 게시글입니다.")); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + @DisplayName("댓글 생성 실패 - 잘못된 요청(필드 누락) → 400 Bad Request") |
| 152 | + void createComment_badRequest() throws Exception { |
| 153 | + // given: 정상 유저 + 게시글 |
| 154 | + User user = User. createUser( "writer", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 155 | + user.setUserProfile(new UserProfile(user, "홍길동", null, null, null, 0)); |
| 156 | + user.setUserStatus(UserStatus.ACTIVE); |
| 157 | + userRepository.save(user); |
| 158 | + |
| 159 | + Post post = new Post(user, "제목", "내용"); |
| 160 | + postRepository.save(post); |
| 161 | + |
| 162 | + String accessToken = generateAccessToken(user); |
| 163 | + |
| 164 | + // content 누락 |
| 165 | + String invalidJson = """ |
| 166 | + { |
| 167 | + } |
| 168 | + """; |
| 169 | + |
| 170 | + // when & then |
| 171 | + mvc.perform(post("/api/posts/{postId}/comments", post.getId()) |
| 172 | + .header("Authorization", "Bearer " + accessToken) |
| 173 | + .contentType(MediaType.APPLICATION_JSON) |
| 174 | + .content(invalidJson)) |
| 175 | + .andDo(print()) |
| 176 | + .andExpect(status().isBadRequest()) |
| 177 | + .andExpect(jsonPath("$.code").value("COMMON_400")) |
| 178 | + .andExpect(jsonPath("$.message").value("잘못된 요청입니다.")); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + @DisplayName("댓글 생성 실패 - 토큰 없음 → 401 Unauthorized") |
| 183 | + void createComment_noToken() throws Exception { |
| 184 | + // given: 게시글 |
| 185 | + User user = User. createUser( "writer", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 186 | + user.setUserProfile(new UserProfile(user, "작성자", null, null, null, 0)); |
| 187 | + user.setUserStatus(UserStatus.ACTIVE); |
| 188 | + userRepository.save(user); |
| 189 | + |
| 190 | + Post post = new Post(user, "제목", "내용"); |
| 191 | + postRepository.save(post); |
| 192 | + |
| 193 | + CommentRequest request = new CommentRequest("댓글 내용"); |
| 194 | + |
| 195 | + // when & then |
| 196 | + mvc.perform(post("/api/posts/{postId}/comments", post.getId()) |
| 197 | + .contentType(MediaType.APPLICATION_JSON) |
| 198 | + .content(objectMapper.writeValueAsString(request))) |
| 199 | + .andDo(print()) |
| 200 | + .andExpect(status().isUnauthorized()) |
| 201 | + .andExpect(jsonPath("$.code").value("AUTH_001")) |
| 202 | + .andExpect(jsonPath("$.message").value("인증이 필요합니다.")); |
| 203 | + } |
| 204 | +} |
0 commit comments