Add test for lock Sone ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Sep 2017 19:18:57 +0000 (21:18 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 14 Sep 2017 19:47:06 +0000 (21:47 +0200)
src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/ajax/LikeAjaxPage.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/LikeAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/LockSoneAjaxPageTest.kt [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java
deleted file mode 100644 (file)
index 666b1bf..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Sone - LikeAjaxPage.java - Copyright © 2010–2016 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.web.ajax;
-
-import javax.annotation.Nonnull;
-
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-
-/**
- * AJAX page that lets the user like a {@link Post}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class LikeAjaxPage extends LoggedInJsonPage {
-
-       /**
-        * Creates a new “like post” AJAX page.
-        *
-        * @param webInterface
-        *            The Sone web interface
-        */
-       public LikeAjaxPage(WebInterface webInterface) {
-               super("like.ajax", webInterface);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Nonnull
-       @Override
-       protected JsonReturnObject createJsonObject(@Nonnull Sone currentSone, @Nonnull FreenetRequest request) {
-               String type = request.getHttpRequest().getParam("type", null);
-               String id = request.getHttpRequest().getParam(type, null);
-               if ((id == null) || (id.length() == 0)) {
-                       return createErrorJsonObject("invalid-" + type + "-id");
-               }
-               if ("post".equals(type)) {
-                       currentSone.addLikedPostId(id);
-                       webInterface.getCore().touchConfiguration();
-               } else if ("reply".equals(type)) {
-                       currentSone.addLikedReplyId(id);
-                       webInterface.getCore().touchConfiguration();
-               } else {
-                       return createErrorJsonObject("invalid-type");
-               }
-               return createSuccessJsonObject();
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/LikeAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/LikeAjaxPage.kt
new file mode 100644 (file)
index 0000000..3b206c8
--- /dev/null
@@ -0,0 +1,32 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.utils.let
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+
+/**
+ * AJAX page that lets the user like a [net.pterodactylus.sone.data.Post].
+ */
+class LikeAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("like.ajax", webInterface) {
+
+       override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
+                       when (request.parameters["type"]) {
+                               "post" -> request.parameters["post"]
+                                               .let(webInterface.core::getPost)
+                                               ?.let { currentSone.addLikedPostId(it.id) }
+                                               ?.also { webInterface.core.touchConfiguration() }
+                                               ?.let { createSuccessJsonObject() }
+                                               ?: createErrorJsonObject("invalid-post-id")
+                               "reply" -> request.parameters["reply"]
+                                               .let(webInterface.core::getPostReply)
+                                               ?.let { currentSone.addLikedReplyId(it.id) }
+                                               ?.also { webInterface.core.touchConfiguration() }
+                                               ?.let { createSuccessJsonObject() }
+                                               ?: createErrorJsonObject("invalid-reply-id")
+                               else -> createErrorJsonObject("invalid-type")
+                       }
+
+
+}
index 5c90809..9a8f2c2 100644 (file)
@@ -1,8 +1,13 @@
 package net.pterodactylus.sone.web.ajax
 
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.data.PostReply
+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.never
 import org.mockito.Mockito.verify
 
 /**
@@ -12,28 +17,48 @@ class LikeAjaxPageTest : JsonPageTest("like.ajax", pageSupplier = ::LikeAjaxPage
 
        @Test
        fun `request with invalid type results in invalid-type error`() {
-           addRequestParameter("type", "invalid")
+               addRequestParameter("type", "invalid")
                addRequestParameter("invalid", "invalid-id")
                assertThat(json.isSuccess, equalTo(false))
                assertThat(json.error, equalTo("invalid-type"))
        }
 
        @Test
-       fun `request with post id results in post being liked by current sone`() {
+       fun `request with valid post id results in post being liked by current sone`() {
                addRequestParameter("type", "post")
                addRequestParameter("post", "post-id")
+               addPost(mock<Post>().apply { whenever(id).thenReturn("post-id") })
                assertThat(json.isSuccess, equalTo(true))
                verify(currentSone).addLikedPostId("post-id")
                verify(core).touchConfiguration()
        }
 
        @Test
-       fun `request with reply id results in reply being liked by current sone`() {
+       fun `request with valid reply id results in reply being liked by current sone`() {
                addRequestParameter("type", "reply")
                addRequestParameter("reply", "reply-id")
+               addReply(mock<PostReply>().apply { whenever(id).thenReturn("reply-id") })
                assertThat(json.isSuccess, equalTo(true))
                verify(currentSone).addLikedReplyId("reply-id")
                verify(core).touchConfiguration()
        }
 
+       @Test
+       fun `request with invalid post id results in post being liked by current sone`() {
+               addRequestParameter("type", "post")
+               addRequestParameter("post", "post-id")
+               assertThat(json.isSuccess, equalTo(false))
+               verify(currentSone, never()).addLikedPostId("post-id")
+               verify(core, never()).touchConfiguration()
+       }
+
+       @Test
+       fun `request with invalid reply id results in reply being liked by current sone`() {
+               addRequestParameter("type", "reply")
+               addRequestParameter("reply", "reply-id")
+               assertThat(json.isSuccess, equalTo(false))
+               verify(currentSone, never()).addLikedReplyId("reply-id")
+               verify(core, never()).touchConfiguration()
+       }
+
 }
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/LockSoneAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/LockSoneAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..b63be51
--- /dev/null
@@ -0,0 +1,30 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.mock
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+import org.mockito.Mockito.verify
+
+/**
+ * Unit test for [LockSoneAjaxPage].
+ */
+class LockSoneAjaxPageTest : JsonPageTest("lockSone.ajax", requiresLogin = false, pageSupplier = ::LockSoneAjaxPage) {
+
+       @Test
+       fun `request without valid sone results in invalid-sone-id`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-sone-id"))
+       }
+
+       @Test
+       fun `request with valid sone id results in locked sone`() {
+               val sone = mock<Sone>()
+               addLocalSone("sone-id", sone)
+               addRequestParameter("sone", "sone-id")
+               assertThat(json.isSuccess, equalTo(true))
+               verify(core).lockSone(sone)
+       }
+
+}