1 package net.pterodactylus.sone.template
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.data.Profile
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.text.FreemailPart
9 import net.pterodactylus.sone.text.FreenetLinkPart
10 import net.pterodactylus.sone.text.LinkPart
11 import net.pterodactylus.sone.text.Part
12 import net.pterodactylus.sone.text.PlainTextPart
13 import net.pterodactylus.sone.text.PostPart
14 import net.pterodactylus.sone.text.SonePart
15 import net.pterodactylus.util.template.HtmlFilter
16 import net.pterodactylus.util.template.TemplateContext
17 import net.pterodactylus.util.template.TemplateContextFactory
18 import org.hamcrest.MatcherAssert.assertThat
19 import org.hamcrest.Matchers.`is`
20 import org.hamcrest.Matchers.containsInAnyOrder
21 import org.jsoup.Jsoup
22 import org.jsoup.nodes.Attribute
23 import org.jsoup.nodes.Element
25 import org.mockito.Mockito.`when`
26 import java.net.URLEncoder
29 * Unit test for [RenderFilter].
31 class RenderFilterTest {
34 private const val FREEMAIL_ID = "t4dlzfdww3xvsnsc6j6gtliox6zaoak7ymkobbmcmdw527ubuqra"
35 private const val SONE_FREEMAIL = "sone@$FREEMAIL_ID.freemail"
36 private const val SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"
37 private const val POST_ID = "37a06250-6775-4b94-86ff-257ba690953c"
40 private val core = mock<Core>()
41 private val templateContextFactory = TemplateContextFactory()
42 private val templateContext: TemplateContext
43 private val sone = setupSone(SONE_IDENTITY, "Sone", "First")
44 private val parameters = mutableMapOf<String, Any?>()
47 templateContextFactory.addFilter("html", HtmlFilter())
48 templateContext = templateContextFactory.createTemplateContext()
51 private val filter = RenderFilter(core, templateContextFactory)
54 fun `plain text part is rendered correctly`() {
55 assertThat(renderParts(PlainTextPart("plain text")), `is`("plain text"))
58 private fun renderParts(vararg part: Part) = filter.format(templateContext, listOf(*part), parameters) as String
61 fun `freenet link is rendered correctly`() {
62 val linkNode = renderParts(FreenetLinkPart("KSK@gpl.txt", "gpl.txt", false)).toLinkNode()
63 verifyLink(linkNode, "/KSK@gpl.txt", "freenet", "KSK@gpl.txt", "gpl.txt")
66 private fun verifyLink(linkNode: Element, url: String, cssClass: String, tooltip: String, text: String) {
67 assertThat(linkNode.nodeName(), `is`("a"))
68 assertThat<List<Attribute>>(linkNode.attributes().asList(), containsInAnyOrder(
69 Attribute("href", url),
70 Attribute("class", cssClass),
71 Attribute("title", tooltip)
73 assertThat(linkNode.text(), `is`(text))
77 fun `trusted freenet link is rendered with correct css class`() {
78 val linkNode = renderParts(FreenetLinkPart("KSK@gpl.txt", "gpl.txt", true)).toLinkNode()
79 verifyLink(linkNode, "/KSK@gpl.txt", "freenet-trusted", "KSK@gpl.txt", "gpl.txt")
82 private fun String.toLinkNode() = Jsoup.parseBodyFragment(this).body().child(0)
85 fun `internet link is rendered correctly`() {
86 val linkNode = renderParts(LinkPart("http://test.com/test.html", "test.com/test.html")).toLinkNode()
87 verifyLink(linkNode, "/external-link/?_CHECKED_HTTP_=${URLEncoder.encode("http://test.com/test.html", "UTF-8")}", "internet",
88 "http://test.com/test.html", "test.com/test.html")
92 fun `sone parts are rendered correctly`() {
93 val linkNode = renderParts(SonePart(sone)).toLinkNode()
94 verifyLink(linkNode, "viewSone.html?sone=" + SONE_IDENTITY, "in-sone", "First", "First")
97 private fun setupSone(identity: String, name: String?, firstName: String): Sone {
98 val sone = mock<Sone>()
99 `when`(sone.id).thenReturn(identity)
100 `when`(sone.profile).thenReturn(Profile(sone))
101 `when`(sone.name).thenReturn(name)
102 sone.profile.firstName = firstName
103 `when`(core.getSone(identity)).thenReturn(sone)
108 fun `sone part with unknown sone is rendered as link to web of trust`() {
109 val sone = setupSone(SONE_IDENTITY, null, "First")
110 val linkNode = renderParts(SonePart(sone)).toLinkNode()
111 verifyLink(linkNode, "/WebOfTrust/ShowIdentity?id=$SONE_IDENTITY", "in-sone", SONE_IDENTITY, SONE_IDENTITY)
115 fun `post part is cut off correctly when there are spaces`() {
116 val post = setupPost(sone, "1234 678901 345 789012 45678 01.")
117 val linkNode = renderParts(PostPart(post)).toLinkNode()
118 verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "1234 678901 345…")
121 private fun setupPost(sone: Sone, value: String): Post {
122 val post = mock<Post>()
123 `when`(post.id).thenReturn(POST_ID)
124 `when`(post.sone).thenReturn(sone)
125 `when`(post.text).thenReturn(value)
130 fun `post part is cut off correctly when there are no spaces`() {
131 val post = setupPost(sone, "1234567890123456789012345678901.")
132 val linkNode = renderParts(PostPart(post)).toLinkNode()
133 verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "12345678901234567890…")
137 fun `post part shorter than 21 chars is not cut off`() {
138 val post = setupPost(sone, "12345678901234567890")
139 val linkNode = renderParts(PostPart(post)).toLinkNode()
140 verifyLink(linkNode, "viewPost.html?post=$POST_ID", "in-sone", "First", "12345678901234567890")
144 fun `multiple parts are rendered correctly`() {
145 val parts = arrayOf(PlainTextPart("te"), PlainTextPart("xt"))
146 assertThat(renderParts(*parts), `is`("text"))
150 fun `freemail address is displayed correctly`() {
151 val linkNode = renderParts(FreemailPart("sone", FREEMAIL_ID, SONE_IDENTITY)).toLinkNode()
152 verifyLink(linkNode, "/Freemail/NewMessage?to=$SONE_IDENTITY", "in-sone", "First\n$SONE_FREEMAIL", "sone@First.freemail")