1 package net.pterodactylus.sone.web
3 import com.google.inject.Guice.*
4 import freenet.client.*
5 import freenet.clients.http.*
6 import freenet.support.api.*
7 import net.pterodactylus.sone.core.*
8 import net.pterodactylus.sone.data.*
9 import net.pterodactylus.sone.database.*
10 import net.pterodactylus.sone.freenet.*
11 import net.pterodactylus.sone.freenet.wot.*
12 import net.pterodactylus.sone.main.*
13 import net.pterodactylus.sone.template.*
14 import net.pterodactylus.sone.test.*
15 import net.pterodactylus.sone.text.*
16 import net.pterodactylus.sone.web.notification.*
17 import net.pterodactylus.sone.web.page.*
18 import net.pterodactylus.util.notify.*
19 import net.pterodactylus.util.template.*
20 import net.pterodactylus.util.web.*
21 import org.hamcrest.MatcherAssert.*
22 import org.hamcrest.Matchers.*
26 class WebInterfaceModuleTest {
28 private val webInterfaceModule = WebInterfaceModule()
29 private val loaders = mock<Loaders>()
30 private val translation = object : Translation {
31 override val currentLocale = Locale.ENGLISH
32 override fun translate(key: String) = if (key == "View.Sone.Text.UnknownDate") "unknown" else key
34 private val additionalModules = arrayOf(
35 Core::class.isProvidedByMock(),
36 SoneProvider::class.isProvidedByMock(),
37 Translation::class.isProvidedBy(translation),
38 SoneTextParser::class.isProvidedByMock(),
39 ElementLoader::class.isProvidedByMock(),
40 Loaders::class.isProvidedBy(loaders),
41 HighLevelSimpleClient::class.isProvidedByMock(),
42 SessionManager::class.isProvidedByMock()
44 private val injector = createInjector(webInterfaceModule, *additionalModules)!!
45 private val templateContext by lazy { injector.getInstance<TemplateContextFactory>().createTemplateContext()!! }
48 fun `template context factory creates template with reflection accessor for objects`() {
49 verifyAccessor<Any, ReflectionAccessor>()
53 fun `template context factory creates template with collection accessor for collections`() {
54 verifyAccessor<Collection<*>, CollectionAccessor>()
58 fun `template context contains sone accessor for sones`() {
59 verifyAccessor<Sone, SoneAccessor>()
63 fun `template context contains post accessor for posts`() {
64 verifyAccessor<Post, PostAccessor>()
68 fun `template context contains reply accessor for replies`() {
69 verifyAccessor<Reply<*>, ReplyAccessor>()
73 fun `template context contains album accessor for albums`() {
74 verifyAccessor<Album, AlbumAccessor>()
78 fun `template context contains image accessor for images`() {
79 verifyAccessor<Image, ImageAccessor>()
83 fun `template context contains identity accessor for identities`() {
84 verifyAccessor<Identity, IdentityAccessor>()
88 fun `template context contains trust accessor for trusts`() {
89 verifyAccessor<Trust, TrustAccessor>()
93 fun `template context contains http request accessor for http requests`() {
94 verifyAccessor<HTTPRequest, HttpRequestAccessor>()
98 fun `template context contains profile accessor for profiles`() {
99 verifyAccessor<Profile, ProfileAccessor>()
102 private inline fun <reified O, reified A : Accessor> verifyAccessor() {
103 assertThat(templateContext.getAccessor(O::class.java), instanceOf(A::class.java))
107 fun `template context contains date filter`() {
108 verifyFilter<DateFilter>("date")
112 fun `template context contains html filter`() {
113 verifyFilter<HtmlFilter>("html")
117 fun `template context contains replace filter`() {
118 verifyFilter<ReplaceFilter>("replace")
122 fun `template context contains store filter`() {
123 verifyFilter<StoreFilter>("store")
127 fun `template context contains l10n filter`() {
128 verifyFilter<L10nFilter>("l10n")
132 fun `template context contains substring filter`() {
133 verifyFilter<SubstringFilter>("substring")
137 fun `template context contains xml filter`() {
138 verifyFilter<XmlFilter>("xml")
142 fun `template context contains change filter`() {
143 verifyFilter<RequestChangeFilter>("change")
147 fun `template context contains match filter`() {
148 verifyFilter<MatchFilter>("match")
152 fun `template context contains css filter`() {
153 verifyFilter<CssClassNameFilter>("css")
157 fun `template context contains js filter`() {
158 verifyFilter<JavascriptFilter>("js")
162 fun `template context contains parser filter`() {
163 verifyFilter<ParserFilter>("parse")
167 fun `template context contains shorten filter`() {
168 verifyFilter<ShortenFilter>("shorten")
172 fun `template context contains render filter`() {
173 verifyFilter<RenderFilter>("render")
177 fun `template context contains linked elements filter`() {
178 verifyFilter<LinkedElementsFilter>("linked-elements")
182 fun `template context contains linked elements render filter`() {
183 verifyFilter<LinkedElementRenderFilter>("render-linked-element")
187 fun `template context contains reparse filter`() {
188 verifyFilter<ReparseFilter>("reparse")
192 fun `template context contains unknown date filter`() {
193 verifyFilter<UnknownDateFilter>("unknown")
197 fun `unknown date filter uses correct l10n key`() {
198 assertThat(getFilter("unknown")!!.format(null, 0L, emptyMap()), equalTo<Any>("unknown"))
202 fun `template context contains format filter`() {
203 verifyFilter<FormatFilter>("format")
207 fun `template context contains duration format filter`() {
208 verifyFilter<DurationFormatFilter>("duration")
212 fun `template context contains collection sort filter`() {
213 verifyFilter<CollectionSortFilter>("sort")
217 fun `template context contains image link filter`() {
218 verifyFilter<ImageLinkFilter>("image-link")
222 fun `template context contains reply group filter`() {
223 verifyFilter<ReplyGroupFilter>("replyGroup")
227 fun `template context contains contains filter`() {
228 verifyFilter<ContainsFilter>("in")
232 fun `template context unique elements filter`() {
233 verifyFilter<UniqueElementFilter>("unique")
237 fun `template context mod filter`() {
238 verifyFilter<ModFilter>("mod")
242 fun `template context pagination filter`() {
243 verifyFilter<PaginationFilter>("paginate")
247 fun `template context histogram renderer`() {
248 verifyFilter<HistogramRenderer>("render-histogram")
251 private inline fun <reified F : Filter> verifyFilter(name: String) {
252 assertThat(getFilter(name), instanceOf(F::class.java))
255 private fun getFilter(name: String): Filter? = templateContext.getFilter(name)
258 fun `template context factory is created as singleton`() {
259 injector.verifySingletonInstance<TemplateContextFactory>()
263 fun `template from classpath is returned`() {
264 val template = Template()
265 templateContext["testTemplate"] = template
266 assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template))
270 fun `template from loaders’ provider is returned`() {
271 val template = Template()
272 whenever(loaders.templateProvider).thenReturn(TemplateProvider { _, templateName ->
273 template.takeIf { templateName == "testTemplate" }
275 assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template))
279 fun `page toadlet factory is created with correct prefix`() {
280 val page = mock<Page<FreenetRequest>>()
281 assertThat(injector.getInstance<PageToadletFactory>().createPageToadlet(page).path(), startsWith("/Sone/"))
285 fun `notification manager is created as singleton`() {
286 injector.verifySingletonInstance<NotificationManager>()