🔀 Merge next
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / page / FreenetTemplatePageTest.kt
1 package net.pterodactylus.sone.web.page
2
3 import net.pterodactylus.sone.main.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.util.web.*
6 import net.pterodactylus.util.web.Method.*
7 import org.hamcrest.MatcherAssert.*
8 import org.hamcrest.Matchers.*
9 import org.junit.*
10 import org.mockito.Mockito.*
11 import java.io.*
12 import java.nio.charset.StandardCharsets.*
13
14 class FreenetTemplatePageTest {
15
16         private val templateRenderer = deepMock<TemplateRenderer>()
17         private val loaders = mock<Loaders>()
18         private val page = TestPage(templateRenderer, loaders)
19
20         @Test
21         fun `path is exposed correctly`() {
22                 assertThat(page.path, equalTo("/test/path"))
23         }
24
25         @Test
26         fun `getPageTitle() default implementation returns empty string`() {
27                 assertThat(page.getPageTitle(mock()), equalTo(""))
28         }
29
30         @Test
31         fun `isPrefixPage() default implementation returns false`() {
32                 assertThat(page.isPrefixPage, equalTo(false))
33         }
34
35         @Test
36         fun `getStylesheets() default implementation returns empty collection`() {
37                 assertThat(page.styleSheets, empty())
38         }
39
40         @Test
41         fun `getShortcutIcon() default implementation returns null`() {
42                 assertThat(page.shortcutIcon, nullValue())
43         }
44
45         @Test
46         fun `getRedirectTarget() default implementation returns null`() {
47                 assertThat(page.getRedirectTarget(mock()), nullValue())
48         }
49
50         @Test
51         fun `getAdditionalLinkNodes() default implementation returns empty collection`() {
52                 assertThat(page.getAdditionalLinkNodes(mock()), empty())
53         }
54
55         @Test
56         fun `isFullAccessOnly() default implementation returns false`() {
57                 assertThat(page.isFullAccessOnly, equalTo(false))
58         }
59
60         @Test
61         fun `isLinkExcepted() default implementation returns false`() {
62                 assertThat(page.isLinkExcepted(mock()), equalTo(false))
63         }
64
65         @Test
66         fun `isEnabled() returns true if full access only is false`() {
67                 assertThat(page.isEnabled(mock()), equalTo(true))
68         }
69
70         @Test
71         fun `isEnabled() returns false if full access only is true`() {
72                 val page = object : TestPage(templateRenderer, loaders) {
73                         override val isFullAccessOnly = true
74                 }
75                 assertThat(page.isEnabled(mock()), equalTo(false))
76         }
77
78         @Test
79         fun `page with redirect target throws redirect exception on handleRequest`() {
80                 val page = object : TestPage(templateRenderer, loaders) {
81                         override fun getRedirectTarget(request: FreenetRequest) = "foo"
82                 }
83                 val request = mock<FreenetRequest>()
84                 val response = mock<Response>()
85                 val pageResponse = page.handleRequest(request, response)
86                 assertThat(pageResponse.statusCode, anyOf(equalTo(302), equalTo(307)))
87                 assertThat(pageResponse.headers, contains(hasHeader("location", "foo")))
88         }
89
90         @Test
91         fun `page with full access only returns unauthorized on handleRequest with non-full access request`() {
92                 val page = object : TestPage(templateRenderer, loaders) {
93                         override val isFullAccessOnly = true
94                 }
95                 val request = deepMock<FreenetRequest>()
96                 val response = Response(null)
97                 val pageResponse = page.handleRequest(request, response)
98                 assertThat(pageResponse.statusCode, equalTo(401))
99         }
100
101         @Test
102         fun `page redirects on POST without form password`() {
103                 val request = deepMock<FreenetRequest>().apply {
104                         whenever(httpRequest.getPartAsStringFailsafe(any(), anyInt())).thenReturn("")
105                         whenever(method).thenReturn(POST)
106                 }
107                 val response = Response(null)
108                 val pageResponse = page.handleRequest(request, response)
109                 assertThat(pageResponse.statusCode, anyOf(equalTo(302), equalTo(307)))
110                 assertThat(pageResponse.headers, contains(hasHeader("location", "invalid-form-password")))
111         }
112
113         @Test
114         fun `page redirects on POST with invalid password`() {
115                 val request = deepMock<FreenetRequest>().apply {
116                         whenever(httpRequest.getPartAsStringFailsafe(any(), anyInt())).thenReturn("invalid")
117                         whenever(method).thenReturn(POST)
118                 }
119                 val response = Response(null)
120                 val pageResponse = page.handleRequest(request, response)
121                 assertThat(pageResponse.statusCode, anyOf(equalTo(302), equalTo(307)))
122                 assertThat(pageResponse.headers, contains(hasHeader("location", "invalid-form-password")))
123         }
124
125         @Test
126         @Dirty
127         fun `freenet template page creates page with correct title`() {
128                 val page = object : TestPage(templateRenderer, loaders) {
129                         override fun getPageTitle(request: FreenetRequest) = "page title"
130                 }
131                 val request = deepMock<FreenetRequest>()
132                 val pageMakerInteractionFactory = deepMock<PageMakerInteractionFactory>()
133                 whenever(pageMakerInteractionFactory.createPageMaker(request.toadletContext, "page title").renderPage()).thenReturn("<page>")
134                 setField(page, "pageMakerInteractionFactory", pageMakerInteractionFactory)
135                 val response = page.handleRequest(request, Response(ByteArrayOutputStream()))
136                 assertThat(response.statusCode, equalTo(200))
137                 assertThat((response.content as ByteArrayOutputStream).toString(UTF_8.name()), equalTo("<page>"))
138         }
139
140         @Test
141         fun `template from annotation is loaded`() {
142                 verify(loaders).loadTemplate("template-path")
143         }
144
145         @TemplatePath("template-path")
146         @ToadletPath("/test/path")
147         private open class TestPage(templateRenderer: TemplateRenderer, loaders: Loaders) : FreenetTemplatePage(templateRenderer, loaders, "invalid-form-password")
148
149 }