From 469b886caf0b27409389ebec3291f7a6eed256e9 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 7 Apr 2023 13:00:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20ActivityPub=E3=81=AEusers=E3=81=AE?= =?UTF-8?q?=E3=83=AB=E3=83=BC=E3=83=86=E3=82=A3=E3=83=B3=E3=82=B0=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 4 ++ .../routing/activitypub/UserRouting.kt | 7 +-- src/test/kotlin/dev/usbharu/hideout/Empty.kt | 7 +++ .../routing/activitypub/UsersAPTest.kt | 44 +++++++++++++++++++ src/test/resources/empty.conf | 22 ++++++++++ 5 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/test/kotlin/dev/usbharu/hideout/Empty.kt create mode 100644 src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt create mode 100644 src/test/resources/empty.conf diff --git a/build.gradle.kts b/build.gradle.kts index b7e81f91..146f725f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -73,6 +73,10 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter:5.8.1") implementation("org.drewcarlson:kjob-core:0.6.0") + testImplementation("io.ktor:ktor-server-test-host-jvm:2.2.4") + + testImplementation("org.slf4j:slf4j-simple:2.0.7") + } jib { diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt index 9c73fbbc..f879a400 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/activitypub/UserRouting.kt @@ -11,9 +11,6 @@ import io.ktor.server.routing.* fun Routing.usersAP(activityPubService: ActivityPubService){ route("/users/{name}"){ createChild(ContentTypeRouteSelector(ContentType.Application.Activity)).handle { - val json = call.receiveText() - val activityTypes = activityPubService.parseActivity(json) - activityPubService.processActivity(json,activityTypes) call.respond(HttpStatusCode.NotImplemented) } } @@ -21,10 +18,10 @@ fun Routing.usersAP(activityPubService: ActivityPubService){ class ContentTypeRouteSelector(private val contentType: ContentType) : RouteSelector() { override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation { - return if (context.call.request.contentType() == contentType) { + return if (ContentType.parse(context.call.request.accept() ?: return RouteSelectorEvaluation.FailedParameter) == contentType) { RouteSelectorEvaluation.Constant } else { - RouteSelectorEvaluation.Failed + RouteSelectorEvaluation.FailedParameter } } diff --git a/src/test/kotlin/dev/usbharu/hideout/Empty.kt b/src/test/kotlin/dev/usbharu/hideout/Empty.kt new file mode 100644 index 00000000..0203d877 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/Empty.kt @@ -0,0 +1,7 @@ +package dev.usbharu.hideout + +import io.ktor.server.application.* + +fun Application.empty(){ + +} diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt new file mode 100644 index 00000000..7eca6e24 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt @@ -0,0 +1,44 @@ +package dev.usbharu.hideout.routing.activitypub + +import dev.usbharu.hideout.plugins.configureRouting +import dev.usbharu.hideout.service.activitypub.ActivityPubService +import dev.usbharu.hideout.service.activitypub.ActivityType +import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService +import dev.usbharu.hideout.util.HttpUtil.Activity +import io.ktor.client.request.* +import io.ktor.http.* +import io.ktor.server.config.* +import io.ktor.server.testing.* +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + + +class UsersAPTest { + + @Test + fun testHandleUsersName() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + application { + configureRouting(object : HttpSignatureVerifyService { + override fun verify(headers: Headers): Boolean { + return true + } + }, object : ActivityPubService { + override fun parseActivity(json: String): ActivityType { + TODO("Not yet implemented") + } + + override fun processActivity(json: String, type: ActivityType) { + TODO("Not yet implemented") + } + }) + } + client.get("/users/test"){ + accept(ContentType.Application.Activity) + }.let { + assertEquals(HttpStatusCode.NotImplemented, it.status) + } + } +} diff --git a/src/test/resources/empty.conf b/src/test/resources/empty.conf new file mode 100644 index 00000000..861f37bc --- /dev/null +++ b/src/test/resources/empty.conf @@ -0,0 +1,22 @@ +ktor { + development = true + deployment { + port = 8080 + port = ${?PORT} + watch = [classes, resources] + } + application { + modules = [dev.usbharu.hideout.EmptyKt.empty] + } +} + +hideout { + hostname = "https://localhost:8080" + hostname = ${?HOSTNAME} + database { + url = "jdbc:h2:./test;MODE=POSTGRESQL" + driver = "org.h2.Driver" + username = "" + password = "" + } +}