#24 simple sns: 포스트 삭제 테스트코드
This commit is contained in:
@@ -63,4 +63,31 @@ public class PostService {
|
||||
return Post.fromEntity(postEntityRepository.saveAndFlush(postEntity));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(String username, Integer postId) {
|
||||
UserEntity userEntity = userEntityRepository.findByUsername(username)
|
||||
.orElseThrow(
|
||||
() -> new SnsApplicationException(
|
||||
USER_NOT_FOUND,
|
||||
String.format("%s not founded", username)
|
||||
)
|
||||
);
|
||||
|
||||
PostEntity postEntity = postEntityRepository.findById(postId)
|
||||
.orElseThrow(
|
||||
() -> new SnsApplicationException(
|
||||
POST_NOT_FOUND,
|
||||
String.format("%s not founded", postId)
|
||||
)
|
||||
);
|
||||
|
||||
if (postEntity.getUser() != userEntity) {
|
||||
throw new SnsApplicationException(
|
||||
INVALID_PERMISSION,
|
||||
String.format("%s has no permission with %s", username, postId)
|
||||
);
|
||||
}
|
||||
|
||||
postEntityRepository.delete(postEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@@ -132,4 +131,50 @@ public class PostControllerTest {
|
||||
).andDo(print())
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 포스트삭제() throws Exception {
|
||||
|
||||
mockMvc.perform(delete("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
).andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
void 포스트삭제시_로그인하지_않은경우() throws Exception {
|
||||
|
||||
mockMvc.perform(delete("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
).andDo(print())
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 포스트삭제시_작성자와_삭제요청자가_다를경우() throws Exception {
|
||||
|
||||
doThrow(new SnsApplicationException(ErrorCode.INVALID_PERMISSION))
|
||||
.when(postService).delete(any(), any());
|
||||
|
||||
mockMvc.perform(delete("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
).andDo(print())
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
void 포스트삭제시_삭제하려는_포스트가_존재하지_않을경우() throws Exception {
|
||||
|
||||
doThrow(new SnsApplicationException(ErrorCode.POST_NOT_FOUND))
|
||||
.when(postService).delete(any(), any());
|
||||
|
||||
mockMvc.perform(delete("/api/v1/posts/1")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
).andDo(print())
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,4 +116,52 @@ public class PostServiceTest {
|
||||
assertEquals(ErrorCode.INVALID_PERMISSION, e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 포스트삭제가_성공한경우() {
|
||||
String username = "username";
|
||||
Integer postId = 1;
|
||||
|
||||
PostEntity postEntity = PostEntityFixture.get(username, postId, 1);
|
||||
UserEntity userEntity = postEntity.getUser();
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(userEntity));
|
||||
when(postEntityRepository.findById(postId)).thenReturn(Optional.of(postEntity));
|
||||
|
||||
assertDoesNotThrow(() -> postService.delete(username, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 포스트삭제시_포스트가_존재하지않는_경우() {
|
||||
String username = "username";
|
||||
Integer postId = 1;
|
||||
|
||||
PostEntity postEntity = PostEntityFixture.get(username, postId, 1);
|
||||
UserEntity userEntity = postEntity.getUser();
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(userEntity));
|
||||
when(postEntityRepository.findById(postId)).thenReturn(Optional.empty());
|
||||
|
||||
SnsApplicationException e =
|
||||
assertThrows(SnsApplicationException.class, () -> postService.delete(username, 1));
|
||||
|
||||
assertEquals(ErrorCode.POST_NOT_FOUND, e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 포스트삭제시_권한이_없는_경우() {
|
||||
String username = "username";
|
||||
Integer postId = 1;
|
||||
|
||||
PostEntity postEntity = PostEntityFixture.get(username, postId, 1);
|
||||
UserEntity writer = UserEntityFixture.get("username1", "password", 2);
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(writer));
|
||||
when(postEntityRepository.findById(postId)).thenReturn(Optional.of(postEntity));
|
||||
|
||||
SnsApplicationException e =
|
||||
assertThrows(SnsApplicationException.class, () -> postService.delete(username, 1));
|
||||
|
||||
assertEquals(ErrorCode.INVALID_PERMISSION, e.getErrorCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user