1 package net.pterodactylus.sone.web
3 import com.google.inject.Guice.*
4 import freenet.client.*
5 import freenet.support.api.*
6 import net.pterodactylus.sone.core.*
7 import net.pterodactylus.sone.data.*
8 import net.pterodactylus.sone.database.*
9 import net.pterodactylus.sone.freenet.*
10 import net.pterodactylus.sone.freenet.wot.*
11 import net.pterodactylus.sone.main.*
12 import net.pterodactylus.sone.template.*
13 import net.pterodactylus.sone.test.*
14 import net.pterodactylus.sone.text.*
15 import net.pterodactylus.sone.web.page.*
16 import net.pterodactylus.util.notify.*
17 import net.pterodactylus.util.template.*
18 import net.pterodactylus.util.web.*
19 import org.hamcrest.MatcherAssert.*
20 import org.hamcrest.Matchers.*
24 class WebInterfaceModuleTest {
26 private val webInterfaceModule = WebInterfaceModule()
27 private val loaders = mock<Loaders>()
28 private val translation = object : Translation {
29 override val currentLocale = Locale.ENGLISH
30 override fun translate(key: String) = if (key == "View.Sone.Text.UnknownDate") "unknown" else key
32 private val additionalModules = arrayOf(
33 Core::class.isProvidedByMock(),
34 SoneProvider::class.isProvidedByMock(),
35 Translation::class.isProvidedBy(translation),
36 SoneTextParser::class.isProvidedByMock(),
37 ElementLoader::class.isProvidedByMock(),
38 Loaders::class.isProvidedBy(loaders),
39 HighLevelSimpleClient::class.isProvidedByMock()
41 private val injector = createInjector(webInterfaceModule, *additionalModules)!!
42 private val templateContext by lazy { injector.getInstance<TemplateContextFactory>().createTemplateContext()!! }
45 fun `template context factory creates template with reflection accessor for objects`() {
46 verifyAccessor<Any, ReflectionAccessor>()
50 fun `template context factory creates template with collection accessor for collections`() {
51 verifyAccessor<Collection<*>, CollectionAccessor>()
55 fun `template context contains sone accessor for sones`() {
56 verifyAccessor<Sone, SoneAccessor>()
60 fun `template context contains post accessor for posts`() {
61 verifyAccessor<Post, PostAccessor>()
65 fun `template context contains reply accessor for replies`() {
66 verifyAccessor<Reply<*>, ReplyAccessor>()
70 fun `template context contains album accessor for albums`() {
71 verifyAccessor<Album, AlbumAccessor>()
75 fun `template context contains image accessor for images`() {
76 verifyAccessor<Image, ImageAccessor>()
80 fun `template context contains identity accessor for identities`() {
81 verifyAccessor<Identity, IdentityAccessor>()
85 fun `template context contains trust accessor for trusts`() {
86 verifyAccessor<Trust, TrustAccessor>()
90 fun `template context contains http request accessor for http requests`() {
91 verifyAccessor<HTTPRequest, HttpRequestAccessor>()
95 fun `template context contains profile accessor for profiles`() {
96 verifyAccessor<Profile, ProfileAccessor>()
99 private inline fun <reified O, reified A : Accessor> verifyAccessor() {
100 assertThat(templateContext.getAccessor(O::class.java), instanceOf(A::class.java))
104 fun `template context contains date filter`() {
105 verifyFilter<DateFilter>("date")
109 fun `template context contains html filter`() {
110 verifyFilter<HtmlFilter>("html")
114 fun `template context contains replace filter`() {
115 verifyFilter<ReplaceFilter>("replace")
119 fun `template context contains store filter`() {
120 verifyFilter<StoreFilter>("store")
124 fun `template context contains l10n filter`() {
125 verifyFilter<L10nFilter>("l10n")
129 fun `template context contains substring filter`() {
130 verifyFilter<SubstringFilter>("substring")
134 fun `template context contains xml filter`() {
135 verifyFilter<XmlFilter>("xml")
139 fun `template context contains change filter`() {
140 verifyFilter<RequestChangeFilter>("change")
144 fun `template context contains match filter`() {
145 verifyFilter<MatchFilter>("match")
149 fun `template context contains css filter`() {
150 verifyFilter<CssClassNameFilter>("css")
154 fun `template context contains js filter`() {
155 verifyFilter<JavascriptFilter>("js")
159 fun `template context contains parser filter`() {
160 verifyFilter<ParserFilter>("parse")
164 fun `template context contains shorten filter`() {
165 verifyFilter<ShortenFilter>("shorten")
169 fun `template context contains render filter`() {
170 verifyFilter<RenderFilter>("render")
174 fun `template context contains linked elements filter`() {
175 verifyFilter<LinkedElementsFilter>("linked-elements")
179 fun `template context contains linked elements render filter`() {
180 verifyFilter<LinkedElementRenderFilter>("render-linked-element")
184 fun `template context contains reparse filter`() {
185 verifyFilter<ReparseFilter>("reparse")
189 fun `template context contains unknown date filter`() {
190 verifyFilter<UnknownDateFilter>("unknown")
194 fun `unknown date filter uses correct l10n key`() {
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")
244 fun `template context histogram renderer`() {
245 verifyFilter<HistogramRenderer>("render-histogram")
248 private inline fun <reified F : Filter> verifyFilter(name: String) {
249 assertThat(getFilter(name), instanceOf(F::class.java))
252 private fun getFilter(name: String): Filter? = templateContext.getFilter(name)
255 fun `template context factory is created as singleton`() {
256 injector.verifySingletonInstance<TemplateContextFactory>()
260 fun `template from classpath is returned`() {
261 val template = Template()
262 templateContext["testTemplate"] = template
263 assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template))
267 fun `template from loaders’ provider is returned`() {
268 val template = Template()
269 whenever(loaders.templateProvider).thenReturn(TemplateProvider { _, templateName ->
270 template.takeIf { templateName == "testTemplate" }
272 assertThat(templateContext.getTemplate("testTemplate"), sameInstance(template))
276 fun `page toadlet factory is created with correct prefix`() {
277 val page = mock<Page<FreenetRequest>>()
278 assertThat(injector.getInstance<PageToadletFactory>().createPageToadlet(page).path(), startsWith("/Sone/"))
282 fun `notification manager is created as singleton`() {
283 injector.verifySingletonInstance<NotificationManager>()