From 31e4218ef87470c654b482142cb3e4c3b3640ea7 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 10 Apr 2023 15:06:11 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC?= =?UTF-8?q?=E3=81=AEinbox=E3=81=AEGET=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../routing/activitypub/InboxRouting.kt | 4 ++- .../activitypub/ActivityPubServiceImpl.kt | 4 +++ .../routing/activitypub/InboxRoutingKtTest.kt | 29 +++++++++++++------ src/test/resources/empty.conf | 1 - 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt index ea2193ea..92753f36 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRouting.kt @@ -25,7 +25,9 @@ fun Routing.inbox( throw HttpSignatureVerifyException() } val json = call.receiveText() + call.application.log.trace("Received: $json") val activityTypes = activityPubService.parseActivity(json) + call.application.log.debug("ActivityTypes: ${activityTypes.name}") val response = activityPubService.processActivity(json, activityTypes) when (response) { is ActivityPubObjectResponse -> call.respond(response.httpStatusCode, Config.configData.objectMapper.writeValueAsString(response.message.apply { context = @@ -38,7 +40,7 @@ fun Routing.inbox( } route("/users/{name}/inbox"){ get { - call.respond(HttpStatusCode.NotImplemented) + call.respond(HttpStatusCode.MethodNotAllowed) } post { if (httpSignatureVerifyService.verify(call.request.headers).not()) { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt index 8dfe8ff1..06224a67 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubServiceImpl.kt @@ -10,12 +10,16 @@ import dev.usbharu.hideout.exception.JsonParseException import kjob.core.Job import kjob.core.dsl.JobContextWithProps import kjob.core.job.JobProps +import org.slf4j.LoggerFactory import kotlin.reflect.full.createInstance import kotlin.reflect.full.primaryConstructor class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFollowService) : ActivityPubService { + + val logger = LoggerFactory.getLogger(this::class.java) override fun parseActivity(json: String): ActivityType { val readTree = Config.configData.objectMapper.readTree(json) + logger.debug("readTree: {}", readTree) if (readTree.isObject.not()) { throw JsonParseException("Json is not object.") } diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt index 106eae24..1e545605 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/InboxRoutingKtTest.kt @@ -1,10 +1,6 @@ package dev.usbharu.hideout.routing.activitypub -import com.fasterxml.jackson.annotation.JsonInclude -import com.fasterxml.jackson.annotation.JsonSetter -import com.fasterxml.jackson.annotation.Nulls -import com.fasterxml.jackson.databind.DeserializationFeature -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import dev.usbharu.hideout.exception.JsonParseException import dev.usbharu.hideout.plugins.configureRouting import dev.usbharu.hideout.plugins.configureSerialization import dev.usbharu.hideout.plugins.configureStatusPages @@ -13,20 +9,19 @@ import dev.usbharu.hideout.service.activitypub.ActivityPubUserService import dev.usbharu.hideout.service.impl.UserService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import io.ktor.client.request.* -import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.server.config.* import io.ktor.server.testing.* -import junit.framework.TestCase.assertEquals import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import org.mockito.kotlin.any import org.mockito.kotlin.doReturn +import org.mockito.kotlin.doThrow import org.mockito.kotlin.mock class InboxRoutingKtTest { @Test - fun `sharedInboxにGETしたら501が帰ってくる`() = testApplication { + fun `sharedInboxにGETしたら405が帰ってくる`() = testApplication { environment { config = ApplicationConfig("empty.conf") } @@ -47,7 +42,9 @@ class InboxRoutingKtTest { val httpSignatureVerifyService = mock{ on { verify(any()) } doReturn true } - val activityPubService = mock() + val activityPubService = mock{ + on { parseActivity(any()) } doThrow JsonParseException() + } val userService = mock() val activityPubUserService = mock() application { @@ -59,4 +56,18 @@ class InboxRoutingKtTest { Assertions.assertEquals(HttpStatusCode.BadRequest, it.status) } } + + @Test + fun `ユーザのinboxにGETしたら405が帰ってくる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + application { + configureSerialization() + configureRouting(mock(), mock(), mock(), mock()) + } + client.get("/users/test/inbox").let { + Assertions.assertEquals(HttpStatusCode.MethodNotAllowed, it.status) + } + } } diff --git a/src/test/resources/empty.conf b/src/test/resources/empty.conf index 861f37bc..ba691e1c 100644 --- a/src/test/resources/empty.conf +++ b/src/test/resources/empty.conf @@ -3,7 +3,6 @@ ktor { deployment { port = 8080 port = ${?PORT} - watch = [classes, resources] } application { modules = [dev.usbharu.hideout.EmptyKt.empty]