From: David ‘Bombe’ Roden Date: Wed, 5 Jul 2017 20:21:53 +0000 (+0200) Subject: Add test for create post ajax page X-Git-Tag: 0.9.7^2~108 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=858eb848396910330160dcce9817c0d3e6d37cce;p=Sone.git Add test for create post ajax page --- diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt new file mode 100644 index 0000000..0588b6a --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt @@ -0,0 +1,117 @@ +package net.pterodactylus.sone.web.ajax + +import com.google.common.base.Optional +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.nullValue +import org.junit.Test + +/** + * Unit test for [CreatePostAjaxPage]. + */ +class CreatePostAjaxPageTest : JsonPageTest(::CreatePostAjaxPage) { + + @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("createPost.ajax")) + } + + @Test + fun `page needs form password`() { + assertThat(page.needsFormPassword(), equalTo(true)) + } + + @Test + fun `page requires login`() { + assertThat(page.requiresLogin(), equalTo(true)) + } + + @Test + fun `page requires a current sone`() { + unsetCurrentSone() + assertThat(json.isSuccess, equalTo(false)) + assertThat(json.error, equalTo("auth-required")) + } + + @Test + fun `missing text parameter returns error`() { + assertThat(json.isSuccess, equalTo(false)) + assertThat(json.error, equalTo("text-required")) + } + + @Test + fun `empty text returns error`() { + addRequestParameter("text", "") + assertThat(json.isSuccess, equalTo(false)) + assertThat(json.error, equalTo("text-required")) + } + + @Test + fun `whitespace-only text returns error`() { + addRequestParameter("text", " ") + assertThat(json.isSuccess, equalTo(false)) + assertThat(json.error, equalTo("text-required")) + } + + @Test + fun `request with valid data creates post`() { + addRequestParameter("text", "test") + val post = createPost() + whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post) + assertThat(json.isSuccess, equalTo(true)) + assertThat(json["postId"].asText(), equalTo("id")) + assertThat(json["sone"].asText(), equalTo(currentSone.id)) + assertThat(json["recipient"], nullValue()) + } + + @Test + fun `request with invalid recipient creates post without recipient`() { + addRequestParameter("text", "test") + addRequestParameter("recipient", "invalid") + val post = createPost() + whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post) + assertThat(json.isSuccess, equalTo(true)) + assertThat(json["postId"].asText(), equalTo("id")) + assertThat(json["sone"].asText(), equalTo(currentSone.id)) + assertThat(json["recipient"], nullValue()) + } + + @Test + fun `request with valid data and recipient creates correct post`() { + addRequestParameter("text", "test") + addRequestParameter("recipient", "valid") + val recipient = mock().apply { whenever(id).thenReturn("valid") } + addSone(recipient) + val post = createPost("valid") + whenever(core.createPost(currentSone, Optional.of(recipient), "test")).thenReturn(post) + assertThat(json.isSuccess, equalTo(true)) + assertThat(json["postId"].asText(), equalTo("id")) + assertThat(json["sone"].asText(), equalTo(currentSone.id)) + assertThat(json["recipient"].asText(), equalTo("valid")) + } + + @Test + fun `text is filtered correctly`() { + addRequestParameter("text", "Link http://freenet.test:8888/KSK@foo is filtered") + addRequestHeader("Host", "freenet.test:8888") + val post = createPost() + whenever(core.createPost(currentSone, Optional.absent(), "Link KSK@foo is filtered")).thenReturn(post) + assertThat(json.isSuccess, equalTo(true)) + assertThat(json["postId"].asText(), equalTo("id")) + assertThat(json["sone"].asText(), equalTo(currentSone.id)) + assertThat(json["recipient"], nullValue()) + } + + private fun createPost(recipientId: String? = null) = + mock().apply { + whenever(id).thenReturn("id") + whenever(sone).thenReturn(currentSone) + whenever(this.recipientId).thenReturn(recipientId.asOptional()) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index b4d897c..b0a67d4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -21,8 +21,10 @@ import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.web.Method.GET import org.junit.Before +import org.mockito.ArgumentMatchers.anyBoolean import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyString +import org.mockito.ArgumentMatchers.eq import org.mockito.ArgumentMatchers.isNull import java.util.NoSuchElementException import javax.naming.SizeLimitExceededException @@ -43,6 +45,7 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock() protected val currentSone = deepMock() + private val requestHeaders = mutableMapOf() private val requestParameters = mutableMapOf() private val requestParts = mutableMapOf() private val localSones = mutableMapOf() @@ -54,6 +57,7 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock JsonPage = { _ -> mock(0).toLowerCase()] } whenever(httpRequest.getParam(anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" } whenever(httpRequest.getParam(anyString(), anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: it.getArgument(1) } whenever(httpRequest.getParam(anyString(), isNull())).thenAnswer { requestParameters[it.getArgument(0)] } @@ -102,6 +107,8 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock JsonPage = { _ -> mock