1 package net.pterodactylus.sone.template
3 import net.pterodactylus.sone.core.ElementLoader
4 import net.pterodactylus.sone.core.LinkedElement
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions
7 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.ALWAYS
8 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.FOLLOWED
9 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.MANUALLY_TRUSTED
10 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.TRUSTED
11 import net.pterodactylus.sone.freenet.wot.OwnIdentity
12 import net.pterodactylus.sone.freenet.wot.Trust
13 import net.pterodactylus.sone.test.*
14 import net.pterodactylus.sone.text.FreenetLinkPart
15 import net.pterodactylus.sone.text.LinkPart
16 import net.pterodactylus.sone.text.Part
17 import net.pterodactylus.sone.text.PlainTextPart
18 import net.pterodactylus.util.template.TemplateContext
19 import org.hamcrest.MatcherAssert.assertThat
20 import org.hamcrest.Matchers.contains
21 import org.hamcrest.Matchers.emptyIterable
22 import org.junit.Before
26 * Unit test for [LinkedElementsFilter].
28 class LinkedElementsFilterTest {
30 private val imageLoader = mock<ElementLoader>()
31 private val filter = LinkedElementsFilter(imageLoader)
32 private val templateContext = TemplateContext()
33 private val parameters = mutableMapOf<String, Any?>()
34 private val sone = createSone()
35 private val remoteSone = createSone("remote-id")
36 private val parts: List<Part> = listOf(
37 PlainTextPart("text"),
38 LinkPart("http://link", "link"),
39 FreenetLinkPart("KSK@link", "link", false),
40 FreenetLinkPart("KSK@loading.png", "link", false),
41 FreenetLinkPart("KSK@link.png", "link", false)
46 whenever(sone.options).thenReturn(DefaultSoneOptions())
50 fun setupImageLoader() {
51 whenever(imageLoader.loadElement("KSK@link")).thenReturn(LinkedElement("KSK@link", failed = true))
52 whenever(imageLoader.loadElement("KSK@loading.png")).thenReturn(LinkedElement("KSK@loading.png", loading = true))
53 whenever(imageLoader.loadElement("KSK@link.png")).thenReturn(LinkedElement("KSK@link.png"))
57 fun `filter does not find any image if there is no template context`() {
58 assertThat(filter.format(null, parts, parameters), emptyIterable())
62 fun `filter does not find any image if there is no current sone`() {
63 verifyThatImagesAreNotPresent()
67 fun `filter does not find any images if there is no remote sone`() {
68 sone.options.loadLinkedImages = ALWAYS
69 templateContext.set("currentSone", sone)
70 verifyThatImagesAreNotPresent()
74 fun `filter does not find any images if sone does not allow to load images`() {
75 templateContext.set("currentSone", sone)
76 parameters["sone"] = remoteSone
77 verifyThatImagesAreNotPresent()
81 fun `filter finds all loaded freenet images from the sone itself`() {
82 templateContext.set("currentSone", sone)
83 parameters["sone"] = sone
84 verifyThatImagesArePresent()
88 fun `filter finds images if the remote sone is local`() {
89 sone.options.loadLinkedImages = MANUALLY_TRUSTED
90 templateContext.set("currentSone", sone)
91 whenever(remoteSone.isLocal).thenReturn(true)
92 parameters["sone"] = remoteSone
93 verifyThatImagesArePresent()
97 fun `filter does not find images if local sone requires manual trust and remote sone has not trust`() {
98 sone.options.loadLinkedImages = MANUALLY_TRUSTED
99 templateContext.set("currentSone", sone)
100 parameters["sone"] = remoteSone
101 verifyThatImagesAreNotPresent()
105 fun `filter does not find images if local sone requires manual trust and remote sone has only implicit trust`() {
106 sone.options.loadLinkedImages = MANUALLY_TRUSTED
107 templateContext.set("currentSone", sone)
108 whenever(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(null, 100, null))
109 parameters["sone"] = remoteSone
110 verifyThatImagesAreNotPresent()
114 fun `filter does not find images if local sone requires manual trust and remote sone has explicit trust of zero`() {
115 sone.options.loadLinkedImages = MANUALLY_TRUSTED
116 templateContext.set("currentSone", sone)
117 whenever(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(0, null, null))
118 parameters["sone"] = remoteSone
119 verifyThatImagesAreNotPresent()
123 fun `filter finds images if local sone requires manual trust and remote sone has explicit trust of one`() {
124 sone.options.loadLinkedImages = MANUALLY_TRUSTED
125 templateContext.set("currentSone", sone)
126 whenever(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
127 parameters["sone"] = remoteSone
128 verifyThatImagesArePresent()
132 fun `filter does not find images if local sone requires following and remote sone is not followed`() {
133 sone.options.loadLinkedImages = FOLLOWED
134 templateContext["currentSone"] = sone
135 parameters["sone"] = remoteSone
136 verifyThatImagesAreNotPresent()
140 fun `filter finds images if local sone requires following and remote sone is followed`() {
141 sone.options.loadLinkedImages = FOLLOWED
142 whenever(sone.hasFriend("remote-id")).thenReturn(true)
143 templateContext["currentSone"] = sone
144 parameters["sone"] = remoteSone
145 verifyThatImagesArePresent()
149 fun `filter finds images if local sone requires following and remote sone is the same as the local sone`() {
150 sone.options.loadLinkedImages = FOLLOWED
151 templateContext["currentSone"] = sone
152 parameters["sone"] = sone
153 verifyThatImagesArePresent()
157 fun `filter finds images if following is required and remote sone is a local sone`() {
158 sone.options.loadLinkedImages = FOLLOWED
159 templateContext["currentSone"] = sone
160 whenever(remoteSone.isLocal).thenReturn(true)
161 parameters["sone"] = remoteSone
162 verifyThatImagesArePresent()
166 fun `filter does not find images if any trust is required and remote sone does not have any trust`() {
167 sone.options.loadLinkedImages = TRUSTED
168 templateContext["currentSone"] = sone
169 parameters["sone"] = remoteSone
170 verifyThatImagesAreNotPresent()
174 fun `filter does not find images if any trust is required and remote sone has implicit trust of zero`() {
175 sone.options.loadLinkedImages = TRUSTED
176 templateContext["currentSone"] = sone
177 whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 0, null))
178 parameters["sone"] = remoteSone
179 verifyThatImagesAreNotPresent()
183 fun `filter finds images if any trust is required and remote sone has implicit trust of one`() {
184 sone.options.loadLinkedImages = TRUSTED
185 templateContext["currentSone"] = sone
186 whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 1, null))
187 parameters["sone"] = remoteSone
188 verifyThatImagesArePresent()
192 fun `filter does not find images if any trust is required and remote sone has explicit trust of zero but implicit trust of one`() {
193 sone.options.loadLinkedImages = TRUSTED
194 templateContext["currentSone"] = sone
195 whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(0, 1, null))
196 parameters["sone"] = remoteSone
197 verifyThatImagesAreNotPresent()
201 fun `filter finds images if any trust is required and remote sone has explicit trust of one but no implicit trust`() {
202 sone.options.loadLinkedImages = TRUSTED
203 templateContext["currentSone"] = sone
204 whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
205 parameters["sone"] = remoteSone
206 verifyThatImagesArePresent()
210 fun `filter finds images if any trust is required and remote sone is a local sone`() {
211 sone.options.loadLinkedImages = TRUSTED
212 templateContext["currentSone"] = sone
213 whenever(remoteSone.isLocal).thenReturn(true)
214 parameters["sone"] = remoteSone
215 verifyThatImagesArePresent()
219 fun `filter finds images if no trust is required`() {
220 sone.options.loadLinkedImages = ALWAYS
221 templateContext["currentSone"] = sone
222 parameters["sone"] = remoteSone
223 verifyThatImagesArePresent()
226 private fun verifyThatImagesArePresent() {
227 val loadedImages = filter.format(templateContext, parts, parameters)
228 assertThat(loadedImages, contains<LinkedElement>(
229 LinkedElement("KSK@loading.png", failed = false, loading = true),
230 LinkedElement("KSK@link.png", failed = false, loading = false)
234 private fun verifyThatImagesAreNotPresent() {
235 assertThat(filter.format(templateContext, parts, parameters), emptyIterable())
238 private fun createSone(id: String = "sone-id"): Sone {
239 val sone = mock<Sone>()
240 whenever(sone.id).thenReturn(id)
241 whenever(sone.options).thenReturn(DefaultSoneOptions())
242 whenever(sone.identity).thenReturn(mock<OwnIdentity>())