Allow deletion of initial post

This commit is contained in:
Andreas Svanberg 2025-03-10 14:02:34 +01:00
parent 2ac30fa980
commit 6834ebaac1
2 changed files with 36 additions and 8 deletions
core/src
main/java/se/su/dsv/scipro/forum
test/java/se/su/dsv/scipro/forum

@ -158,13 +158,13 @@ public class BasicForumServiceImpl implements BasicForumService {
@Override
public boolean canDelete(ForumPost forumPost) {
ForumPost initialPost = forumPost.getForumThread().getPosts().get(0);
if (forumPost.equals(initialPost)) {
// The initial post in a thread can never be deleted
return false;
User user = currentUserProvider.get();
if (isInitialPost(forumPost)) {
// can delete original if no replies
boolean hasNoReplies = forumPost.getForumThread().getPosts().size() == 1;
return hasNoReplies && Objects.equals(forumPost.getPostedBy(), user);
}
User user = currentUserProvider.get();
// Current user can be null meaning the call came from the system
if (user == null) {
// Allow the system to delete any post
@ -173,6 +173,11 @@ public class BasicForumServiceImpl implements BasicForumService {
return Objects.equals(forumPost.getPostedBy(), user);
}
private static boolean isInitialPost(ForumPost forumPost) {
ForumPost initialPost = forumPost.getForumThread().getPosts().get(0);
return forumPost.equals(initialPost);
}
@Override
@Transactional
public void deletePost(ForumPost post) {

@ -32,14 +32,14 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
}
@Test
public void can_not_delete_original_post() {
public void can_delete_entire_thread_if_no_replies() {
ForumThread thread = basicForumService.createThread("Test thread");
ForumPost originalPost = basicForumService.createReply(thread, op, "Test post", Set.of());
setLoggedInAs(op);
assertFalse(basicForumService.canDelete(originalPost));
assertThrows(IllegalArgumentException.class, () -> basicForumService.deletePost(originalPost));
assertTrue(basicForumService.canDelete(originalPost));
assertDoesNotThrow(() -> basicForumService.deletePost(originalPost));
}
@Test
@ -80,4 +80,27 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
assertTrue(basicForumService.canDelete(secondReply));
assertDoesNotThrow(() -> basicForumService.deletePost(secondReply));
}
@Test
public void can_not_delete_original_post_with_replies() {
ForumThread thread = basicForumService.createThread("Test thread");
ForumPost originalPost = basicForumService.createReply(thread, op, "Test post", Set.of());
ForumPost reply = basicForumService.createReply(thread, commenter, "Test reply", Set.of());
setLoggedInAs(op);
assertFalse(basicForumService.canDelete(originalPost));
assertThrows(IllegalArgumentException.class, () -> basicForumService.deletePost(originalPost));
}
@Test
public void can_not_delete_someone_elses_original_post() {
ForumThread thread = basicForumService.createThread("Test thread");
ForumPost originalPost = basicForumService.createReply(thread, op, "Test post", Set.of());
setLoggedInAs(commenter);
assertFalse(basicForumService.canDelete(originalPost));
assertThrows(IllegalArgumentException.class, () -> basicForumService.deletePost(originalPost));
}
}