🔀 Merge “release/v81” into “master”
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / LinkedElementsFilterTest.kt
1 package net.pterodactylus.sone.template
2
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
23 import org.junit.Test
24
25 /**
26  * Unit test for [LinkedElementsFilter].
27  */
28 class LinkedElementsFilterTest {
29
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)
42         )
43
44         @Before
45         fun setupSone() {
46                 whenever(sone.options).thenReturn(DefaultSoneOptions())
47         }
48
49         @Before
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"))
54         }
55
56         @Test
57         fun `filter does not find any image if there is no template context`() {
58                 assertThat(filter.format(null, parts, parameters), emptyIterable())
59         }
60
61         @Test
62         fun `filter does not find any image if there is no current sone`() {
63                 verifyThatImagesAreNotPresent()
64         }
65
66         @Test
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()
71         }
72
73         @Test
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()
78         }
79
80         @Test
81         fun `filter finds all loaded freenet images from the sone itself`() {
82                 templateContext.set("currentSone", sone)
83                 parameters["sone"] = sone
84                 verifyThatImagesArePresent()
85         }
86
87         @Test
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()
94         }
95
96         @Test
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()
102         }
103
104         @Test
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()
111         }
112
113         @Test
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()
120         }
121
122         @Test
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()
129         }
130
131         @Test
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()
137         }
138
139         @Test
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()
146         }
147
148         @Test
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()
154         }
155
156         @Test
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()
163         }
164
165         @Test
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()
171         }
172
173         @Test
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()
180         }
181
182         @Test
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()
189         }
190
191         @Test
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()
198         }
199
200         @Test
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()
207         }
208
209         @Test
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()
216         }
217
218         @Test
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()
224         }
225
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)
231                 ))
232         }
233
234         private fun verifyThatImagesAreNotPresent() {
235                 assertThat(filter.format(templateContext, parts, parameters), emptyIterable())
236         }
237
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>())
243                 return sone
244         }
245
246 }