🔀 Merge branch 'release/v82'
[Sone.git] / web / PageToadletRegistryTest.kt
1 package net.pterodactylus.sone.web
2
3 import com.google.inject.*
4 import freenet.clients.http.*
5 import net.pterodactylus.sone.main.*
6 import net.pterodactylus.sone.test.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.web.*
9 import org.junit.*
10 import org.junit.rules.*
11 import org.mockito.Mockito.*
12 import kotlin.test.Test
13
14 class PageToadletRegistryTest {
15
16         private val pageMaker = mock<PageMaker>()
17         private val toadletContainer = mock<ToadletContainer>()
18         private val pageToadletFactory = mock<PageToadletFactory>()
19         private val sonePlugin = mock<SonePlugin>()
20
21         private val injector = Guice.createInjector(
22                         PageMaker::class.isProvidedBy(pageMaker),
23                         ToadletContainer::class.isProvidedBy(toadletContainer),
24                         PageToadletFactory::class.isProvidedBy(pageToadletFactory),
25                         SonePlugin::class.isProvidedBy(sonePlugin)
26         )
27
28         @JvmField
29         @Rule
30         val expectedException: ExpectedException = ExpectedException.none()
31         private val pageToadletRegistry = injector.getInstance<PageToadletRegistry>()
32
33         @Test
34         fun `registry adds navigation category to page maker`() {
35                 pageToadletRegistry.registerToadlets()
36                 verify(pageMaker).addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin)
37         }
38
39         private val page = TestPage()
40
41         @Test
42         fun `adding a page without menuname will add it correctly`() {
43                 val toadletWithoutMenuname = createPageToadlet()
44                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadletWithoutMenuname)
45                 pageToadletRegistry.addPage(page)
46                 pageToadletRegistry.registerToadlets()
47                 verify(toadletContainer).register(toadletWithoutMenuname, null, "/Sone/", true, false)
48         }
49
50         @Test
51         fun `adding a page with menuname will add it correctly`() {
52                 val toadletWithMenuname = createPageToadlet("Test")
53                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadletWithMenuname)
54                 pageToadletRegistry.addPage(page)
55                 pageToadletRegistry.registerToadlets()
56                 verify(toadletContainer).register(toadletWithMenuname, "Navigation.Menu.Sone.Name", "/Sone/", true, "Navigation.Menu.Sone.Item.Test.Name", "Navigation.Menu.Sone.Item.Test.Tooltip", false, toadletWithMenuname)
57         }
58
59         @Test
60         fun `adding a page after registering will throw an exception`() {
61                 val toadletWithMenuname = createPageToadlet("Test")
62                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadletWithMenuname)
63                 pageToadletRegistry.registerToadlets()
64                 expectedException.expect(IllegalStateException::class.java)
65                 pageToadletRegistry.addPage(page)
66         }
67
68         @Test
69         fun `unregistering toadlets will remove category link`() {
70                 pageToadletRegistry.unregisterToadlets()
71                 verify(pageMaker).removeNavigationCategory("Navigation.Menu.Sone.Name")
72         }
73
74         @Test
75         fun `unregistering toadlets will unregister them from the container`() {
76                 val toadletWithMenuname = createPageToadlet("Test")
77                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadletWithMenuname)
78                 pageToadletRegistry.addPage(page)
79                 pageToadletRegistry.registerToadlets()
80                 pageToadletRegistry.unregisterToadlets()
81                 verify(toadletContainer).unregister(toadletWithMenuname)
82         }
83
84         @Test
85         fun `adding a debug page will not add it to the container`() {
86                 val toadlet = createPageToadlet()
87                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
88                 pageToadletRegistry.addDebugPage(page)
89                 pageToadletRegistry.registerToadlets()
90                 verify(toadletContainer, never()).register(toadlet, null, "/Sone/", true, false)
91         }
92
93         @Test
94         fun `adding a debug page and activating debug mode will add it to the container`() {
95                 val toadlet = createPageToadlet()
96                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
97                 pageToadletRegistry.addDebugPage(page)
98                 pageToadletRegistry.registerToadlets()
99                 pageToadletRegistry.activateDebugMode()
100                 verify(toadletContainer).register(toadlet, null, "/Sone/", true, false)
101         }
102
103         @Test
104         fun `adding a debug page and activating debug mode twice will add it to the container once`() {
105                 val toadlet = createPageToadlet()
106                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
107                 pageToadletRegistry.addDebugPage(page)
108                 pageToadletRegistry.registerToadlets()
109                 pageToadletRegistry.activateDebugMode()
110                 pageToadletRegistry.activateDebugMode()
111                 verify(toadletContainer, times(1)).register(toadlet, null, "/Sone/", true, false)
112         }
113
114         @Test
115         fun `debug pages are ungegistered from the container`() {
116                 val toadlet = createPageToadlet()
117                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
118                 pageToadletRegistry.addDebugPage(page)
119                 pageToadletRegistry.registerToadlets()
120                 pageToadletRegistry.activateDebugMode()
121                 pageToadletRegistry.unregisterToadlets()
122                 verify(toadletContainer).unregister(toadlet)
123         }
124
125         @Test
126         fun `inactive debug pages are not ungegistered from the container`() {
127                 val toadlet = createPageToadlet()
128                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
129                 pageToadletRegistry.addDebugPage(page)
130                 pageToadletRegistry.registerToadlets()
131                 pageToadletRegistry.unregisterToadlets()
132                 verify(toadletContainer, never()).unregister(toadlet)
133         }
134
135         @Test
136         fun `debug page can not be added after registering`() {
137                 val toadlet = createPageToadlet()
138                 whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet)
139                 pageToadletRegistry.registerToadlets()
140                 expectedException.expect(IllegalStateException::class.java)
141                 pageToadletRegistry.addDebugPage(page)
142         }
143
144         private fun createPageToadlet(menuName: String? = null) =
145                         mock<PageToadlet>().apply {
146                                 whenever(this.path()).thenReturn("/Sone/")
147                                 whenever(this.menuName).thenReturn(menuName)
148                         }
149
150         private class TestPage : Page<FreenetRequest> {
151                 override fun getPath() = ""
152                 override fun isPrefixPage() = false
153                 override fun handleRequest(freenetRequest: FreenetRequest, response: Response) = response
154         }
155
156 }