From 37d919ca8a47a120c755fad431cbb0f2c6133ce9 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 10 Apr 2023 11:37:08 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E3=83=A2=E3=83=83=E3=82=AF=E3=82=92?= =?UTF-8?q?=E5=B0=8E=E5=85=A5=E3=81=97=E3=83=86=E3=82=B9=E3=83=88=E5=90=8D?= =?UTF-8?q?=E3=82=92=E8=A9=B3=E7=B4=B0=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 4 +- .../routing/activitypub/UsersAPTest.kt | 109 ++++-------------- 2 files changed, 23 insertions(+), 90 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9afedfb0..2e5f0742 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -69,10 +69,12 @@ dependencies { implementation("io.ktor:ktor-client-cio:$ktor_version") implementation("io.ktor:ktor-client-content-negotiation:$ktor_version") testImplementation("io.ktor:ktor-client-mock:$ktor_version") - implementation("tech.barbero.http-messages-signing:http-messages-signing-core:1.0.0") testImplementation("org.junit.jupiter:junit-jupiter:5.8.1") + testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0") + testImplementation("org.mockito:mockito-inline:5.2.0") + implementation("org.drewcarlson:kjob-core:0.6.0") testImplementation("io.ktor:ktor-server-test-host-jvm:2.2.4") diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt index f0c22e09..32b6cb17 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/activitypub/UsersAPTest.kt @@ -9,35 +9,29 @@ import com.fasterxml.jackson.module.kotlin.readValue import dev.usbharu.hideout.ap.Image import dev.usbharu.hideout.ap.Key import dev.usbharu.hideout.ap.Person -import dev.usbharu.hideout.config.Config -import dev.usbharu.hideout.domain.model.ActivityPubResponse -import dev.usbharu.hideout.domain.model.User -import dev.usbharu.hideout.domain.model.UserEntity -import dev.usbharu.hideout.domain.model.job.HideoutJob import dev.usbharu.hideout.plugins.configureRouting import dev.usbharu.hideout.plugins.configureSerialization -import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.service.activitypub.ActivityPubService import dev.usbharu.hideout.service.activitypub.ActivityPubUserService -import dev.usbharu.hideout.service.activitypub.ActivityType import dev.usbharu.hideout.service.impl.UserService import dev.usbharu.hideout.service.signature.HttpSignatureVerifyService import dev.usbharu.hideout.util.HttpUtil.Activity -import io.ktor.client.call.* 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 kjob.core.dsl.JobContextWithProps import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyString +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock import kotlin.test.assertEquals class UsersAPTest { - @Test - fun testHandleUsersName() = testApplication { + @Test() + fun `ユーザのURLにAcceptヘッダーをActivityにしてアクセスしたときPersonが返ってくるか`() = testApplication { environment { config = ApplicationConfig("empty.conf") } @@ -65,82 +59,18 @@ class UsersAPTest { ) ) person.context = listOf("https://www.w3.org/ns/activitystreams") + + val httpSignatureVerifyService = mock {} + val activityPubService = mock {} + val userService = mock {} + + val activityPubUserService = mock { + onBlocking { getPersonByName(anyString()) } doReturn person + } + application { configureSerialization() - configureRouting(object : HttpSignatureVerifyService { - override fun verify(headers: Headers): Boolean { - return true - } - }, object : ActivityPubService { - override fun parseActivity(json: String): ActivityType { - TODO("Not yet implemented") - } - - override suspend fun processActivity(json: String, type: ActivityType): ActivityPubResponse? { - TODO("Not yet implemented") - } - - override suspend fun processActivity( - job: JobContextWithProps, - hideoutJob: HideoutJob - ) { - TODO("Not yet implemented") - } - - }, UserService(object : IUserRepository { - override suspend fun create(user: User): UserEntity { - TODO("Not yet implemented") - } - - override suspend fun findById(id: Long): UserEntity? { - TODO("Not yet implemented") - } - - override suspend fun findByName(name: String): UserEntity? { - TODO("Not yet implemented") - } - - override suspend fun findByUrl(url: String): UserEntity? { - TODO("Not yet implemented") - } - - override suspend fun update(userEntity: UserEntity) { - TODO("Not yet implemented") - } - - override suspend fun delete(id: Long) { - TODO("Not yet implemented") - } - - override suspend fun findAll(): List { - TODO("Not yet implemented") - } - - override suspend fun findAllByLimitAndByOffset(limit: Int, offset: Long): List { - TODO("Not yet implemented") - } - - override suspend fun createFollower(id: Long, follower: Long) { - TODO("Not yet implemented") - } - - override suspend fun deleteFollower(id: Long, follower: Long) { - TODO("Not yet implemented") - } - - override suspend fun findFollowersById(id: Long): List { - TODO("Not yet implemented") - } - }), object : ActivityPubUserService { - override suspend fun getPersonByName(name: String): Person { - return person - } - - override suspend fun fetchPerson(url: String): Person { - TODO("Not yet implemented") - } - - }) + configureRouting(httpSignatureVerifyService, activityPubService, userService, activityPubUserService) } client.get("/users/test") { accept(ContentType.Application.Activity) @@ -148,12 +78,13 @@ class UsersAPTest { val objectMapper = jacksonObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - objectMapper.configOverride(List::class.java).setSetterInfo(JsonSetter.Value.forValueNulls( - Nulls.AS_EMPTY)) + objectMapper.configOverride(List::class.java).setSetterInfo( + JsonSetter.Value.forValueNulls( + Nulls.AS_EMPTY + ) + ) val actual = it.bodyAsText() val readValue = objectMapper.readValue(actual) - println(objectMapper.writeValueAsString(person)) - println(objectMapper.writeValueAsString(readValue)) assertEquals(person, readValue) } }