From b7ca3f9535d49bbde4f21316190af4c7c0918dea Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 19 Feb 2014 21:51:02 +0100 Subject: [PATCH] Add unit test for DeletePostAjaxPage. --- .../sone/web/ajax/DeletePostAjaxPageTest.java | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.java 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"); + } + +} -- 2.7.4