Merge branch 'release-0.9.7'
[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.mock
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 import org.mockito.Mockito.`when`
25
26 /**
27  * Unit test for [LinkedElementsFilter].
28  */
29 class LinkedElementsFilterTest {
30
31         private val imageLoader = mock<ElementLoader>()
32         private val filter = LinkedElementsFilter(imageLoader)
33         private val templateContext = TemplateContext()
34         private val parameters = mutableMapOf<String, Any?>()
35         private val sone = createSone()
36         private val remoteSone = createSone("remote-id")
37         private val parts: List<Part> = listOf(
38                         PlainTextPart("text"),
39                         LinkPart("http://link", "link"),
40                         FreenetLinkPart("KSK@link", "link", false),
41                         FreenetLinkPart("KSK@loading.png", "link", false),
42                         FreenetLinkPart("KSK@link.png", "link", false)
43         )
44
45         @Before
46         fun setupSone() {
47                 `when`(sone.options).thenReturn(DefaultSoneOptions())
48         }
49
50         @Before
51         fun setupImageLoader() {
52                 `when`(imageLoader.loadElement("KSK@link")).thenReturn(LinkedElement("KSK@link", failed = true))
53                 `when`(imageLoader.loadElement("KSK@loading.png")).thenReturn(LinkedElement("KSK@loading.png", loading = true))
54                 `when`(imageLoader.loadElement("KSK@link.png")).thenReturn(LinkedElement("KSK@link.png"))
55         }
56
57         @Test
58         fun `filter does not find any image if there is no template context`() {
59                 assertThat(filter.format(null, parts, parameters), emptyIterable())
60         }
61
62         @Test
63         fun `filter does not find any image if there is no current sone`() {
64                 verifyThatImagesAreNotPresent()
65         }
66
67         @Test
68         fun `filter does not find any images if there is no remote sone`() {
69                 sone.options.loadLinkedImages = ALWAYS
70                 templateContext.set("currentSone", sone)
71                 verifyThatImagesAreNotPresent()
72         }
73
74         @Test
75         fun `filter does not find any images if sone does not allow to load images`() {
76                 templateContext.set("currentSone", sone)
77                 parameters["sone"] = remoteSone
78                 verifyThatImagesAreNotPresent()
79         }
80
81         @Test
82         fun `filter finds all loaded freenet images from the sone itself`() {
83                 templateContext.set("currentSone", sone)
84                 parameters["sone"] = sone
85                 verifyThatImagesArePresent()
86         }
87
88         @Test
89         fun `filter finds images if the remote sone is local`() {
90                 sone.options.loadLinkedImages = MANUALLY_TRUSTED
91                 templateContext.set("currentSone", sone)
92                 `when`(remoteSone.isLocal).thenReturn(true)
93                 parameters["sone"] = remoteSone
94                 verifyThatImagesArePresent()
95         }
96
97         @Test
98         fun `filter does not find images if local sone requires manual trust and remote sone has not trust`() {
99                 sone.options.loadLinkedImages = MANUALLY_TRUSTED
100                 templateContext.set("currentSone", sone)
101                 parameters["sone"] = remoteSone
102                 verifyThatImagesAreNotPresent()
103         }
104
105         @Test
106         fun `filter does not find images if local sone requires manual trust and remote sone has only implicit trust`() {
107                 sone.options.loadLinkedImages = MANUALLY_TRUSTED
108                 templateContext.set("currentSone", sone)
109                 `when`(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(null, 100, null))
110                 parameters["sone"] = remoteSone
111                 verifyThatImagesAreNotPresent()
112         }
113
114         @Test
115         fun `filter does not find images if local sone requires manual trust and remote sone has explicit trust of zero`() {
116                 sone.options.loadLinkedImages = MANUALLY_TRUSTED
117                 templateContext.set("currentSone", sone)
118                 `when`(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(0, null, null))
119                 parameters["sone"] = remoteSone
120                 verifyThatImagesAreNotPresent()
121         }
122
123         @Test
124         fun `filter finds images if local sone requires manual trust and remote sone has explicit trust of one`() {
125                 sone.options.loadLinkedImages = MANUALLY_TRUSTED
126                 templateContext.set("currentSone", sone)
127                 `when`(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
128                 parameters["sone"] = remoteSone
129                 verifyThatImagesArePresent()
130         }
131
132         @Test
133         fun `filter does not find images if local sone requires following and remote sone is not followed`() {
134             sone.options.loadLinkedImages = FOLLOWED
135                 templateContext["currentSone"] = sone
136                 parameters["sone"] = remoteSone
137                 verifyThatImagesAreNotPresent()
138         }
139
140         @Test
141         fun `filter finds images if local sone requires following and remote sone is followed`() {
142             sone.options.loadLinkedImages = FOLLOWED
143                 `when`(sone.hasFriend("remote-id")).thenReturn(true)
144                 templateContext["currentSone"] = sone
145                 parameters["sone"] = remoteSone
146                 verifyThatImagesArePresent()
147         }
148
149         @Test
150         fun `filter finds images if local sone requires following and remote sone is the same as the local sone`() {
151             sone.options.loadLinkedImages = FOLLOWED
152                 templateContext["currentSone"] = sone
153                 parameters["sone"] = sone
154                 verifyThatImagesArePresent()
155         }
156
157         @Test
158         fun `filter finds images if following is required and remote sone is a local sone`() {
159                 sone.options.loadLinkedImages = FOLLOWED
160                 templateContext["currentSone"] = sone
161                 `when`(remoteSone.isLocal).thenReturn(true)
162                 parameters["sone"] = remoteSone
163                 verifyThatImagesArePresent()
164         }
165
166         @Test
167         fun `filter does not find images if any trust is required and remote sone does not have any trust`() {
168             sone.options.loadLinkedImages = TRUSTED
169                 templateContext["currentSone"] = sone
170                 parameters["sone"] = remoteSone
171                 verifyThatImagesAreNotPresent()
172         }
173
174         @Test
175         fun `filter does not find images if any trust is required and remote sone has implicit trust of zero`() {
176             sone.options.loadLinkedImages = TRUSTED
177                 templateContext["currentSone"] = sone
178                 `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 0, null))
179                 parameters["sone"] = remoteSone
180                 verifyThatImagesAreNotPresent()
181         }
182
183         @Test
184         fun `filter finds images if any trust is required and remote sone has implicit trust of one`() {
185             sone.options.loadLinkedImages = TRUSTED
186                 templateContext["currentSone"] = sone
187                 `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 1, null))
188                 parameters["sone"] = remoteSone
189                 verifyThatImagesArePresent()
190         }
191
192         @Test
193         fun `filter does not find images if any trust is required and remote sone has explicit trust of zero but implicit trust of one`() {
194                 sone.options.loadLinkedImages = TRUSTED
195                 templateContext["currentSone"] = sone
196                 `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(0, 1, null))
197                 parameters["sone"] = remoteSone
198                 verifyThatImagesAreNotPresent()
199         }
200
201         @Test
202         fun `filter finds images if any trust is required and remote sone has explicit trust of one but no implicit trust`() {
203                 sone.options.loadLinkedImages = TRUSTED
204                 templateContext["currentSone"] = sone
205                 `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
206                 parameters["sone"] = remoteSone
207                 verifyThatImagesArePresent()
208         }
209
210         @Test
211         fun `filter finds images if any trust is required and remote sone is a local sone`() {
212                 sone.options.loadLinkedImages = TRUSTED
213                 templateContext["currentSone"] = sone
214                 `when`(remoteSone.isLocal).thenReturn(true)
215                 parameters["sone"] = remoteSone
216                 verifyThatImagesArePresent()
217         }
218
219         @Test
220         fun `filter finds images if no trust is required`() {
221             sone.options.loadLinkedImages = ALWAYS
222                 templateContext["currentSone"] = sone
223                 parameters["sone"] = remoteSone
224                 verifyThatImagesArePresent()
225         }
226
227         private fun verifyThatImagesArePresent() {
228                 val loadedImages = filter.format(templateContext, parts, parameters)
229                 assertThat(loadedImages, contains<LinkedElement>(
230                                 LinkedElement("KSK@loading.png", failed = false, loading = true),
231                                 LinkedElement("KSK@link.png", failed = false, loading = false)
232                 ))
233         }
234
235         private fun verifyThatImagesAreNotPresent() {
236                 assertThat(filter.format(templateContext, parts, parameters), emptyIterable())
237         }
238
239         private fun createSone(id: String = "sone-id"): Sone {
240                 val sone = mock<Sone>()
241                 `when`(sone.id).thenReturn(id)
242                 `when`(sone.options).thenReturn(DefaultSoneOptions())
243                 `when`(sone.identity).thenReturn(mock<OwnIdentity>())
244                 return sone
245         }
246
247 }