test: テストを追加2

This commit is contained in:
2025-02-19 13:06:41 +09:00
parent 17cc92d344
commit d696494100
4 changed files with 109 additions and 2 deletions
@@ -52,6 +52,7 @@ dependencies {
testImplementation(libs.coroutines.test)
testImplementation(libs.h2db)
testImplementation(libs.flyway.core)
testImplementation(libs.mockito.kotlin)
}
tasks.test {
@@ -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?,
@@ -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<IllegalArgumentException> {
webFingerApplicationService.execute("a", Anonymous)
}
}
@Test
fun ドメインが自ドメインと一致しないとだめ() = runTest {
assertThrows<IllegalArgumentException> {
webFingerApplicationService.execute("acct:test@remote.example.com", Anonymous)
}
}
@Test
fun `acct@username@hostはだめ`() = runTest {
assertThrows<IllegalArgumentException> {
webFingerApplicationService.execute("acct:@username@example.com", Anonymous)
}
}
@Test
fun actorが存在しないとだめ() = runTest {
assertThrows<IllegalArgumentException> {
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)
}
}
@@ -0,0 +1,8 @@
package util
import dev.usbharu.hideout.core.application.shared.Transaction
object TestTransaction : Transaction {
override suspend fun <T> transaction(block: suspend () -> T): T = block()
override suspend fun <T> transaction(transactionLevel: Int, block: suspend () -> T): T = block()
}