From d696494100ac9317be6093c7e81b325fd54d8615 Mon Sep 17 00:00:00 2001 From: usbharu Date: Wed, 19 Feb 2025 13:06:41 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hideout/hideout-activitypub/build.gradle.kts | 1 + .../activitypub/interfaces/wellknown/XRD.kt | 4 +- .../WebFingerApplicationServiceTest.kt | 98 +++++++++++++++++++ .../src/test/kotlin/util/TestTransaction.kt | 8 ++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 hideout/hideout-activitypub/src/test/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationServiceTest.kt create mode 100644 hideout/hideout-activitypub/src/test/kotlin/util/TestTransaction.kt diff --git a/hideout/hideout-activitypub/build.gradle.kts b/hideout/hideout-activitypub/build.gradle.kts index 1791af68..68951ff6 100644 --- a/hideout/hideout-activitypub/build.gradle.kts +++ b/hideout/hideout-activitypub/build.gradle.kts @@ -52,6 +52,7 @@ dependencies { testImplementation(libs.coroutines.test) testImplementation(libs.h2db) testImplementation(libs.flyway.core) + testImplementation(libs.mockito.kotlin) } tasks.test { diff --git a/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/XRD.kt b/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/XRD.kt index 1f4827f4..e6617c3f 100644 --- a/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/XRD.kt +++ b/hideout/hideout-activitypub/src/main/kotlin/dev/usbharu/hideout/activitypub/interfaces/wellknown/XRD.kt @@ -9,7 +9,7 @@ import java.net.URI @JacksonXmlRootElement(localName = "XRD", namespace = "http://docs.oasis-open.org/ns/xri/xrd-1.0") @JsonInclude(JsonInclude.Include.NON_NULL) -class XRD( +data class XRD( @JacksonXmlProperty(localName = "Link", namespace = "http://docs.oasis-open.org/ns/xri/xrd-1.0") @JacksonXmlElementWrapper(useWrapping = false) @JsonProperty("links") @@ -21,7 +21,7 @@ class XRD( ) @JsonInclude(JsonInclude.Include.NON_NULL) -class Link( +data class Link( @JacksonXmlProperty(localName = "rel", isAttribute = true) val rel: String, @JsonInclude(JsonInclude.Include.NON_NULL) @JacksonXmlProperty(localName = "template", isAttribute = true) val template: String?, diff --git a/hideout/hideout-activitypub/src/test/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationServiceTest.kt b/hideout/hideout-activitypub/src/test/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationServiceTest.kt new file mode 100644 index 00000000..cb341b90 --- /dev/null +++ b/hideout/hideout-activitypub/src/test/kotlin/dev/usbharu/hideout/activitypub/application/webfinger/WebFingerApplicationServiceTest.kt @@ -0,0 +1,98 @@ +package dev.usbharu.hideout.activitypub.application.webfinger + +import dev.usbharu.hideout.activitypub.external.activitystreams.TestActorFactory +import dev.usbharu.hideout.activitypub.interfaces.wellknown.Link +import dev.usbharu.hideout.activitypub.interfaces.wellknown.XRD +import dev.usbharu.hideout.core.config.ApplicationConfig +import dev.usbharu.hideout.core.domain.model.actor.ActorRepository +import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.Spy +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.eq +import org.mockito.kotlin.times +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import util.TestTransaction +import java.net.URI +import kotlin.test.assertEquals + +@ExtendWith(MockitoExtension::class) +class WebFingerApplicationServiceTest { + @InjectMocks + lateinit var webFingerApplicationService: WebFingerApplicationService + + @Mock + lateinit var actorRepository: ActorRepository + + @Spy + val transaction = TestTransaction + + @Spy + val applicationConfig = ApplicationConfig(URI.create("https://example.com")) + + @Test + fun acctから始まらないとだめ() = runTest { + assertThrows { + webFingerApplicationService.execute("a", Anonymous) + } + } + + @Test + fun ドメインが自ドメインと一致しないとだめ() = runTest { + assertThrows { + webFingerApplicationService.execute("acct:test@remote.example.com", Anonymous) + } + } + + @Test + fun `acct@username@hostはだめ`() = runTest { + assertThrows { + webFingerApplicationService.execute("acct:@username@example.com", Anonymous) + } + } + + @Test + fun actorが存在しないとだめ() = runTest { + assertThrows { + webFingerApplicationService.execute("acct:test2@example.com", Anonymous) + } + + verify(actorRepository, times(1)).findByNameAndDomain(eq("test2"), eq("example.com")) + } + + @Test + fun actorが存在したら返す() = runTest { + whenever( + actorRepository.findByNameAndDomain( + eq("test"), eq("example.com") + ) + ).thenReturn( + TestActorFactory.create( + actorName = "test", + domain = "example.com", + uri = URI.create("https://example.com/users/test") + ) + ) + + val execute = webFingerApplicationService.execute("acct:test@example.com", Anonymous) + + val expected = XRD( + listOf( + Link( + rel = "self", + href = "https://example.com/users/test", + type = "application/activity+json", + template = null + ) + ), URI.create("acct:test@example.com") + ) + + assertEquals(expected, execute) + } +} \ No newline at end of file diff --git a/hideout/hideout-activitypub/src/test/kotlin/util/TestTransaction.kt b/hideout/hideout-activitypub/src/test/kotlin/util/TestTransaction.kt new file mode 100644 index 00000000..067d8758 --- /dev/null +++ b/hideout/hideout-activitypub/src/test/kotlin/util/TestTransaction.kt @@ -0,0 +1,8 @@ +package util + +import dev.usbharu.hideout.core.application.shared.Transaction + +object TestTransaction : Transaction { + override suspend fun transaction(block: suspend () -> T): T = block() + override suspend fun transaction(transactionLevel: Int, block: suspend () -> T): T = block() +}