✅ Add more tests for FreenetTemplatePage
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Apr 2019 21:02:05 +0000 (23:02 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Apr 2019 23:06:46 +0000 (01:06 +0200)
src/test/java/net/pterodactylus/sone/test/Matchers.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/page/FreenetTemplatePageTest.kt

diff --git a/src/test/java/net/pterodactylus/sone/test/Matchers.kt b/src/test/java/net/pterodactylus/sone/test/Matchers.kt
new file mode 100644 (file)
index 0000000..c084d35
--- /dev/null
@@ -0,0 +1,21 @@
+package net.pterodactylus.sone.test
+
+import net.pterodactylus.util.web.*
+import org.hamcrest.*
+
+fun hasHeader(name: String, value: String) = object : TypeSafeDiagnosingMatcher<Header>() {
+       override fun matchesSafely(item: Header, mismatchDescription: Description) =
+                       compare(item.name, { it.equals(name, ignoreCase = true) }) { mismatchDescription.appendText("name is ").appendValue(it) }
+                                       ?: compare(item.hasValue(value), { it }) { mismatchDescription.appendText("does not have value ").appendValue(value) }
+                                       ?: true
+
+       override fun describeTo(description: Description) {
+               description.appendText("name is ").appendValue(name)
+                               .appendText(", value is ").appendValue(value)
+       }
+}
+
+fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) =
+               false.takeUnless { comparison(value) }
+                               ?.also { onError(value) }
+
index f08d3ae..bfc2581 100644 (file)
@@ -1,69 +1,64 @@
 package net.pterodactylus.sone.web.page
 
 import net.pterodactylus.sone.test.*
+import net.pterodactylus.util.web.*
+import net.pterodactylus.util.web.Method.*
 import org.hamcrest.MatcherAssert.*
 import org.hamcrest.Matchers.*
 import org.junit.*
+import org.mockito.ArgumentMatchers.*
 
 class FreenetTemplatePageTest {
 
+       private val page = FreenetTemplatePage("/test/path", null, null, "invalid-form-password")
+
        @Test
        fun `path is exposed correctly`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.path, equalTo("/test/path"))
        }
 
        @Test
        fun `getPageTitle() default implementation returns null`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.getPageTitle(mock()), nullValue())
        }
 
        @Test
        fun `isPrefixPage() default implementation returns false`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.isPrefixPage, equalTo(false))
        }
 
        @Test
        fun `getStylesheets() default implementation returns empty collection`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.styleSheets, empty())
        }
 
        @Test
        fun `getShortcutIcon() default implementation returns null`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.shortcutIcon, nullValue())
        }
 
        @Test
        fun `getRedirectTarget() default implementation returns null`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.getRedirectTarget(mock()), nullValue())
        }
 
        @Test
        fun `getAdditionalLinkNodes() default implementation returns empty collection`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.getAdditionalLinkNodes(mock()), empty())
        }
 
        @Test
        fun `isFullAccessOnly() default implementation returns false`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.isFullAccessOnly, equalTo(false))
        }
 
        @Test
        fun `isLinkExcepted() default implementation returns false`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.isLinkExcepted(mock()), equalTo(false))
        }
 
        @Test
        fun `isEnabled() returns true if full access only is false`() {
-               val page = FreenetTemplatePage("/test/path", null, null, null)
                assertThat(page.isEnabled(mock()), equalTo(true))
        }
 
@@ -75,4 +70,51 @@ class FreenetTemplatePageTest {
                assertThat(page.isEnabled(mock()), equalTo(false))
        }
 
+       @Test
+       fun `page with redirect target throws redirect exception on handleRequest`() {
+               val page = object : FreenetTemplatePage("/test/path", null, null, null) {
+                       override fun getRedirectTarget(request: FreenetRequest?) = "foo"
+               }
+               val request = mock<FreenetRequest>()
+               val response = mock<Response>()
+               val pageResponse = page.handleRequest(request, response)
+               assertThat(pageResponse.statusCode, anyOf(equalTo(302), equalTo(307)))
+               assertThat(pageResponse.headers, contains(hasHeader("location", "foo")))
+       }
+
+       @Test
+       fun `page with full access only returns unauthorized on handleRequest with non-full access request`() {
+               val page = object : FreenetTemplatePage("/test/path", null, null, null) {
+                       override fun isFullAccessOnly() = true
+               }
+               val request = deepMock<FreenetRequest>()
+               val response = Response(null)
+               val pageResponse = page.handleRequest(request, response)
+               assertThat(pageResponse.statusCode, equalTo(401))
+       }
+
+       @Test
+       fun `page redirects on POST without form password`() {
+               val request = deepMock<FreenetRequest>().apply {
+                       whenever(httpRequest.getPartAsStringFailsafe(any(), anyInt())).thenReturn("")
+                       whenever(method).thenReturn(POST)
+               }
+               val response = Response(null)
+               val pageResponse = page.handleRequest(request, response)
+               assertThat(pageResponse.statusCode, anyOf(equalTo(302), equalTo(307)))
+               assertThat(pageResponse.headers, contains(hasHeader("location", "invalid-form-password")))
+       }
+
+       @Test
+       fun `page redirects on POST with invalid password`() {
+               val request = deepMock<FreenetRequest>().apply {
+                       whenever(httpRequest.getPartAsStringFailsafe(any(), anyInt())).thenReturn("invalid")
+                       whenever(method).thenReturn(POST)
+               }
+               val response = Response(null)
+               val pageResponse = page.handleRequest(request, response)
+               assertThat(pageResponse.statusCode, anyOf(equalTo(302), equalTo(307)))
+               assertThat(pageResponse.headers, contains(hasHeader("location", "invalid-form-password")))
+       }
+
 }