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