Add test for DI constructability of TrustAjaxPage
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / JsonPageTest.kt
index 558b414..ea882d3 100644 (file)
@@ -13,14 +13,23 @@ abstract class JsonPageTest(
                private val expectedPath: String,
                private val requiresLogin: Boolean = true,
                private val needsFormPassword: Boolean = true,
-               pageSupplier: (WebInterface) -> JsonPage = { mock() }): TestObjects() {
+               pageSupplier: (WebInterface) -> JsonPage = { mock() }) : TestObjects() {
 
        protected open val page: JsonPage by lazy { pageSupplier(webInterface) }
        protected val json by lazy {
                page.createJsonObject(freenetRequest)
        }
 
-       protected val JsonReturnObject.error get() = if (this is JsonErrorReturnObject) this.error else null
+       private val JsonReturnObject.error get() = (this as? JsonErrorReturnObject)?.error
+
+       protected fun assertThatJsonIsSuccessful() {
+               assertThat(json.isSuccess, equalTo(true))
+       }
+
+       protected fun assertThatJsonFailed(error: String? = null) {
+               assertThat(json.isSuccess, equalTo(false))
+               error?.run { assertThat(json.error, equalTo(this)) }
+       }
 
        @Test
        fun `page returns correct path`() {
@@ -29,12 +38,12 @@ abstract class JsonPageTest(
 
        @Test
        fun `page needs form password`() {
-               assertThat(page.needsFormPassword(), equalTo(needsFormPassword))
+               assertThat(page.needsFormPassword, equalTo(needsFormPassword))
        }
 
        @Test
        fun `page requires login`() {
-               assertThat(page.requiresLogin(), equalTo(requiresLogin))
+               assertThat(page.requiresLogin, equalTo(requiresLogin))
        }
 
 }