Move recurring tests for ajax pages to test base class
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ajax / JsonPageTest.kt
index b5f17cf..5d43a1e 100644 (file)
@@ -19,9 +19,15 @@ import net.pterodactylus.sone.utils.asOptional
 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
+import org.mockito.ArgumentMatchers.eq
 import org.mockito.ArgumentMatchers.isNull
 import java.util.NoSuchElementException
 import javax.naming.SizeLimitExceededException
@@ -29,7 +35,11 @@ import javax.naming.SizeLimitExceededException
 /**
  * Base class for tests for any [JsonPage] implementations.
  */
-open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<JsonPage>() }) {
+abstract class JsonPageTest(
+               private val expectedPath: String,
+               private val requiresLogin: Boolean = true,
+               private val needsFormPassword: Boolean = true,
+               pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<JsonPage>() }) {
 
        protected val webInterface = mock<WebInterface>()
        protected val core = mock<Core>()
@@ -42,10 +52,12 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        protected val httpRequest = mock<HTTPRequest>()
        protected val currentSone = deepMock<Sone>()
 
+       private val requestHeaders = mutableMapOf<String, String>()
        private val requestParameters = mutableMapOf<String, String>()
        private val requestParts = mutableMapOf<String, String>()
        private val localSones = mutableMapOf<String, Sone>()
        private val remoteSones = mutableMapOf<String, Sone>()
+       private val posts = mutableMapOf<String, Post>()
        private val newPosts = mutableMapOf<String, Post>()
        private val newReplies = mutableMapOf<String, PostReply>()
        private val linkedElements = mutableMapOf<String, LinkedElement>()
@@ -53,6 +65,7 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
 
        @Before
        fun setupWebInterface() {
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(currentSone)
                whenever(webInterface.core).thenReturn(core)
@@ -64,7 +77,8 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        @Before
        fun setupCore() {
                whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
-               whenever(core.getPost(anyString())).thenAnswer { newPosts[it[0]].asOptional() }
+               whenever(core.getLocalSone(anyString())).thenAnswer { localSones[it[0]] }
+               whenever(core.getPost(anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() }
        }
 
        @Before
@@ -82,11 +96,14 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        @Before
        fun setupFreenetRequest() {
                whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
+               whenever(freenetRequest.method).thenReturn(GET)
                whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
        }
 
        @Before
        fun setupHttpRequest() {
+               whenever(httpRequest.method).thenReturn("GET")
+               whenever(httpRequest.getHeader(anyString())).thenAnswer { requestHeaders[it.get<String>(0).toLowerCase()] }
                whenever(httpRequest.getParam(anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" }
                whenever(httpRequest.getParam(anyString(), anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: it.getArgument(1) }
                whenever(httpRequest.getParam(anyString(), isNull())).thenAnswer { requestParameters[it.getArgument(0)] }
@@ -99,6 +116,8 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
                whenever(httpRequest.isPartSet(anyString())).thenAnswer { it.getArgument(0) in requestParts }
        }
 
+       protected val JsonReturnObject.error get() = if (this is JsonErrorReturnObject) this.error else null
+
        protected fun Sone.mock(id: String, name: String, local: Boolean = false, time: Long, status: SoneStatus = idle) = apply {
                whenever(this.id).thenReturn(id)
                whenever(this.name).thenReturn(name)
@@ -108,10 +127,15 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
        }
 
        protected fun unsetCurrentSone() {
+               whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null)
                whenever(webInterface.getCurrentSoneWithoutCreatingSession(toadletContext)).thenReturn(null)
                whenever(webInterface.getCurrentSoneCreatingSession(toadletContext)).thenReturn(null)
        }
 
+       protected fun addRequestHeader(key: String, value: String) {
+               requestHeaders += key.toLowerCase() to value
+       }
+
        protected fun addRequestParameter(key: String, value: String) {
                requestParameters += key to value
        }
@@ -128,6 +152,14 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
                remoteSones += sone.id to sone
        }
 
+       protected fun addLocalSone(id: String, sone: Sone) {
+               localSones += id to sone
+       }
+
+       protected fun addPost(id: String, post: Post) {
+               posts[id] = post
+       }
+
        protected fun addNewPost(id: String, soneId: String, time: Long, recipientId: String? = null) =
                        mock<Post>().apply {
                                whenever(this.id).thenReturn(id)
@@ -155,4 +187,19 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<J
                linkedElements[link] = LinkedElement(link, failed, loading)
        }
 
+       @Test
+       fun `page returns correct path`() {
+               assertThat(page.path, equalTo(expectedPath))
+       }
+
+       @Test
+       fun `page needs form password`() {
+               assertThat(page.needsFormPassword(), equalTo(needsFormPassword))
+       }
+
+       @Test
+       fun `page requires login`() {
+               assertThat(page.requiresLogin(), equalTo(requiresLogin))
+       }
+
 }