X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FWebPageTest2.kt;h=e8fbd98f4da61d2281cfd07b39355a5942b440a4;hp=69ac20ab0ffc11bd4eb76086aead0ec1176c14c3;hb=655d836ea81170e883cf229573bc5edd0169cee7;hpb=d20861b75602e3a12e354f2c19dca26fcf7b3e5c diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt index 69ac20a..e8fbd98 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt @@ -2,7 +2,9 @@ package net.pterodactylus.sone.web.pages import com.google.common.eventbus.EventBus import freenet.clients.http.ToadletContext +import freenet.support.SimpleReadOnlyArrayBucket import freenet.support.api.HTTPRequest +import freenet.support.api.HTTPUploadedFile import net.pterodactylus.sone.core.Preferences import net.pterodactylus.sone.data.Album import net.pterodactylus.sone.data.Image @@ -20,13 +22,13 @@ import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException +import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.template.Template import net.pterodactylus.util.template.TemplateContext import net.pterodactylus.util.web.Method import net.pterodactylus.util.web.Method.GET import net.pterodactylus.util.web.Response import org.junit.Assert.fail -import org.junit.Before import org.mockito.ArgumentMatchers.anyBoolean import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyLong @@ -61,6 +63,9 @@ open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePa private val requestHeaders = mutableMapOf() private val getRequestParameters = mutableMapOf>() private val postRequestParameters = mutableMapOf() + private val uploadedFileNames = mutableMapOf() + private val uploadedFileContentTypes = mutableMapOf() + private val uploadedFileResources = mutableMapOf() private val ownIdentities = mutableSetOf() private val allSones = mutableMapOf() private val localSones = mutableMapOf() @@ -69,6 +74,7 @@ open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePa private val perPostReplies = mutableMapOf() private val allAlbums = mutableMapOf() private val allImages = mutableMapOf() + private val notifications = mutableMapOf() private val translations = mutableMapOf() init { @@ -99,7 +105,8 @@ open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePa whenever(webInterface.getCurrentSoneCreatingSession(eq(toadletContext))).thenReturn(currentSone) whenever(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone) whenever(webInterface.getCurrentSoneWithoutCreatingSession(eq(toadletContext))).thenReturn(currentSone) - whenever(webInterface.getNotifications(currentSone)).thenReturn(emptyList()) + whenever(webInterface.getNotifications(currentSone)).then { notifications.values } + whenever(webInterface.getNotification(anyString())).then { notifications[it[0]].asOptional() } } private fun setupHttpRequest() { @@ -117,6 +124,17 @@ open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePa whenever(httpRequest.getMultipleIntParam(anyString())).then { getRequestParameters[it[0]]?.map { it.toIntOrNull() ?: 0 } ?: emptyArray() } whenever(httpRequest.isPartSet(anyString())).then { it[0] in postRequestParameters } whenever(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).then { postRequestParameters[it[0]]?.decode()?.take(it[1]) ?: "" } + whenever(httpRequest.getUploadedFile(anyString())).then { + it.get(0).takeIf { it in uploadedFileNames } + ?.let { name -> UploadedFile(uploadedFileNames[name]!!, uploadedFileContentTypes[name]!!, uploadedFileResources[name]!!) + } + } + } + + private class UploadedFile(private val filename: String, private val contentType: String, private val resourceName: String): HTTPUploadedFile { + override fun getFilename() = filename + override fun getContentType() = contentType + override fun getData() = javaClass.getResourceAsStream(resourceName).readBytes().let(::SimpleReadOnlyArrayBucket) } private fun ByteArray.decode(charset: Charset = UTF_8) = String(this, charset) @@ -192,10 +210,20 @@ open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePa translations[key] = value } + fun addNotification(id: String, notification: Notification) { + notifications[id] = notification + } + fun addTemporaryImage(id: String, temporaryImage: TemporaryImage) { whenever(core.getTemporaryImage(id)).thenReturn(temporaryImage) } + fun addUploadedFile(name: String, filename: String, contentType: String, resource: String) { + uploadedFileNames[name] = filename + uploadedFileContentTypes[name] = contentType + uploadedFileResources[name] = resource + } + fun verifyNoRedirect(assertions: () -> Unit) { var caughtException: Exception? = null try {