1 package net.pterodactylus.sone.web.page
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.*
11 import org.mockito.Mockito.*
13 class FreenetTemplatePageTest {
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")
21 fun `path is exposed correctly`() {
22 assertThat(page.path, equalTo("/test/path"))
26 fun `getPageTitle() default implementation returns empty string`() {
27 assertThat(page.getPageTitle(mock()), equalTo(""))
31 fun `isPrefixPage() default implementation returns false`() {
32 assertThat(page.isPrefixPage, equalTo(false))
36 fun `getStylesheets() default implementation returns empty collection`() {
37 assertThat(page.styleSheets, empty())
41 fun `getShortcutIcon() default implementation returns null`() {
42 assertThat(page.shortcutIcon, nullValue())
46 fun `getRedirectTarget() default implementation returns null`() {
47 assertThat(page.getRedirectTarget(mock()), nullValue())
51 fun `getAdditionalLinkNodes() default implementation returns empty collection`() {
52 assertThat(page.getAdditionalLinkNodes(mock()), empty())
56 fun `isFullAccessOnly() default implementation returns false`() {
57 assertThat(page.isFullAccessOnly, equalTo(false))
61 fun `isLinkExcepted() default implementation returns false`() {
62 assertThat(page.isLinkExcepted(mock()), equalTo(false))
66 fun `isEnabled() returns true if full access only is false`() {
67 assertThat(page.isEnabled(mock()), equalTo(true))
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
75 assertThat(page.isEnabled(mock()), equalTo(false))
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"
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")))
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
95 val request = deepMock<FreenetRequest>()
96 val response = Response(null)
97 val pageResponse = page.handleRequest(request, response)
98 assertThat(pageResponse.statusCode, equalTo(401))
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)
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")))
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)
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")))
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")
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