def7410e20c6420d54fab7cf91516896d89b2fb1
[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.template.*
6 import net.pterodactylus.util.web.*
7 import net.pterodactylus.util.web.Method.*
8 import org.hamcrest.MatcherAssert.*
9 import org.hamcrest.Matchers.*
10 import org.junit.*
11 import org.mockito.Mockito.*
12
13 class FreenetTemplatePageTest {
14
15         private val templateContextFactory = deepMock<TemplateContextFactory>()
16         private val loaders = mock<Loaders>()
17         private val template = mock<Template>()
18         private val page = FreenetTemplatePage("/test/path", templateContextFactory, loaders, template, "invalid-form-password")
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 : FreenetTemplatePage("/test/path", templateContextFactory, loaders, template, "invalid-form-password") {
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 : FreenetTemplatePage("/test/path", templateContextFactory, loaders, template, "invalid-form-password") {
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 : FreenetTemplatePage("/test/path", templateContextFactory, loaders, template, "invalid-form-password") {
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         fun `template from annotation is loaded`() {
127                 val template = deepMock<Template>()
128                 whenever(loaders.loadTemplate("template-path")).thenReturn(template)
129                 TestPage(templateContextFactory, loaders)
130                 verify(loaders).loadTemplate("template-path")
131         }
132
133         @TemplatePath("template-path")
134         private class TestPage(templateContextFactory: TemplateContextFactory, loaders: Loaders) : FreenetTemplatePage("/", templateContextFactory, loaders, Template(), "") {
135                 override fun getPath() = ""
136                 override fun isPrefixPage() = false
137                 override fun handleRequest(request: FreenetRequest, response: Response) = response
138         }
139
140 }