Add test for DI constructability of TrustAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / JsonPageTest.kt
1 package net.pterodactylus.sone.web.ajax
2
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
7 import org.junit.Test
8
9 /**
10  * Base class for tests for any [JsonPage] implementations.
11  */
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() {
17
18         protected open val page: JsonPage by lazy { pageSupplier(webInterface) }
19         protected val json by lazy {
20                 page.createJsonObject(freenetRequest)
21         }
22
23         private val JsonReturnObject.error get() = (this as? JsonErrorReturnObject)?.error
24
25         protected fun assertThatJsonIsSuccessful() {
26                 assertThat(json.isSuccess, equalTo(true))
27         }
28
29         protected fun assertThatJsonFailed(error: String? = null) {
30                 assertThat(json.isSuccess, equalTo(false))
31                 error?.run { assertThat(json.error, equalTo(this)) }
32         }
33
34         @Test
35         fun `page returns correct path`() {
36                 assertThat(page.path, equalTo(expectedPath))
37         }
38
39         @Test
40         fun `page needs form password`() {
41                 assertThat(page.needsFormPassword, equalTo(needsFormPassword))
42         }
43
44         @Test
45         fun `page requires login`() {
46                 assertThat(page.requiresLogin, equalTo(requiresLogin))
47         }
48
49 }