From: David ‘Bombe’ Roden Date: Fri, 12 Apr 2019 21:02:05 +0000 (+0200) Subject: ✅ Add more tests for FreenetTemplatePage X-Git-Tag: v79^2~61 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=28c3ef5bb54ac66cbbc7b610b21ee4d300180dd6;p=Sone.git ✅ Add more tests for FreenetTemplatePage --- 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 index 0000000..c084d35 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/test/Matchers.kt @@ -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
() { + 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 compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) = + false.takeUnless { comparison(value) } + ?.also { onError(value) } + diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetTemplatePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetTemplatePageTest.kt index f08d3ae..bfc2581 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetTemplatePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/FreenetTemplatePageTest.kt @@ -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() + val response = mock() + 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() + 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().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().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"))) + } + }