Add unit test for DeletePostAjaxPage.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 19 Feb 2014 20:51:02 +0000 (21:51 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:26:09 +0000 (22:26 +0100)
src/test/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPageTest.java [new file with mode: 0644]

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 (file)
index 0000000..912c1d5
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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");
+       }
+
+}