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
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?>?) =
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
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? {
--- /dev/null
+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) }
--- /dev/null
+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")))
+ }
+
+}
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
@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)