1 package net.pterodactylus.sone.web
3 import com.google.inject.Guice.*
4 import freenet.client.*
5 import freenet.clients.http.*
7 import freenet.support.api.*
8 import net.pterodactylus.sone.core.*
9 import net.pterodactylus.sone.data.*
10 import net.pterodactylus.sone.database.*
11 import net.pterodactylus.sone.freenet.*
12 import net.pterodactylus.sone.freenet.wot.*
13 import net.pterodactylus.sone.main.*
14 import net.pterodactylus.sone.template.*
15 import net.pterodactylus.sone.test.*
16 import net.pterodactylus.sone.text.*
17 import net.pterodactylus.sone.web.page.*
18 import net.pterodactylus.util.template.*
19 import net.pterodactylus.util.web.*
20 import org.hamcrest.MatcherAssert.*
21 import org.hamcrest.Matchers.*
23 import kotlin.test.Test
25 class WebInterfaceModuleTest {
27 private val webInterfaceModule = WebInterfaceModule()
28 private val l10n = mock<BaseL10n>()
29 private val loaders = mock<Loaders>()
30 private val additionalModules = arrayOf(
31 Core::class.isProvidedByMock(),
32 SoneProvider::class.isProvidedByMock(),
33 BaseL10n::class.isProvidedBy(l10n),
34 SoneTextParser::class.isProvidedByMock(),
35 ElementLoader::class.isProvidedByMock(),
36 Loaders::class.isProvidedBy(loaders),
37 HighLevelSimpleClient::class.isProvidedByMock(),
38 SessionManager::class.isProvidedByMock()
40 private val injector = createInjector(webInterfaceModule, *additionalModules)!!
41 private val templateContext by lazy { injector.getInstance<TemplateContextFactory>().createTemplateContext()!! }
44 fun `template context factory creates template with reflection accessor for objects`() {
45 verifyAccessor<Any, ReflectionAccessor>()
49 fun `template context factory creates template with collection accessor for collections`() {
50 verifyAccessor<Collection<*>, CollectionAccessor>()
54 fun `template context contains sone accessor for sones`() {
55 verifyAccessor<Sone, SoneAccessor>()
59 fun `template context contains post accessor for posts`() {
60 verifyAccessor<Post, PostAccessor>()
64 fun `template context contains reply accessor for replies`() {
65 verifyAccessor<Reply<*>, ReplyAccessor>()
69 fun `template context contains album accessor for albums`() {
70 verifyAccessor<Album, AlbumAccessor>()
74 fun `template context contains image accessor for images`() {
75 verifyAccessor<Image, ImageAccessor>()
79 fun `template context contains identity accessor for identities`() {
80 verifyAccessor<Identity, IdentityAccessor>()
84 fun `template context contains trust accessor for trusts`() {
85 verifyAccessor<Trust, TrustAccessor>()
89 fun `template context contains http request accessor for http requests`() {
90 verifyAccessor<HTTPRequest, HttpRequestAccessor>()
94 fun `template context contains profile accessor for profiles`() {
95 verifyAccessor<Profile, ProfileAccessor>()
98 private inline fun <reified O, reified A : Accessor> verifyAccessor() {
99 assertThat(templateContext.getAccessor(O::class.java), instanceOf(A::class.java))
103 fun `template context contains date filter`() {
104 verifyFilter<DateFilter>("date")
108 fun `template context contains html filter`() {
109 verifyFilter<HtmlFilter>("html")
113 fun `template context contains replace filter`() {
114 verifyFilter<ReplaceFilter>("replace")
118 fun `template context contains store filter`() {
119 verifyFilter<StoreFilter>("store")
123 fun `template context contains l10n filter`() {
124 verifyFilter<L10nFilter>("l10n")
128 fun `template context contains substring filter`() {
129 verifyFilter<SubstringFilter>("substring")
133 fun `template context contains xml filter`() {
134 verifyFilter<XmlFilter>("xml")
138 fun `template context contains change filter`() {
139 verifyFilter<RequestChangeFilter>("change")
143 fun `template context contains match filter`() {
144 verifyFilter<MatchFilter>("match")
148 fun `template context contains css filter`() {
149 verifyFilter<CssClassNameFilter>("css")
153 fun `template context contains js filter`() {
154 verifyFilter<JavascriptFilter>("js")
158 fun `template context contains parser filter`() {
159 verifyFilter<ParserFilter>("parse")
163 fun `template context contains shorten filter`() {
164 verifyFilter<ShortenFilter>("shorten")
168 fun `template context contains render filter`() {
169 verifyFilter<RenderFilter>("render")
173 fun `template context contains linked elements filter`() {
174 verifyFilter<LinkedElementsFilter>("linked-elements")
178 fun `template context contains linked elements render filter`() {
179 verifyFilter<LinkedElementRenderFilter>("render-linked-element")
183 fun `template context contains reparse filter`() {
184 verifyFilter<ReparseFilter>("reparse")
188 fun `template context contains unknown date filter`() {
189 verifyFilter<UnknownDateFilter>("unknown")
193 fun `unknown date filter uses correct l10n key`() {
194 whenever(l10n.getString("View.Sone.Text.UnknownDate")).thenReturn("unknown")
195 assertThat(getFilter("unknown")!!.format(null, 0L, emptyMap()), equalTo<Any>("unknown"))
199 fun `template context contains format filter`() {
200 verifyFilter<FormatFilter>("format")
204 fun `template context contains duration format filter`() {
205 verifyFilter<DurationFormatFilter>("duration")
209 fun `template context contains collection sort filter`() {
210 verifyFilter<CollectionSortFilter>("sort")
214 fun `template context contains image link filter`() {
215 verifyFilter<ImageLinkFilter>("image-link")
219 fun `template context contains reply group filter`() {
220 verifyFilter<ReplyGroupFilter>("replyGroup")
224 fun `template context contains contains filter`() {
225 verifyFilter<ContainsFilter>("in")
229 fun `template context unique elements filter`() {
230 verifyFilter<UniqueElementFilter>("unique")
234 fun `template context mod filter`() {
235 verifyFilter<ModFilter>("mod")
239 fun `template context pagination filter`() {
240 verifyFilter<PaginationFilter>("paginate")
243 private inline fun <reified F : Filter> verifyFilter(name: String) {
244 assertThat(getFilter(name), instanceOf(F::class.java))
247 private fun getFilter(name: String): Filter? = templateContext.getFilter(name)
250 fun `template context factory is created as singleton`() {
251 val factory1 = injector.getInstance<TemplateContextFactory>()
252 val factory2 = injector.getInstance<TemplateContextFactory>()
253 assertThat(factory1, sameInstance(factory2))
257 fun `template from classpath is returned`() {
258 val template = Template()
259 templateContext["testTemplate"] = template
260 assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template))
264 fun `template from loaders’ provider is returned`() {
265 val template = Template()
266 whenever(loaders.templateProvider).thenReturn(TemplateProvider { _, templateName ->
267 template.takeIf { templateName == "testTemplate" }
269 assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template))
273 fun `page toadlet factory is created with correct prefix`() {
274 val page = mock<Page<FreenetRequest>>()
275 assertThat(injector.getInstance<PageToadletFactory>().createPageToadlet(page).path(), startsWith("/Sone/"))