Add test for create post ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 5 Jul 2017 20:21:53 +0000 (22:21 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 5 Jul 2017 20:21:53 +0000 (22:21 +0200)
src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt

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 (file)
index 0000000..0588b6a
--- /dev/null
@@ -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<Sone>().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<Post>().apply {
+                               whenever(id).thenReturn("id")
+                               whenever(sone).thenReturn(currentSone)
+                               whenever(this.recipientId).thenReturn(recipientId.asOptional())
+                       }
+
+}
index b4d897c..b0a67d4 100644 (file)
@@ -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<J
        protected val httpRequest = mock<HTTPRequest>()
        protected val currentSone = deepMock<Sone>()
 
+       private val requestHeaders = mutableMapOf<String, String>()
        private val requestParameters = mutableMapOf<String, String>()
        private val requestParts = mutableMapOf<String, String>()
        private val localSones = mutableMapOf<String, Sone>()
@@ -54,6 +57,7 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
 
        @Before
        fun setupWebInterface() {
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.core).thenReturn(core)
@@ -90,6 +94,7 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        @Before
        fun setupHttpRequest() {
                whenever(httpRequest.method).thenReturn("GET")
+               whenever(httpRequest.getHeader(anyString())).thenAnswer { requestHeaders[it.get<String>(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<J
                whenever(httpRequest.isPartSet(anyString())).thenAnswer { it.getArgument(0) in requestParts }
        }
 
+       protected val JsonReturnObject.error get() = if (this is JsonErrorReturnObject) this.error else null
+
        protected fun Sone.mock(id: String, name: String, local: Boolean = false, time: Long, status: SoneStatus = idle) = apply {
                whenever(this.id).thenReturn(id)
                whenever(this.name).thenReturn(name)
@@ -111,10 +118,15 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        }
 
        protected fun unsetCurrentSone() {
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null)
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null)
        }
 
+       protected fun addRequestHeader(key: String, value: String) {
+               requestHeaders += key.toLowerCase() to value
+       }
+
        protected fun addRequestParameter(key: String, value: String) {
                requestParameters += key to value
        }