test: ユーザーのinboxのGETのテストを追加

This commit is contained in:
usbharu 2023-04-10 15:06:11 +09:00
parent e4dc17f3b4
commit 31e4218ef8
4 changed files with 27 additions and 11 deletions

View File

@ -25,7 +25,9 @@ fun Routing.inbox(
throw HttpSignatureVerifyException() throw HttpSignatureVerifyException()
} }
val json = call.receiveText() val json = call.receiveText()
call.application.log.trace("Received: $json")
val activityTypes = activityPubService.parseActivity(json) val activityTypes = activityPubService.parseActivity(json)
call.application.log.debug("ActivityTypes: ${activityTypes.name}")
val response = activityPubService.processActivity(json, activityTypes) val response = activityPubService.processActivity(json, activityTypes)
when (response) { when (response) {
is ActivityPubObjectResponse -> call.respond(response.httpStatusCode, Config.configData.objectMapper.writeValueAsString(response.message.apply { context = 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"){ route("/users/{name}/inbox"){
get { get {
call.respond(HttpStatusCode.NotImplemented) call.respond(HttpStatusCode.MethodNotAllowed)
} }
post { post {
if (httpSignatureVerifyService.verify(call.request.headers).not()) { if (httpSignatureVerifyService.verify(call.request.headers).not()) {

View File

@ -10,12 +10,16 @@ import dev.usbharu.hideout.exception.JsonParseException
import kjob.core.Job import kjob.core.Job
import kjob.core.dsl.JobContextWithProps import kjob.core.dsl.JobContextWithProps
import kjob.core.job.JobProps import kjob.core.job.JobProps
import org.slf4j.LoggerFactory
import kotlin.reflect.full.createInstance import kotlin.reflect.full.createInstance
import kotlin.reflect.full.primaryConstructor import kotlin.reflect.full.primaryConstructor
class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFollowService) : ActivityPubService { class ActivityPubServiceImpl(private val activityPubFollowService: ActivityPubFollowService) : ActivityPubService {
val logger = LoggerFactory.getLogger(this::class.java)
override fun parseActivity(json: String): ActivityType { override fun parseActivity(json: String): ActivityType {
val readTree = Config.configData.objectMapper.readTree(json) val readTree = Config.configData.objectMapper.readTree(json)
logger.debug("readTree: {}", readTree)
if (readTree.isObject.not()) { if (readTree.isObject.not()) {
throw JsonParseException("Json is not object.") throw JsonParseException("Json is not object.")
} }

View File

@ -1,10 +1,6 @@
package dev.usbharu.hideout.routing.activitypub package dev.usbharu.hideout.routing.activitypub
import com.fasterxml.jackson.annotation.JsonInclude import dev.usbharu.hideout.exception.JsonParseException
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.plugins.configureRouting import dev.usbharu.hideout.plugins.configureRouting
import dev.usbharu.hideout.plugins.configureSerialization import dev.usbharu.hideout.plugins.configureSerialization
import dev.usbharu.hideout.plugins.configureStatusPages 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.impl.UserService
import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.config.* import io.ktor.server.config.*
import io.ktor.server.testing.* import io.ktor.server.testing.*
import junit.framework.TestCase.assertEquals
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.kotlin.any import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn import org.mockito.kotlin.doReturn
import org.mockito.kotlin.doThrow
import org.mockito.kotlin.mock import org.mockito.kotlin.mock
class InboxRoutingKtTest { class InboxRoutingKtTest {
@Test @Test
fun `sharedInboxにGETしたら501が帰ってくる`() = testApplication { fun `sharedInboxにGETしたら405が帰ってくる`() = testApplication {
environment { environment {
config = ApplicationConfig("empty.conf") config = ApplicationConfig("empty.conf")
} }
@ -47,7 +42,9 @@ class InboxRoutingKtTest {
val httpSignatureVerifyService = mock<HttpSignatureVerifyService>{ val httpSignatureVerifyService = mock<HttpSignatureVerifyService>{
on { verify(any()) } doReturn true on { verify(any()) } doReturn true
} }
val activityPubService = mock<ActivityPubService>() val activityPubService = mock<ActivityPubService>{
on { parseActivity(any()) } doThrow JsonParseException()
}
val userService = mock<UserService>() val userService = mock<UserService>()
val activityPubUserService = mock<ActivityPubUserService>() val activityPubUserService = mock<ActivityPubUserService>()
application { application {
@ -59,4 +56,18 @@ class InboxRoutingKtTest {
Assertions.assertEquals(HttpStatusCode.BadRequest, it.status) 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)
}
}
} }

View File

@ -3,7 +3,6 @@ ktor {
deployment { deployment {
port = 8080 port = 8080
port = ${?PORT} port = ${?PORT}
watch = [classes, resources]
} }
application { application {
modules = [dev.usbharu.hideout.EmptyKt.empty] modules = [dev.usbharu.hideout.EmptyKt.empty]