From: David ‘Bombe’ Roden Date: Wed, 19 Feb 2014 20:51:02 +0000 (+0100) Subject: Add unit test for DeletePostAjaxPage. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b7ca3f9535d49bbde4f21316190af4c7c0918dea;p=Sone.git Add unit test for DeletePostAjaxPage. --- diff --git a/src/test/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.java b/src/test/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.java new file mode 100644 index 0000000..912c1d5 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.java @@ -0,0 +1,56 @@ +package net.pterodactylus.sone.web.ajax; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import net.pterodactylus.sone.data.Mocks; +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.web.page.FreenetRequest; + +import org.junit.Test; + +/** + * Unit test for {@link DeletePostAjaxPage}. + * + * @author David ‘Bombe’ Roden + */ +public class DeletePostAjaxPageTest { + + private Mocks mocks = new Mocks(); + private final DeletePostAjaxPage deletePostAjaxPage = new DeletePostAjaxPage(mocks.webInterface); + private final FreenetRequest freenetRequest = mocks.mockRequest("deletePost.ajax"); + + @Test + public void canDeletePost() { + Sone sender = mocks.mockSone("SoneId").local().create(); + Post post = mocks.mockPost(sender, "PostId").create(); + when(freenetRequest.getHttpRequest().getParam("post")).thenReturn("PostId"); + JsonReturnObject jsonReturnObject = deletePostAjaxPage.createJsonObject(freenetRequest); + assertThat(jsonReturnObject.isSuccess(), is(true)); + verify(mocks.core).deletePost(post); + } + + @Test + public void canNotDeletePostWithInvalidId() { + JsonReturnObject jsonReturnObject = deletePostAjaxPage.createJsonObject(freenetRequest); + verifyFailure(jsonReturnObject, "invalid-post-id"); + } + + private void verifyFailure(JsonReturnObject jsonReturnObject, String errorId) { + assertThat(jsonReturnObject.isSuccess(), is(false)); + assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is(errorId)); + } + + @Test + public void canNotDeletePostFromRemoteSone() { + Sone sender = mocks.mockSone("SoneId").create(); + mocks.mockPost(sender, "PostId").create(); + when(freenetRequest.getHttpRequest().getParam("post")).thenReturn("PostId"); + JsonReturnObject jsonReturnObject = deletePostAjaxPage.createJsonObject(freenetRequest); + verifyFailure(jsonReturnObject, "not-authorized"); + } + +}