From: David ‘Bombe’ Roden Date: Sat, 7 Jan 2017 21:49:49 +0000 (+0100) Subject: Add unit test for delete post command and actually delete the post X-Git-Tag: 0.9.7^2~362 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=10d9eb5fcea8440b9ea06a3732678b286090934f;p=Sone.git Add unit test for delete post command and actually delete the post --- diff --git a/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java index e68cbb5..3cfb102 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/DeletePostCommand.java @@ -51,6 +51,7 @@ public class DeletePostCommand extends AbstractSoneCommand { if (!post.getSone().isLocal()) { return new ErrorResponse(401, "Not allowed."); } + getCore().deletePost(post); return new Response("PostDeleted", new SimpleFieldSetBuilder().get()); } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt new file mode 100644 index 0000000..4a23602 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/DeletePostCommandTest.kt @@ -0,0 +1,59 @@ +package net.pterodactylus.sone.fcp + +import com.google.common.base.Optional.of +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.freenet.fcp.FcpException +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test +import org.mockito.Mockito +import org.mockito.Mockito.verify + +/** + * Unit test for [DeletePostCommand]. + */ +class DeletePostCommandTest : SoneCommandTest() { + + private val postFromRemoteSone = mock().apply { whenever(sone).thenReturn(remoteSone) } + private val postFromLocalSone = mock().apply { whenever(sone).thenReturn(localSone) } + override fun createCommand(core: Core) = DeletePostCommand(core) + + @Test + fun `command requires write access`() { + assertThat(command.requiresWriteAccess(), equalTo(true)) + } + + @Test + fun `request without any parameter results in fcp exception`() { + requestWithoutAnyParameterResultsInFcpException() + } + + @Test + fun `request with invalid post parameter results in fcp exception`() { + parameters.putSingle("Post", "InvalidPostId") + expectedException.expect(FcpException::class.java) + command.execute(parameters, null, null) + } + + @Test + fun `request with post from remote sone returns error response`() { + parameters.putSingle("Post", "RemotePostId") + whenever(core.getPost("RemotePostId")).thenReturn(of(postFromRemoteSone)) + val response = command.execute(parameters, null, null) + assertThat(response.replyParameters["Message"], equalTo("Error")) + assertThat(response.replyParameters["ErrorCode"], equalTo("401")) + } + + @Test + fun `request with post from local sone deletes posts`() { + parameters.putSingle("Post", "LocalPostId") + whenever(core.getPost("LocalPostId")).thenReturn(of(postFromLocalSone)) + val response = command.execute(parameters, null, null) + assertThat(response.replyParameters["Message"], equalTo("PostDeleted")) + verify(core).deletePost(postFromLocalSone) + } + +}