1 package net.pterodactylus.sone.template
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.database.*
5 import net.pterodactylus.sone.test.*
6 import net.pterodactylus.sone.text.*
7 import net.pterodactylus.sone.text.Part
8 import net.pterodactylus.util.template.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
12 import org.jsoup.nodes.*
18 * Unit test for [RenderFilter].
20 class RenderFilterTest {
23 private const val FREEMAIL_ID = "t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra"
24 private const val SONE_FREEMAIL = "sone@$FREEMAIL_ID.freemail"
25 private const val SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"
26 private const val POST_ID = "37a06250-6775-4b94-86ff-257ba690953c"
29 private val soneProvider = mock<SoneProvider>()
30 private val soneTextParser = mock<SoneTextParser>()
31 private val htmlFilter = HtmlFilter()
32 private val sone = setupSone(SONE_IDENTITY, "Sone", "First")
33 private val parameters = mutableMapOf<String, Any?>()
35 private val filter = RenderFilter(soneProvider, soneTextParser, htmlFilter)
36 private val templateContext = TemplateContext()
39 fun `plain text part is rendered correctly`() {
40 assertThat(renderParts(PlainTextPart("plain text")), equalTo("plain text"))
43 private fun renderParts(vararg part: Part) = filter.format(templateContext, listOf(*part), parameters) as String
46 fun `freenet link is rendered correctly`() {
47 val linkNode = renderParts(FreenetLinkPart("KSK@gpl.txt", "gpl.txt", false)).toLinkNode()
48 verifyLink(linkNode, "/KSK@gpl.txt", "freenet", "KSK@gpl.txt", "gpl.txt")
51 private fun verifyLink(linkNode: Element, url: String, cssClass: String, tooltip: String, text: String) {
52 assertThat(linkNode.nodeName(), equalTo("a"))
53 assertThat<List<Attribute>>(linkNode.attributes().asList(), containsInAnyOrder(
54 Attribute("href", url),
55 Attribute("class", cssClass),
56 Attribute("title", tooltip)
58 assertThat(linkNode.text(), equalTo(text))
62 fun `trusted freenet link is rendered with correct css class`() {
63 val linkNode = renderParts(FreenetLinkPart("KSK@gpl.txt", "gpl.txt", true)).toLinkNode()
64 verifyLink(linkNode, "/KSK@gpl.txt", "freenet-trusted", "KSK@gpl.txt", "gpl.txt")
67 private fun String.toLinkNode() = Jsoup.parseBodyFragment(this).body().child(0)
70 fun `internet link is rendered correctly`() {
71 val linkNode = renderParts(LinkPart("http://test.com/test.html", "test.com/test.html")).toLinkNode()
72 verifyLink(linkNode, "/external-link/?_CHECKED_HTTP_=${URLEncoder.encode("http://test.com/test.html", "UTF-8")}", "internet",
73 "http://test.com/test.html", "test.com/test.html")
77 fun `sone parts are rendered correctly`() {
78 val linkNode = renderParts(SonePart(sone)).toLinkNode()
79 verifyLink(linkNode, "viewSone.html?sone=" + SONE_IDENTITY, "in-sone", "First", "First")
82 private fun setupSone(identity: String, name: String?, firstName: String): Sone {
83 val sone = mock<Sone>()
84 whenever(sone.id).thenReturn(identity)
85 whenever(sone.profile).thenReturn(Profile(sone))
86 whenever(sone.name).thenReturn(name)
87 sone.profile.firstName = firstName
88 whenever(soneProvider.getSone(identity)).thenReturn(sone)
93 fun `sone part with unknown sone is rendered as link to web of trust`() {
94 val sone = setupSone(SONE_IDENTITY, null, "First")
95 val linkNode = renderParts(SonePart(sone)).toLinkNode()
96 verifyLink(linkNode, "/WebOfTrust/ShowIdentity?id=$SONE_IDENTITY", "in-sone", SONE_IDENTITY, SONE_IDENTITY)
100 fun `post part is cut off correctly when there are spaces`() {
101 val post = setupPost(sone, "1234 678901 345 789012 45678 01.")
102 whenever(soneTextParser.parse(eq("1234 678901 345 789012 45678 01."), ArgumentMatchers.any()))
103 .thenReturn(listOf(PlainTextPart("1234 678901 345 789012 45678 01.")))
104 val linkNode = renderParts(PostPart(post)).toLinkNode()
105 verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "1234 678901 345…")
108 private fun setupPost(sone: Sone, value: String) =
110 whenever(id).thenReturn(POST_ID)
111 whenever(this.sone).thenReturn(this@RenderFilterTest.sone)
112 whenever(text).thenReturn(value)
116 fun `post part is cut off correctly when there are no spaces`() {
117 val post = setupPost(sone, "1234567890123456789012345678901.")
118 whenever(soneTextParser.parse(eq("1234567890123456789012345678901."), ArgumentMatchers.any()))
119 .thenReturn(listOf(PlainTextPart("1234567890123456789012345678901.")))
120 val linkNode = renderParts(PostPart(post)).toLinkNode()
121 verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "12345678901234567890…")
125 fun `post part shorter than 21 chars is not cut off`() {
126 val post = setupPost(sone, "12345678901234567890")
127 whenever(soneTextParser.parse(eq("12345678901234567890"), ArgumentMatchers.any()))
128 .thenReturn(listOf(PlainTextPart("12345678901234567890")))
129 val linkNode = renderParts(PostPart(post)).toLinkNode()
130 verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "12345678901234567890")
134 fun `multiple parts are rendered correctly`() {
135 val parts = arrayOf(PlainTextPart("te"), PlainTextPart("xt"))
136 assertThat(renderParts(*parts), equalTo("text"))
140 fun `freemail address is displayed correctly`() {
141 val linkNode = renderParts(FreemailPart("sone", FREEMAIL_ID, SONE_IDENTITY)).toLinkNode()
142 verifyLink(linkNode, "/Freemail/NewMessage?to=$SONE_IDENTITY", "in-sone", "First\n$SONE_FREEMAIL", "sone@First.freemail")