From: David ‘Bombe’ Roden Date: Thu, 31 Aug 2017 18:16:42 +0000 (+0200) Subject: Move recurring tests for ajax pages to test base class X-Git-Tag: 0.9.7^2~102 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a57c17c9a431eeb5925cc8a4e7500c53c9b3f412;p=Sone.git Move recurring tests for ajax pages to test base class --- diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.kt index 1d26c71..d6ade8b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.kt @@ -11,17 +11,7 @@ import org.mockito.Mockito.verify /** * Unit test for [BookmarkAjaxPage]. */ -class BookmarkAjaxPageTest : JsonPageTest(::BookmarkAjaxPage) { - - @Test - fun `page returns correct path`() { - assertThat(page.path, equalTo("bookmark.ajax")) - } - - @Test - fun `page does not require login`() { - assertThat(page.requiresLogin(), equalTo(false)) - } +class BookmarkAjaxPageTest : JsonPageTest("bookmark.ajax", requiresLogin = false, pageSupplier = ::BookmarkAjaxPage) { @Test fun `missing post ID results in invalid id response`() { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt index 0588b6a..b153093 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt @@ -14,22 +14,7 @@ import org.junit.Test /** * Unit test for [CreatePostAjaxPage]. */ -class CreatePostAjaxPageTest : JsonPageTest(::CreatePostAjaxPage) { - - @Test - fun `page returns correct path`() { - assertThat(page.path, equalTo("createPost.ajax")) - } - - @Test - fun `page needs form password`() { - assertThat(page.needsFormPassword(), equalTo(true)) - } - - @Test - fun `page requires login`() { - assertThat(page.requiresLogin(), equalTo(true)) - } +class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::CreatePostAjaxPage) { @Test fun `page requires a current sone`() { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt index 9d9008b..ff48460 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPageTest.kt @@ -12,22 +12,7 @@ import org.junit.Test /** * Unit test for [CreateReplyAjaxPage]. */ -class CreateReplyAjaxPageTest : JsonPageTest(::CreateReplyAjaxPage) { - - @Test - fun `page returns correct path`() { - assertThat(page.path, equalTo("createReply.ajax")) - } - - @Test - fun `page needs form password`() { - assertThat(page.needsFormPassword(), equalTo(true)) - } - - @Test - fun `page requires login`() { - assertThat(page.requiresLogin(), equalTo(true)) - } +class CreateReplyAjaxPageTest : JsonPageTest("createReply.ajax", pageSupplier = ::CreateReplyAjaxPage) { @Test fun `invalid post ID results in error message`() { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt index ea3be1a..92ec1d4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetLinkedElementAjaxPageTest.kt @@ -16,27 +16,12 @@ import org.mockito.ArgumentMatchers /** * Unit test for [GetLinkedElementAjaxPage]. */ -class GetLinkedElementAjaxPageTest: JsonPageTest() { +class GetLinkedElementAjaxPageTest: JsonPageTest("getLinkedElement.ajax", requiresLogin = false, needsFormPassword = false) { private val linkedElementRenderFilter = mock() override var page: JsonPage = GetLinkedElementAjaxPage(webInterface, elementLoader, linkedElementRenderFilter) @Test - fun `page returns correct path`() { - assertThat(page.path, equalTo("getLinkedElement.ajax")) - } - - @Test - fun `page does not require login`() { - assertThat(page.requiresLogin(), equalTo(false)) - } - - @Test - fun `page does not require form password`() { - assertThat(page.needsFormPassword(), equalTo(false)) - } - - @Test fun `only loaded linked elements are returned`() { addRequestParameter("elements", jsonArray("KSK@foo.png", "KSK@foo.jpg", "KSK@foo.html").toString()) addLinkedElement("KSK@foo.png", true, false) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt index 2be420d..58193a9 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt @@ -28,7 +28,7 @@ import java.util.TimeZone /** * Unit test for [GetStatusAjaxPage]. */ -class GetStatusAjaxPageTest: JsonPageTest() { +class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = false, needsFormPassword = false) { private val timeTextConverter = mock() private val l10nFilter = mock() @@ -41,21 +41,6 @@ class GetStatusAjaxPageTest: JsonPageTest() { } @Test - fun `page returns correct path`() { - assertThat(page.path, equalTo("getStatus.ajax")) - } - - @Test - fun `page does not require form password`() { - assertThat(page.needsFormPassword(), equalTo(false)) - } - - @Test - fun `page does not require login`() { - assertThat(page.requiresLogin(), equalTo(false)) - } - - @Test fun `page returns correct attribute “loggedIn” if sone is logged in`() { assertThat(json.get("loggedIn").asText(), equalTo("true")) } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index 87b2939..5d43a1e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -20,7 +20,10 @@ import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.web.Method.GET +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo import org.junit.Before +import org.junit.Test import org.mockito.ArgumentMatchers.anyBoolean import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyString @@ -32,7 +35,11 @@ import javax.naming.SizeLimitExceededException /** * Base class for tests for any [JsonPage] implementations. */ -open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock() }) { +abstract class JsonPageTest( + private val expectedPath: String, + private val requiresLogin: Boolean = true, + private val needsFormPassword: Boolean = true, + pageSupplier: (WebInterface) -> JsonPage = { _ -> mock() }) { protected val webInterface = mock() protected val core = mock() @@ -180,4 +187,19 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock