1 package net.pterodactylus.sone.web.ajax
3 import net.pterodactylus.sone.test.mock
4 import net.pterodactylus.sone.web.WebInterface
5 import org.hamcrest.MatcherAssert.assertThat
6 import org.hamcrest.Matchers.equalTo
10 * Base class for tests for any [JsonPage] implementations.
12 abstract class JsonPageTest(
13 private val expectedPath: String,
14 private val requiresLogin: Boolean = true,
15 private val needsFormPassword: Boolean = true,
16 pageSupplier: (WebInterface) -> JsonPage = { mock() }) : TestObjects() {
18 protected open val page: JsonPage by lazy { pageSupplier(webInterface) }
19 protected val json by lazy {
20 page.createJsonObject(freenetRequest)
23 private val JsonReturnObject.error get() = (this as? JsonErrorReturnObject)?.error
25 protected fun assertThatJsonIsSuccessful() {
26 assertThat(json.isSuccess, equalTo(true))
29 protected fun assertThatJsonFailed(error: String? = null) {
30 assertThat(json.isSuccess, equalTo(false))
31 error?.run { assertThat(json.error, equalTo(this)) }
35 fun `page returns correct path`() {
36 assertThat(page.path, equalTo(expectedPath))
40 fun `page needs form password`() {
41 assertThat(page.needsFormPassword, equalTo(needsFormPassword))
45 fun `page requires login`() {
46 assertThat(page.requiresLogin, equalTo(requiresLogin))