🚧 Add no-negative identity filter
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / page / PageToadletFactoryTest.kt
1 package net.pterodactylus.sone.web.page
2
3 import com.google.inject.*
4 import freenet.client.*
5 import net.pterodactylus.sone.test.*
6 import net.pterodactylus.util.web.*
7 import org.hamcrest.MatcherAssert.*
8 import org.hamcrest.Matchers.*
9 import org.junit.*
10
11 private val highLevelSimpleClient = mock<HighLevelSimpleClient>()
12 private const val pathPrefix = "/some/prefix/"
13
14 class PageToadletFactoryTest {
15
16         private val pageToadletFactory = PageToadletFactory(highLevelSimpleClient, pathPrefix)
17
18         @Test
19         fun `page toadlet without menu name is created without menu name`() {
20                 val page = mock<Page<FreenetRequest>>()
21                 val pageToadlet = pageToadletFactory.createPageToadlet(page)
22                 assertThat(pageToadlet.menuName, nullValue())
23         }
24
25         @Test
26         fun `page toadlet with menu name is created with menu name`() {
27                 val page = mock<Page<FreenetRequest>>()
28                 val pageToadlet = pageToadletFactory.createPageToadlet(page, "testName")
29                 assertThat(pageToadlet.menuName, equalTo("testName"))
30         }
31
32         @Test
33         fun `path prefix is handed down correctly`() {
34                 val page = mock<Page<FreenetRequest>>().apply {
35                         whenever(path).thenReturn("path")
36                 }
37                 val pageToadlet = pageToadletFactory.createPageToadlet(page)
38                 assertThat(pageToadlet.path(), equalTo("/some/prefix/path"))
39         }
40
41         @Test
42         fun `menu name is added from annotation when no menu name is given`() {
43                 val page = TestPageWithMenuName()
44                 val pageToadlet = pageToadletFactory.createPageToadlet(page)
45                 assertThat(pageToadlet.menuName, equalTo("testName"))
46         }
47
48         @Test
49         fun `menu name from annotation is ignored when menu name is given`() {
50                 val page = TestPageWithMenuName()
51                 val pageToadlet = pageToadletFactory.createPageToadlet(page, "foo")
52                 assertThat(pageToadlet.menuName, equalTo("foo"))
53         }
54
55         @Test
56         fun `page toadlet factory can be created by guice`() {
57                 val injector = Guice.createInjector(
58                                 HighLevelSimpleClient::class.isProvidedBy(highLevelSimpleClient),
59                                 String::class.withNameIsProvidedBy("/Sone/", "toadletPathPrefix")
60                 )
61             assertThat(injector.getInstance<PageToadletFactory>(), notNullValue())
62         }
63
64 }
65
66 @MenuName("testName")
67 private class TestPageWithMenuName : Page<FreenetRequest> {
68
69         override fun getPath() = ""
70         override fun isPrefixPage() = false
71         override fun handleRequest(request: FreenetRequest, response: Response) = response
72
73 }