1 package net.pterodactylus.sone.web.page
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.*
10 import org.mockito.Mockito.*
12 import java.nio.charset.StandardCharsets.*
14 class FreenetTemplatePageTest {
16 private val templateRenderer = deepMock<TemplateRenderer>()
17 private val loaders = mock<Loaders>()
18 private val page = TestPage(templateRenderer, loaders)
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 : TestPage(templateRenderer, loaders) {
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 : TestPage(templateRenderer, loaders) {
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 : TestPage(templateRenderer, loaders) {
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")))
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"
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>"))
141 fun `template from annotation is loaded`() {
142 verify(loaders).loadTemplate("template-path")
145 @TemplatePath("template-path")
146 @ToadletPath("/test/path")
147 private open class TestPage(templateRenderer: TemplateRenderer, loaders: Loaders) : FreenetTemplatePage(templateRenderer, loaders, "invalid-form-password")