Allow deletion of initial post
This commit is contained in:
parent
2ac30fa980
commit
6834ebaac1
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
|
@Override
|
||||||
public boolean canDelete(ForumPost forumPost) {
|
public boolean canDelete(ForumPost forumPost) {
|
||||||
ForumPost initialPost = forumPost.getForumThread().getPosts().get(0);
|
User user = currentUserProvider.get();
|
||||||
if (forumPost.equals(initialPost)) {
|
if (isInitialPost(forumPost)) {
|
||||||
// The initial post in a thread can never be deleted
|
// can delete original if no replies
|
||||||
return false;
|
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
|
// Current user can be null meaning the call came from the system
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
// Allow the system to delete any post
|
// Allow the system to delete any post
|
||||||
@ -173,6 +173,11 @@ public class BasicForumServiceImpl implements BasicForumService {
|
|||||||
return Objects.equals(forumPost.getPostedBy(), user);
|
return Objects.equals(forumPost.getPostedBy(), user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isInitialPost(ForumPost forumPost) {
|
||||||
|
ForumPost initialPost = forumPost.getForumThread().getPosts().get(0);
|
||||||
|
return forumPost.equals(initialPost);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deletePost(ForumPost post) {
|
public void deletePost(ForumPost post) {
|
||||||
|
@ -32,14 +32,14 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void can_not_delete_original_post() {
|
public void can_delete_entire_thread_if_no_replies() {
|
||||||
ForumThread thread = basicForumService.createThread("Test thread");
|
ForumThread thread = basicForumService.createThread("Test thread");
|
||||||
ForumPost originalPost = basicForumService.createReply(thread, op, "Test post", Set.of());
|
ForumPost originalPost = basicForumService.createReply(thread, op, "Test post", Set.of());
|
||||||
|
|
||||||
setLoggedInAs(op);
|
setLoggedInAs(op);
|
||||||
|
|
||||||
assertFalse(basicForumService.canDelete(originalPost));
|
assertTrue(basicForumService.canDelete(originalPost));
|
||||||
assertThrows(IllegalArgumentException.class, () -> basicForumService.deletePost(originalPost));
|
assertDoesNotThrow(() -> basicForumService.deletePost(originalPost));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -80,4 +80,27 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
|
|||||||
assertTrue(basicForumService.canDelete(secondReply));
|
assertTrue(basicForumService.canDelete(secondReply));
|
||||||
assertDoesNotThrow(() -> basicForumService.deletePost(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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user