Add test for get post ajax page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 10 Sep 2017 14:49:28 +0000 (16:49 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 10 Sep 2017 14:49:28 +0000 (16:49 +0200)
src/main/kotlin/net/pterodactylus/sone/template/LinkedElementRenderFilter.kt
src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt
src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt

index a527a1d..4536c04 100644 (file)
@@ -1,11 +1,10 @@
 package net.pterodactylus.sone.template
 
 import net.pterodactylus.sone.core.LinkedElement
+import net.pterodactylus.sone.utils.asTemplate
 import net.pterodactylus.util.template.Filter
 import net.pterodactylus.util.template.TemplateContext
 import net.pterodactylus.util.template.TemplateContextFactory
-import net.pterodactylus.util.template.TemplateParser
-import java.io.StringReader
 import java.io.StringWriter
 import javax.inject.Inject
 
@@ -15,11 +14,9 @@ import javax.inject.Inject
 class LinkedElementRenderFilter @Inject constructor(private val templateContextFactory: TemplateContextFactory): Filter {
 
        companion object {
-               private val loadedImageTemplate = """<%include linked/image.html>""".parse()
-               private val loadedHtmlPageTemplate = """<%include linked/html-page.html>""".parse()
-               private val notLoadedImageTemplate = """<%include linked/notLoaded.html>""".parse()
-
-               private fun String.parse() = StringReader(this).use { TemplateParser.parse(it) }!!
+               private val loadedImageTemplate = """<%include linked/image.html>""".asTemplate()
+               private val loadedHtmlPageTemplate = """<%include linked/html-page.html>""".asTemplate()
+               private val notLoadedImageTemplate = """<%include linked/notLoaded.html>""".asTemplate()
        }
 
        override fun format(templateContext: TemplateContext?, data: Any?, parameters: Map<String, Any?>?) =
index 0db2a29..43331c3 100644 (file)
@@ -10,11 +10,10 @@ import net.pterodactylus.sone.text.PostPart
 import net.pterodactylus.sone.text.SonePart
 import net.pterodactylus.sone.text.SoneTextParser
 import net.pterodactylus.sone.text.SoneTextParserContext
+import net.pterodactylus.sone.utils.asTemplate
 import net.pterodactylus.util.template.Filter
 import net.pterodactylus.util.template.TemplateContext
 import net.pterodactylus.util.template.TemplateContextFactory
-import net.pterodactylus.util.template.TemplateParser
-import java.io.StringReader
 import java.io.StringWriter
 import java.io.Writer
 import java.net.URLEncoder
@@ -27,8 +26,8 @@ import java.net.URLEncoder
 class RenderFilter(private val core: Core, private val templateContextFactory: TemplateContextFactory) : Filter {
 
        companion object {
-               private val plainTextTemplate = TemplateParser.parse(StringReader("<%text|html>"))
-               private val linkTemplate = TemplateParser.parse(StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"))
+               private val plainTextTemplate = "<%text|html>".asTemplate()
+               private val linkTemplate = "<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>".asTemplate()
        }
 
        override fun format(templateContext: TemplateContext?, data: Any?, parameters: MutableMap<String, Any?>?): Any? {
diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Templates.kt
new file mode 100644 (file)
index 0000000..8187138
--- /dev/null
@@ -0,0 +1,7 @@
+package net.pterodactylus.sone.utils
+
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateParser
+import java.io.StringReader
+
+fun String.asTemplate(): Template = StringReader(this).use { TemplateParser.parse(it) }
diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPageTest.kt
new file mode 100644 (file)
index 0000000..768a186
--- /dev/null
@@ -0,0 +1,55 @@
+package net.pterodactylus.sone.web.ajax
+
+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 net.pterodactylus.sone.utils.asTemplate
+import net.pterodactylus.util.template.ReflectionAccessor
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+
+/**
+ * Unit test for [GetPostAjaxPage].
+ */
+class GetPostAjaxPageTest : JsonPageTest("getPost.ajax", needsFormPassword = false,
+               pageSupplier = { webInterface ->
+                       GetPostAjaxPage(webInterface, "<%core>\n<%request>\n<%post.text>\n<%currentSone>\n<%localSones>".asTemplate())
+               }) {
+
+       @Test
+       fun `request with missing post results in invalid-post-id`() {
+               assertThat(json.isSuccess, equalTo(false))
+               assertThat(json.error, equalTo("invalid-post-id"))
+       }
+
+       @Test
+       fun `request with valid post results in post json`() {
+               val sone = mock<Sone>().apply { whenever(id).thenReturn("sone-id") }
+               val post = mock<Post>().apply {
+                       whenever(id).thenReturn("post-id")
+                       whenever(time).thenReturn(1000)
+                       whenever(this.sone).thenReturn(sone)
+                       whenever(recipientId).thenReturn("recipient-id".asOptional())
+                       whenever(text).thenReturn("post text")
+               }
+               webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
+               addPost(post)
+               addRequestParameter("post", "post-id")
+               assertThat(json.isSuccess, equalTo(true))
+               assertThat(json["post"]["id"].asText(), equalTo("post-id"))
+               assertThat(json["post"]["time"].asLong(), equalTo(1000L))
+               assertThat(json["post"]["sone"].asText(), equalTo("sone-id"))
+               assertThat(json["post"]["recipient"].asText(), equalTo("recipient-id"))
+               assertThat(json["post"]["html"].asText(), equalTo(listOf(
+                               core.toString(),
+                               freenetRequest.toString(),
+                               "post text",
+                               currentSone.toString(),
+                               core.localSones.toString()
+               ).joinToString("\n")))
+       }
+
+}
index ac70936..fed8784 100644 (file)
@@ -26,6 +26,7 @@ import net.pterodactylus.sone.utils.asOptional
 import net.pterodactylus.sone.web.WebInterface
 import net.pterodactylus.sone.web.page.FreenetRequest
 import net.pterodactylus.util.notify.Notification
+import net.pterodactylus.util.template.TemplateContextFactory
 import net.pterodactylus.util.web.Method.GET
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
@@ -82,6 +83,7 @@ abstract class JsonPageTest(
 
        @Before
        fun setupWebInterface() {
+               whenever(webInterface.templateContextFactory).thenReturn(TemplateContextFactory())
                whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone)