+++ /dev/null
-/*
- * 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();
- }
-
-}
--- /dev/null
+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")
+ }
+
+
+}
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
/**
@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()
+ }
+
}
--- /dev/null
+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)
+ }
+
+}