From 845569a7f914f235df95eec2b66b6699d6723c62 Mon Sep 17 00:00:00 2001 From: usbharu Date: Mon, 17 Jun 2024 14:40:23 +0900 Subject: [PATCH 1/6] =?UTF-8?q?test:=20Actor=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../relationship/get/Relationship.kt | 6 +- .../ActorInstanceRelationshipEvent.kt | 6 +- .../hideout/core/domain/model/actor/Actor.kt | 2 +- .../core/domain/model/actor/ActorKeyId.kt | 6 +- .../core/domain/model/actor/ActorName.kt | 6 +- .../hideout/core/domain/model/actor/Role.kt | 5 +- .../ActorInstanceRelationship.kt | 21 ++--- .../core/domain/model/filter/FilterKeyword.kt | 9 --- ...osedActorInstanceRelationshipRepository.kt | 6 +- .../ExposedActorRepository.kt | 2 +- .../model/actor/ActorDescriptionTest.kt | 12 ++- .../core/domain/model/actor/ActorKeyIdTest.kt | 25 ++++++ .../domain/model/actor/ActorPostsCountTest.kt | 20 +++++ .../domain/model/actor/ActorPrivateKeyTest.kt | 18 +++++ .../domain/model/actor/ActorPublicKeyTest.kt | 17 +++- .../model/actor/ActorRelationshipCountTest.kt | 21 +++++ .../core/domain/model/actor/ActorsTest.kt | 42 ++++++++++ .../domain/model/actor/TestActorFactory.kt | 10 ++- .../ActorInstanceRelationshipTest.kt | 77 +++++++++++++++++++ 19 files changed, 270 insertions(+), 41 deletions(-) create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyIdTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCountTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPrivateKeyTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCountTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationshipTest.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/relationship/get/Relationship.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/relationship/get/Relationship.kt index 4893d62a..03237ef4 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/relationship/get/Relationship.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/relationship/get/Relationship.kt @@ -49,9 +49,9 @@ data class Relationship( muting = relationship.muting, followRequesting = relationship.followRequesting, followRequestedBy = relationship2.followRequesting, - domainBlocking = actorInstanceRelationship.isBlocking(), - domainMuting = actorInstanceRelationship.isMuting(), - domainDoNotSendPrivate = actorInstanceRelationship.isDoNotSendPrivate() + domainBlocking = actorInstanceRelationship.blocking, + domainMuting = actorInstanceRelationship.muting, + domainDoNotSendPrivate = actorInstanceRelationship.doNotSendPrivate ) } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/event/actorinstancerelationship/ActorInstanceRelationshipEvent.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/event/actorinstancerelationship/ActorInstanceRelationshipEvent.kt index 5ff3fc5f..bf332080 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/event/actorinstancerelationship/ActorInstanceRelationshipEvent.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/event/actorinstancerelationship/ActorInstanceRelationshipEvent.kt @@ -34,9 +34,9 @@ class ActorInstanceRelationshipEventBody(actorInstanceRelationship: ActorInstanc mapOf( "actorId" to actorInstanceRelationship.actorId, "instanceId" to actorInstanceRelationship.instanceId, - "muting" to actorInstanceRelationship.isMuting(), - "blocking" to actorInstanceRelationship.isBlocking(), - "doNotSendPrivate" to actorInstanceRelationship.isDoNotSendPrivate(), + "muting" to actorInstanceRelationship.muting, + "blocking" to actorInstanceRelationship.blocking, + "doNotSendPrivate" to actorInstanceRelationship.doNotSendPrivate, ) ) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Actor.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Actor.kt index 45d34c4d..239ce88f 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Actor.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Actor.kt @@ -79,7 +79,7 @@ class Actor( private set fun setRole(roles: Set, actor: Actor) { - require(actor.roles.contains(Role.ADMINISTRATOR).not()) + require(actor.roles.contains(Role.ADMINISTRATOR)) this.roles = roles } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyId.kt index ccf98962..3bc3e643 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyId.kt @@ -17,4 +17,8 @@ package dev.usbharu.hideout.core.domain.model.actor @JvmInline -value class ActorKeyId(val keyId: String) +value class ActorKeyId(val keyId: String) { + init { + require(keyId.isNotBlank()) + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt index 6819fc8e..a2e6697e 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt @@ -20,12 +20,12 @@ package dev.usbharu.hideout.core.domain.model.actor value class ActorName(val name: String) { init { require(name.isNotBlank()) - require(name.length <= length) + require(name.length <= LENGTH) require(regex.matches(name)) } companion object { - val length = 300 - private val regex = Regex("^[a-zA-Z0-9_-]{1,$length}\$") + const val LENGTH = 300 + private val regex = Regex("^[a-zA-Z0-9_-]{1,$LENGTH}\$") } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Role.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Role.kt index 39c3acd4..083ffc20 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Role.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/Role.kt @@ -17,5 +17,8 @@ package dev.usbharu.hideout.core.domain.model.actor enum class Role { - LOCAL, MODERATOR, ADMINISTRATOR, REMOTE + LOCAL, + MODERATOR, + ADMINISTRATOR, + REMOTE; } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationship.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationship.kt index f4244ca5..90c7b529 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationship.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationship.kt @@ -22,13 +22,20 @@ import dev.usbharu.hideout.core.domain.model.actor.ActorId import dev.usbharu.hideout.core.domain.model.instance.InstanceId import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventStorable -data class ActorInstanceRelationship( +class ActorInstanceRelationship( val actorId: ActorId, val instanceId: InstanceId, - private var blocking: Boolean = false, - private var muting: Boolean = false, - private var doNotSendPrivate: Boolean = false, + blocking: Boolean = false, + muting: Boolean = false, + doNotSendPrivate: Boolean = false, ) : DomainEventStorable() { + var doNotSendPrivate = doNotSendPrivate + private set + var muting = muting + private set + var blocking = blocking + private set + fun block(): ActorInstanceRelationship { addDomainEvent(ActorInstanceRelationshipDomainEventFactory(this).createEvent(BLOCK)) blocking = true @@ -62,12 +69,6 @@ data class ActorInstanceRelationship( return this } - fun isBlocking() = blocking - - fun isMuting() = muting - - fun isDoNotSendPrivate() = doNotSendPrivate - override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt index 0e8774ba..9f234f6e 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt @@ -1,17 +1,8 @@ package dev.usbharu.hideout.core.domain.model.filter -import dev.usbharu.hideout.core.domain.model.filter.FilterMode.* - class FilterKeyword( val id: FilterKeywordId, var keyword: FilterKeywordKeyword, val mode: FilterMode ) { - fun match(string: String): Boolean { - when (mode) { - WHOLE_WORD -> TODO() - REGEX -> TODO() - NONE -> TODO() - } - } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorInstanceRelationshipRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorInstanceRelationshipRepository.kt index ab7ab447..1843c88a 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorInstanceRelationshipRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorInstanceRelationshipRepository.kt @@ -41,9 +41,9 @@ class ExposedActorInstanceRelationshipRepository(override val domainEventPublish ActorInstanceRelationships.upsert { it[actorId] = actorInstanceRelationship.actorId.id it[instanceId] = actorInstanceRelationship.instanceId.instanceId - it[blocking] = actorInstanceRelationship.isBlocking() - it[muting] = actorInstanceRelationship.isMuting() - it[doNotSendPrivate] = actorInstanceRelationship.isDoNotSendPrivate() + it[blocking] = actorInstanceRelationship.blocking + it[muting] = actorInstanceRelationship.muting + it[doNotSendPrivate] = actorInstanceRelationship.doNotSendPrivate } } update(actorInstanceRelationship) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorRepository.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorRepository.kt index 8e184021..a11d20f3 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorRepository.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/exposedrepository/ExposedActorRepository.kt @@ -105,7 +105,7 @@ class ExposedActorRepository( object Actors : Table("actors") { val id = long("id") - val name = varchar("name", ActorName.length) + val name = varchar("name", ActorName.LENGTH) val domain = varchar("domain", Domain.LENGTH) val screenName = varchar("screen_name", ActorScreenName.length) val description = varchar("description", ActorDescription.length) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt index acfa8b11..6bf6ab63 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt @@ -1,3 +1,13 @@ package dev.usbharu.hideout.core.domain.model.actor -class ActorDescriptionTest \ No newline at end of file +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class ActorDescriptionTest { + @Test + fun actorDescriptionがlength以上なら無視される() { + val actorScreenName = ActorDescription("a".repeat(100000)) + + assertEquals(ActorDescription.length, actorScreenName.description.length) + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyIdTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyIdTest.kt new file mode 100644 index 00000000..8de564da --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorKeyIdTest.kt @@ -0,0 +1,25 @@ +package dev.usbharu.hideout.core.domain.model.actor + +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class ActorKeyIdTest { + @Test + fun keyIdはblankではいけない() { + assertThrows { + ActorKeyId("") + } + + assertThrows { + ActorKeyId(" ") + } + } + + @Test + fun keyIdがblankでなければ作成できる() { + assertDoesNotThrow { + ActorKeyId("aiueo") + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCountTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCountTest.kt new file mode 100644 index 00000000..451913b5 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCountTest.kt @@ -0,0 +1,20 @@ +package dev.usbharu.hideout.core.domain.model.actor + +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test + +class ActorPostsCountTest { + @Test + fun postsCountが負になることはない() { + org.junit.jupiter.api.assertThrows { + ActorPostsCount(-1) + } + } + + @Test + fun postsCountが正の数値なら設定できる() { + assertDoesNotThrow { + ActorPostsCount(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPrivateKeyTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPrivateKeyTest.kt new file mode 100644 index 00000000..68ff6877 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPrivateKeyTest.kt @@ -0,0 +1,18 @@ +package dev.usbharu.hideout.core.domain.model.actor + +import dev.usbharu.hideout.util.Base64Util +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import java.security.KeyPairGenerator + +class ActorPrivateKeyTest { + @Test + fun privateKeyから生成できる() { + val genKeyPair = KeyPairGenerator.getInstance("RSA").genKeyPair() + val actorPrivateKey = ActorPrivateKey.create(genKeyPair.private) + val key = "-----BEGIN PRIVATE KEY-----\n" + + Base64Util.encode(genKeyPair.private.encoded).chunked(64) + .joinToString("\n") + "\n-----END PRIVATE KEY-----" + assertEquals(key, actorPrivateKey.privateKey) + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPublicKeyTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPublicKeyTest.kt index 37bb1938..a2ec7f79 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPublicKeyTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPublicKeyTest.kt @@ -1,3 +1,18 @@ package dev.usbharu.hideout.core.domain.model.actor -class ActorPublicKeyTest \ No newline at end of file +import dev.usbharu.hideout.util.Base64Util +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import java.security.KeyPairGenerator + +class ActorPublicKeyTest { + @Test + fun publicKeyから生成できる() { + val genKeyPair = KeyPairGenerator.getInstance("RSA").genKeyPair() + val actorPublicKey = ActorPublicKey.create(genKeyPair.public) + val key = "-----BEGIN PUBLIC KEY-----\n" + + Base64Util.encode(genKeyPair.public.encoded).chunked(64) + .joinToString("\n") + "\n-----END PUBLIC KEY-----" + assertEquals(key, actorPublicKey.publicKey) + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCountTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCountTest.kt new file mode 100644 index 00000000..340d1563 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCountTest.kt @@ -0,0 +1,21 @@ +package dev.usbharu.hideout.core.domain.model.actor + +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class ActorRelationshipCountTest { + @Test + fun relationshipCountが負になることはない() { + assertThrows { + ActorRelationshipCount(-1) + } + } + + @Test + fun relationshipCountが正の数値なら設定できる() { + assertDoesNotThrow { + ActorRelationshipCount(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt index a8a67f4d..b50a6637 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt @@ -1,7 +1,9 @@ package dev.usbharu.hideout.core.domain.model.actor import dev.usbharu.hideout.core.domain.event.actor.ActorEvent +import dev.usbharu.hideout.core.domain.model.media.MediaId import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows import utils.AssertDomainEvent.assertContainsEvent import utils.AssertDomainEvent.assertEmpty @@ -127,4 +129,44 @@ class ActorsTest { assertContainsEvent(actor, ActorEvent.CHECK_UPDATE.eventName) } + + @Test + fun bannerが設定されたらupdateイベントが発生する() { + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) + + actor.setBannerUrl(MediaId(1), actor) + + assertContainsEvent(actor, ActorEvent.UPDATE.eventName) + } + + @Test + fun iconが設定されたらupdateイベントが発生する() { + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) + + actor.setIconUrl(MediaId(1), actor) + + assertContainsEvent(actor, ActorEvent.UPDATE.eventName) + } + + @Test + fun administratorロールを持っている人はroleを設定できる() { + val admin = TestActorFactory.create(roles = setOf(Role.ADMINISTRATOR)) + + val actor = TestActorFactory.create() + + assertDoesNotThrow { + actor.setRole(setOf(Role.MODERATOR), admin) + } + } + + @Test + fun administratorロールを持ってないとはroleを設定できない() { + val admin = TestActorFactory.create(roles = setOf(Role.MODERATOR)) + + val actor = TestActorFactory.create() + + assertThrows { + actor.setRole(setOf(Role.MODERATOR), admin) + } + } } \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActorFactory.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActorFactory.kt index 0fb9087d..57e3d0d5 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActorFactory.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActorFactory.kt @@ -34,9 +34,10 @@ object TestActorFactory { lastPostDate: Instant? = null, suspend: Boolean = false, alsoKnownAs: Set = emptySet(), - moveTo: ActorId? = null, + moveTo: Long? = null, emojiIds: Set = emptySet(), deleted: Boolean = false, + roles: Set = emptySet(), ): Actor { return runBlocking { Actor( @@ -62,12 +63,13 @@ object TestActorFactory { lastPostAt = lastPostDate, suspend = suspend, alsoKnownAs = alsoKnownAs, - moveTo = moveTo, + moveTo = moveTo?.let { ActorId(it) }, emojiIds = emojiIds, deleted = deleted, - roles = emptySet(), + roles = roles, icon = null, - banner = null + banner = null, + ) } } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationshipTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationshipTest.kt new file mode 100644 index 00000000..c24c559d --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actorinstancerelationship/ActorInstanceRelationshipTest.kt @@ -0,0 +1,77 @@ +package dev.usbharu.hideout.core.domain.model.actorinstancerelationship + +import dev.usbharu.hideout.core.domain.event.actorinstancerelationship.ActorInstanceRelationshipEvent +import dev.usbharu.hideout.core.domain.model.actor.ActorId +import dev.usbharu.hideout.core.domain.model.instance.InstanceId +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import utils.AssertDomainEvent.assertContainsEvent + +class ActorInstanceRelationshipTest { + @Test + fun blockするとBLOCKイベントが発生する() { + val actorInstanceRelationship = ActorInstanceRelationship(ActorId(1), InstanceId(2), false) + + actorInstanceRelationship.block() + + assertContainsEvent(actorInstanceRelationship, ActorInstanceRelationshipEvent.BLOCK.eventName) + assertTrue(actorInstanceRelationship.blocking) + } + + @Test + fun muteするとMUTEイベントが発生する() { + val actorInstanceRelationship = ActorInstanceRelationship(ActorId(1), InstanceId(2), false) + + actorInstanceRelationship.mute() + + assertContainsEvent(actorInstanceRelationship, ActorInstanceRelationshipEvent.MUTE.eventName) + assertTrue(actorInstanceRelationship.muting) + } + + @Test + fun unmuteするとUNMUTEイベントが発生する() { + val actorInstanceRelationship = ActorInstanceRelationship(ActorId(1), InstanceId(2), muting = true) + + actorInstanceRelationship.unmute() + + assertContainsEvent(actorInstanceRelationship, ActorInstanceRelationshipEvent.UNMUTE.eventName) + assertFalse(actorInstanceRelationship.muting) + } + + @Test + fun unblockで解除される() { + val actorInstanceRelationship = ActorInstanceRelationship(ActorId(1), InstanceId(2), true) + + actorInstanceRelationship.unblock() + + assertFalse(actorInstanceRelationship.blocking) + } + + @Test + fun doNotSendPrivateで設定される() { + val actorInstanceRelationship = ActorInstanceRelationship(ActorId(1), InstanceId(2)) + + actorInstanceRelationship.doNotSendPrivate() + + assertTrue(actorInstanceRelationship.doNotSendPrivate) + } + + @Test + fun doSendPrivateで解除される() { + val actorInstanceRelationship = ActorInstanceRelationship(ActorId(1), InstanceId(2), doNotSendPrivate = true) + + actorInstanceRelationship.doSendPrivate() + + assertFalse(actorInstanceRelationship.doNotSendPrivate) + } + + @Test + fun defaultで全部falseが作られる() { + val default = ActorInstanceRelationship.default(ActorId(1), InstanceId(2)) + + assertFalse(default.muting) + assertFalse(default.blocking) + assertFalse(default.doNotSendPrivate) + } +} \ No newline at end of file From 571211f99055f782d88418bd44d389d42326b570 Mon Sep 17 00:00:00 2001 From: usbharu Date: Mon, 17 Jun 2024 16:10:02 +0900 Subject: [PATCH 2/6] =?UTF-8?q?test:=20Filter=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/application/ApplicationId.kt | 6 +- .../model/application/ApplicationName.kt | 10 +- .../core/domain/model/emoji/EmojiId.kt | 6 +- .../core/domain/model/filter/Filter.kt | 12 +- .../core/domain/model/filter/FilterId.kt | 6 +- .../core/domain/model/filter/FilterName.kt | 12 +- .../usbharu/hideout/EqualsAndToStringTest.kt | 2 - .../model/application/ApplicationIdTest.kt | 20 +++ .../model/application/ApplicationNameTest.kt | 20 +++ .../model/application/ApplicationTest.kt | 13 ++ .../core/domain/model/emoji/EmojiIdTest.kt | 20 +++ .../core/domain/model/filter/FilterIdTest.kt | 20 +++ .../domain/model/filter/FilterNameTest.kt | 13 ++ .../core/domain/model/filter/FilterTest.kt | 138 ++++++++++++++++++ 14 files changed, 287 insertions(+), 11 deletions(-) create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt index 6daadcf0..41007237 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt @@ -17,4 +17,8 @@ package dev.usbharu.hideout.core.domain.model.application @JvmInline -value class ApplicationId(val id: Long) +value class ApplicationId(val id: Long) { + init { + require(0 <= id) + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt index 6b9f9a77..c57f4755 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt @@ -17,4 +17,12 @@ package dev.usbharu.hideout.core.domain.model.application @JvmInline -value class ApplicationName(val name: String) +value class ApplicationName(val name: String) { + init { + require(name.length <= LENGTH) + } + + companion object { + const val LENGTH = 300 + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt index 0ba25e1a..5cf4284d 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt @@ -17,4 +17,8 @@ package dev.usbharu.hideout.core.domain.model.emoji @JvmInline -value class EmojiId(val emojiId: Long) +value class EmojiId(val emojiId: Long) { + init { + require(0 <= emojiId) + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt index 41a835b3..dc26a715 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt @@ -21,6 +21,11 @@ class Filter( this.filterKeywords = filterKeywords } + /** + * フィルターを正規表現として表現したものを返します + * + * @return フィルターの正規表現 + */ fun compileFilter(): Regex { val words = mutableListOf() val wholeWords = mutableListOf() @@ -33,10 +38,11 @@ class Filter( NONE -> words.add(filterKeyword.keyword.keyword) } } + val wholeWordsRegex = wholeWords.takeIf { it.isNotEmpty() }?.joinToString("|", "\\b(", ")\\b") + val noneWordsRegex = words.takeIf { it.isNotEmpty() }?.joinToString("|", "(", ")") + val regex = regexes.takeIf { it.isNotEmpty() }?.joinToString("|", "(", ")") - return (wholeWords + regexes + wholeWords) - .joinToString("|") - .toRegex() + return listOfNotNull(wholeWordsRegex, noneWordsRegex, regex).joinToString("|").toRegex() } fun reconstructWith(filterKeywords: Set): Filter { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt index ce42a4c4..a9ee5ce4 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt @@ -1,4 +1,8 @@ package dev.usbharu.hideout.core.domain.model.filter @JvmInline -value class FilterId(val id: Long) +value class FilterId(val id: Long) { + init { + require(0 <= id) + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt index 09e424de..4f398bd3 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt @@ -1,4 +1,12 @@ package dev.usbharu.hideout.core.domain.model.filter -@JvmInline -value class FilterName(val name: String) + +class FilterName(name: String) { + + + val name = name.take(LENGTH) + + companion object { + const val LENGTH = 300 + } +} diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt index 96f3655f..d6325826 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt @@ -19,7 +19,6 @@ package dev.usbharu.hideout import com.fasterxml.jackson.module.kotlin.isKotlinClass import com.jparams.verifier.tostring.ToStringVerifier import com.jparams.verifier.tostring.preset.Presets -import dev.usbharu.hideout.core.domain.model.emoji.UnicodeEmoji import nl.jqno.equalsverifier.EqualsVerifier import nl.jqno.equalsverifier.Warning import nl.jqno.equalsverifier.internal.reflection.PackageScanner @@ -115,7 +114,6 @@ class EqualsAndToStringTest { .filterNot { it.superclass.isSealed } - .filterNot { it == UnicodeEmoji::class.java } .map { dynamicTest(it.name) { diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt new file mode 100644 index 00000000..e8c08c9d --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt @@ -0,0 +1,20 @@ +package dev.usbharu.hideout.core.domain.model.application + +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test + +class ApplicationIdTest { + @Test + fun applicationIdは0以上である必要がある() { + org.junit.jupiter.api.assertThrows { + ApplicationId(-1) + } + } + + @Test + fun applicationIdが0以上なら設定できる() { + assertDoesNotThrow { + ApplicationId(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt new file mode 100644 index 00000000..a9fddfa1 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt @@ -0,0 +1,20 @@ +package dev.usbharu.hideout.core.domain.model.application + +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test + +class ApplicationNameTest { + @Test + fun applicationNameがlength以上の時エラー() { + org.junit.jupiter.api.assertThrows { + ApplicationName("a".repeat(1000)) + } + } + + @Test + fun applicationNameがlength未満の時設定できる() { + assertDoesNotThrow { + ApplicationName("a".repeat(100)) + } + } +} diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt new file mode 100644 index 00000000..af51c836 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt @@ -0,0 +1,13 @@ +package dev.usbharu.hideout.core.domain.model.application + +import org.junit.jupiter.api.Test + +class ApplicationTest { + @Test + fun インスタンスを生成できる() { + Application( + ApplicationId(1), + ApplicationName("aiueo") + ) + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt new file mode 100644 index 00000000..c304294b --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt @@ -0,0 +1,20 @@ +package dev.usbharu.hideout.core.domain.model.emoji + +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test + +class EmojiIdTest { + @Test + fun emojiIdは0以上である必要がある() { + org.junit.jupiter.api.assertThrows { + EmojiId(-1) + } + } + + @Test + fun emojiIdは0以上なら設定できる() { + assertDoesNotThrow { + EmojiId(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt new file mode 100644 index 00000000..f91e4d5f --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt @@ -0,0 +1,20 @@ +package dev.usbharu.hideout.core.domain.model.filter + +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test + +class FilterIdTest { + @Test + fun filterIdは0以上である必要がある() { + org.junit.jupiter.api.assertThrows { + FilterId(-1) + } + } + + @Test + fun filterIdが0以上なら設定できる() { + assertDoesNotThrow { + FilterId(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt new file mode 100644 index 00000000..00bd6c84 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt @@ -0,0 +1,13 @@ +package dev.usbharu.hideout.core.domain.model.filter + +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals + +class FilterNameTest { + @Test + fun FilterNameがlength以上のときは無視される() { + val filterName = FilterName("a".repeat(1000)) + + assertEquals(FilterName.LENGTH, filterName.name.length) + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt new file mode 100644 index 00000000..9512f9c7 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt @@ -0,0 +1,138 @@ +package dev.usbharu.hideout.core.domain.model.filter + +import dev.usbharu.hideout.core.domain.model.actor.ActorId +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailHashedPassword +import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId +import org.junit.jupiter.api.Assertions.assertDoesNotThrow +import org.junit.jupiter.api.Test + +class FilterTest { + @Test + fun `setFilterKeywords 所有者のみ変更できる`() { + val filter = Filter.create( + id = FilterId(1), + userDetailId = UserDetailId(1), + name = FilterName("aiueo"), + filterContext = setOf(), + filterAction = FilterAction.HIDE, + filterKeywords = setOf() + ) + + val userDetail = UserDetail.create( + id = UserDetailId(1), + actorId = ActorId(1), + password = UserDetailHashedPassword(""), + autoAcceptFolloweeFollowRequest = false, + lastMigration = null + ) + + assertDoesNotThrow { + filter.setFilterKeywords( + setOf( + FilterKeyword( + FilterKeywordId(1), + FilterKeywordKeyword("keyword"), + FilterMode.NONE + ) + ), userDetail + ) + } + + } + + @Test + fun compileFilterで正規表現として表すことができるNONE() { + val filter = Filter( + id = FilterId(1), + userDetailId = UserDetailId(1), + name = FilterName("aiueo"), + filterContext = setOf(), + filterAction = FilterAction.HIDE, + filterKeywords = setOf( + FilterKeyword( + FilterKeywordId(1), + FilterKeywordKeyword("hoge"), + FilterMode.NONE + ) + ) + ) + + kotlin.test.assertEquals("(hoge)", filter.compileFilter().pattern) + + } + + @Test + fun compileFilterで正規表現として表すことができるWHOLE_WORD() { + val filter = Filter( + id = FilterId(1), + userDetailId = UserDetailId(1), + name = FilterName("aiueo"), + filterContext = setOf(), + filterAction = FilterAction.HIDE, + filterKeywords = setOf( + FilterKeyword( + FilterKeywordId(1), + FilterKeywordKeyword("hoge"), + FilterMode.WHOLE_WORD + ) + ) + ) + + kotlin.test.assertEquals("\\b(hoge)\\b", filter.compileFilter().pattern) + + } + + @Test + fun compileFilterで正規表現として表すことができるREGEX() { + val filter = Filter( + id = FilterId(1), + userDetailId = UserDetailId(1), + name = FilterName("aiueo"), + filterContext = setOf(), + filterAction = FilterAction.HIDE, + filterKeywords = setOf( + FilterKeyword( + FilterKeywordId(1), + FilterKeywordKeyword("hoge"), + FilterMode.REGEX + ) + ) + ) + + kotlin.test.assertEquals("(hoge)", filter.compileFilter().pattern) + + } + + @Test + fun compileFilterで正規表現として表すことができる() { + val filter = Filter( + id = FilterId(1), + userDetailId = UserDetailId(1), + name = FilterName("aiueo"), + filterContext = setOf(), + filterAction = FilterAction.HIDE, + filterKeywords = setOf( + FilterKeyword( + FilterKeywordId(1), + FilterKeywordKeyword("hoge"), + FilterMode.WHOLE_WORD + ), + FilterKeyword( + FilterKeywordId(2), + FilterKeywordKeyword("hoge"), + FilterMode.REGEX + ), + FilterKeyword( + FilterKeywordId(3), + FilterKeywordKeyword("hoge"), + FilterMode.NONE + ) + ) + ) + + kotlin.test.assertEquals("\\b(hoge)\\b|(hoge)|(hoge)", filter.compileFilter().pattern) + + } +} + From 1038e9e2419bd292ddfbe589ab0b2a008ba34ccc Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 18 Jun 2024 00:00:52 +0900 Subject: [PATCH 3/6] =?UTF-8?q?test:=20posts=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/core/domain/model/post/Post.kt | 3 - .../core/domain/model/post/PostTest.kt | 66 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt index 9bf9211c..98b4ee60 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt @@ -298,9 +298,6 @@ class Post( fun isAllow(actor: Actor, action: Action, resource: Post): Boolean { return when (action) { UPDATE -> { - if (actor.deleted) { - return true - } resource.actorId == actor.id || actor.roles.contains(Role.ADMINISTRATOR) || actor.roles.contains( Role.MODERATOR ) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt index c881d6e4..e9c8e8e1 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt @@ -9,6 +9,8 @@ import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows import utils.AssertDomainEvent.assertContainsEvent import utils.AssertDomainEvent.assertEmpty +import java.net.URI +import java.time.Instant import kotlin.test.assertEquals import kotlin.test.assertNull @@ -260,5 +262,69 @@ class PostTest { assertEmpty(post) } + @Test + fun sensitiveが変更されるとupdateイベントが発生する() { + val post = TestPostFactory.create() + val actor = TestActorFactory.create(id = post.actorId.id, publicKey = ActorPublicKey("")) + post.setSensitive(true, actor) + assertContainsEvent(post, PostEvent.UPDATE.eventName) + } + @Test + fun 削除されている場合sensitiveを変更できない() { + val post = TestPostFactory.create(deleted = true) + val actor = TestActorFactory.create(id = post.actorId.id, publicKey = ActorPublicKey("")) + assertThrows { + post.setSensitive(true, actor) + } + } + + @Test + fun sensitiveが変更されなかった場合イベントが発生しない() { + val post = TestPostFactory.create(overview = "aaaa") + val actor = TestActorFactory.create(id = post.actorId.id, publicKey = ActorPublicKey("")) + post.setSensitive(false, actor) + assertEmpty(post) + } + + @Test + fun hideがtrueの時emptyが帰る() { + val post = TestPostFactory.create(hide = true) + + assertEquals(PostContent.empty.text, post.text) + } + + @Test + fun hideがfalseの時textが返る() { + val post = TestPostFactory.create(hide = false, content = "aaaa") + + assertEquals("aaaa", post.text) + } + + @Test + fun `create actorが削除済みの時作成できない`() { + val actor = TestActorFactory.create(deleted = true) + assertThrows { + Post.create( + id = PostId(1), + actorId = actor.id, + overview = null, + content = PostContent.empty, + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = emptyList(), + visibleActors = emptySet(), + hide = false, + moveTo = null, + actor = actor + + ) + } + } } \ No newline at end of file From 34a049da5b2d9fc395d8b716ed1356928831121a Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 18 Jun 2024 15:58:49 +0900 Subject: [PATCH 4/6] =?UTF-8?q?test:=20Post=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/actor/ActorDescriptionTest.kt | 2 +- .../core/domain/model/post/PostTest.kt | 225 +++++++++++++++++- 2 files changed, 225 insertions(+), 2 deletions(-) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt index 6bf6ab63..c61538b0 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescriptionTest.kt @@ -8,6 +8,6 @@ class ActorDescriptionTest { fun actorDescriptionがlength以上なら無視される() { val actorScreenName = ActorDescription("a".repeat(100000)) - assertEquals(ActorDescription.length, actorScreenName.description.length) + assertEquals(ActorDescription.LENGTH, actorScreenName.description.length) } } \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt index e9c8e8e1..8487e528 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt @@ -4,6 +4,8 @@ import dev.usbharu.hideout.core.domain.event.post.PostEvent import dev.usbharu.hideout.core.domain.model.actor.ActorId import dev.usbharu.hideout.core.domain.model.actor.ActorPublicKey import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory +import dev.usbharu.hideout.core.domain.model.emoji.EmojiId +import dev.usbharu.hideout.core.domain.model.media.MediaId import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows @@ -327,4 +329,225 @@ class PostTest { ) } } -} \ No newline at end of file + + @Test + fun `create actorがsuspedの時visibilityがpublicの登校はunlistedになる`() { + val actor = TestActorFactory.create(suspend = true) + + val post = Post.create( + id = PostId(1), + actorId = actor.id, + overview = null, + content = PostContent.empty, + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = emptyList(), + visibleActors = emptySet(), + hide = false, + moveTo = null, + actor = actor + ) + + assertEquals(Visibility.UNLISTED, post.visibility) + } + + @Test + fun `create actorがsuspedの時visibilityがunlistedの登校は変わらない`() { + val actor = TestActorFactory.create(suspend = true) + + val post = Post.create( + id = PostId(1), + actorId = actor.id, + overview = null, + content = PostContent.empty, + createdAt = Instant.now(), + visibility = Visibility.UNLISTED, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = emptyList(), + visibleActors = emptySet(), + hide = false, + moveTo = null, + actor = actor + ) + + assertEquals(Visibility.UNLISTED, post.visibility) + } + + @Test + fun `create 作成できる`() { + val actor = TestActorFactory.create(suspend = true) + + + assertDoesNotThrow { + + Post.create( + id = PostId(1), + actorId = actor.id, + overview = null, + content = PostContent.empty, + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = emptyList(), + visibleActors = emptySet(), + hide = false, + moveTo = null, + actor = actor + ) + } + } + + @Test + fun `create 作成できる2`() { + val actor = TestActorFactory.create(suspend = true) + + + assertDoesNotThrow { + + Post.create( + id = PostId(1), + actorId = actor.id, + content = PostContent.empty, + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = emptyList(), + actor = actor + ) + } + } + + @Test + fun `emojiIds hideがtrueの時empty`() { + val actor = TestActorFactory.create() + val emojiIds = listOf(EmojiId(1), EmojiId(2)) + val post = Post.create( + id = PostId(1), + actorId = actor.id, + content = PostContent("aaa", "aaa", emojiIds), + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = emptyList(), + actor = actor, + hide = true + ) + + assertEquals(PostContent.empty.emojiIds, post.emojiIds) + + } + + @Test + fun `emojiIds hideがfalseの時中身が返される`() { + val actor = TestActorFactory.create() + val emojiIds = listOf(EmojiId(1), EmojiId(2)) + val post = Post.create( + id = PostId(1), + actorId = actor.id, + content = PostContent("aaa", "aaa", emojiIds), + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = emptyList(), + actor = actor, + hide = false + ) + + assertEquals(emojiIds, post.emojiIds) + } + + @Test + fun `reconstructWith 与えた引数で上書きされる`() { + val post = TestPostFactory.create() + val mediaIds = listOf(MediaId(1)) + val visibleActors = setOf((ActorId(2))) + val emojis = listOf(EmojiId(3)) + val reconstructWith = post.reconstructWith(mediaIds, emojis, visibleActors) + + assertEquals(mediaIds, reconstructWith.mediaIds) + assertEquals(visibleActors, reconstructWith.visibleActors) + assertEquals(emojis, reconstructWith.emojiIds) + } + + @Test + fun `mediaIds hideがtrueの時emptyが返される`() { + val actor = TestActorFactory.create() + val emojiIds = listOf(EmojiId(1), EmojiId(2)) + val mediaIds = listOf(MediaId(1)) + val post = Post.create( + id = PostId(1), + actorId = actor.id, + content = PostContent("aaa", "aaa", emojiIds), + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = mediaIds, + actor = actor, + hide = true + ) + + assertEquals(emptyList(), post.mediaIds) + + } + + @Test + fun `mediaIds hideがfalseの時中身が返される`() { + val actor = TestActorFactory.create() + val emojiIds = listOf(EmojiId(1), EmojiId(2)) + val mediaIds = listOf(MediaId(2)) + val post = Post.create( + id = PostId(1), + actorId = actor.id, + content = PostContent("aaa", "aaa", emojiIds), + createdAt = Instant.now(), + visibility = Visibility.PUBLIC, + url = URI.create("https://example.com"), + repostId = null, + replyId = null, + sensitive = false, + apId = URI.create("https://example.com"), + deleted = false, + mediaIds = mediaIds, + actor = actor, + hide = false + ) + + assertEquals(mediaIds, post.mediaIds) + } +} From 19ee832a6f359ac92a7419351197bd061a5b56a8 Mon Sep 17 00:00:00 2001 From: usbharu Date: Wed, 19 Jun 2024 10:23:03 +0900 Subject: [PATCH 5/6] =?UTF-8?q?test:=20Post=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/core/domain/model/post/Post.kt | 1 + .../core/domain/model/post/PostTest.kt | 134 ++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt index cca33575..389aa714 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt @@ -184,6 +184,7 @@ class Post( } fun restore(content: PostContent, overview: PostOverview?, mediaIds: List) { + require(deleted) deleted = false this.content = content this.overview = overview diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt index 8487e528..7c7be023 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt @@ -6,6 +6,7 @@ import dev.usbharu.hideout.core.domain.model.actor.ActorPublicKey import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory import dev.usbharu.hideout.core.domain.model.emoji.EmojiId import dev.usbharu.hideout.core.domain.model.media.MediaId +import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows @@ -14,6 +15,7 @@ import utils.AssertDomainEvent.assertEmpty import java.net.URI import java.time.Instant import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertNull class PostTest { @@ -550,4 +552,136 @@ class PostTest { assertEquals(mediaIds, post.mediaIds) } + + @Test + fun `delete deleteイベントが発生する`() { + + val actor = TestActorFactory.create() + val post = TestPostFactory.create(deleted = false, actorId = actor.id.id) + post.delete(actor) + assertContainsEvent(post, PostEvent.DELETE.eventName) + } + + @Test + fun `delete すでにdeletedがtrueの時deleteイベントは発生しない`() { + + val actor = TestActorFactory.create() + val post = TestPostFactory.create(deleted = true, actorId = actor.id.id) + post.delete(actor) + assertEmpty(post) + } + + @Test + fun `delete contentがemptyにoverviewがnullにmediaIdsがemptyになる`() { + val actor = TestActorFactory.create() + val post = TestPostFactory.create(deleted = false, actorId = actor.id.id) + post.delete(actor) + assertEquals(PostContent.empty, post.content) + assertNull(post.overview) + assertEquals(emptyList(), post.mediaIds) + assertTrue(post.deleted) + } + + @Test + fun `checkUpdate CHECKUPDATEイベントが発生する`() { + val post = TestPostFactory.create() + + post.checkUpdate() + + assertContainsEvent(post, PostEvent.CHECK_UPDATE.eventName) + } + + @Test + fun `restore 指定された引数で再構成されCHECKUPDATEイベントが発生する`() { + val post = TestPostFactory.create(deleted = true) + + val postContent = PostContent("aiueo", "aiueo", listOf(EmojiId(1))) + val overview = PostOverview("overview") + val mediaIds = listOf(MediaId(1)) + post.restore( + postContent, + overview, + mediaIds + ) + + assertContainsEvent(post, PostEvent.CHECK_UPDATE.eventName) + assertEquals(postContent, post.content) + assertEquals(overview, post.overview) + assertEquals(mediaIds, post.mediaIds) + } + + @Test + fun deletedがfalseの時失敗する() { + val post = TestPostFactory.create(deleted = false) + + val postContent = PostContent("aiueo", "aiueo", listOf(EmojiId(1))) + val overview = PostOverview("overview") + val mediaIds = listOf(MediaId(1)) + assertThrows { + post.restore( + postContent, + overview, + mediaIds + ) + } + } + + @Test + fun `addMediaIds deletedがtrueの時失敗する`() { + val post = TestPostFactory.create(deleted = true) + val actor = TestActorFactory.create(id = post.actorId.id) + + assertThrows { + post.addMediaIds(listOf(MediaId(1)), actor) + } + } + + @Test + fun `addMediaIds updateイベントが発生する`() { + val post = TestPostFactory.create(deleted = false) + val actor = TestActorFactory.create(id = post.actorId.id) + + post.addMediaIds(listOf(MediaId(2)), actor) + assertContainsEvent(post, PostEvent.UPDATE.eventName) + } + + @Test + fun `hide hideがtrueになる`() { + val post = TestPostFactory.create(hide = false) + + post.hide() + + assertTrue(post.hide) + } + + @Test + fun `show hideがfalseになる`() { + val post = TestPostFactory.create(hide = true) + + post.show() + + assertFalse(post.hide) + } + + @Test + fun `moveTo すでに設定されている場合は失敗する`() { + val post = TestPostFactory.create(moveTo = 100) + val actor = TestActorFactory.create(post.actorId.id) + + assertThrows { + post.moveTo(PostId(2), actor) + } + } + + @Test + fun `moveTo moveToが設定される`() { + val post = TestPostFactory.create(moveTo = null) + val actor = TestActorFactory.create(post.actorId.id) + + assertDoesNotThrow { + post.moveTo(PostId(2), actor) + } + + assertEquals(PostId(2), post.moveTo) + } } From 1de6aff598eaea9704386e93b14757affbe99638 Mon Sep 17 00:00:00 2001 From: usbharu Date: Wed, 19 Jun 2024 10:55:49 +0900 Subject: [PATCH 6/6] =?UTF-8?q?test:=20LocalActorMigrationCheckDomainServi?= =?UTF-8?q?ceImpl=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...calActorMigrationCheckDomainServiceImpl.kt | 2 +- ...ctorMigrationCheckDomainServiceImplTest.kt | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImplTest.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImpl.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImpl.kt index f74bf530..933f7201 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImpl.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImpl.kt @@ -34,7 +34,7 @@ class LocalActorMigrationCheckDomainServiceImpl : LocalActorMigrationCheckDomain return AccountMigrationCheck.AlreadyMoved("${from.name}@${from.domain} was move to ${from.moveTo}") } - if (to.alsoKnownAs.contains(to.id).not()) { + if (to.alsoKnownAs.contains(from.id).not()) { return AccountMigrationCheck.AlsoKnownAsNotFound("${to.id} has ${to.alsoKnownAs}") } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImplTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImplTest.kt new file mode 100644 index 00000000..7428222c --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/local/LocalActorMigrationCheckDomainServiceImplTest.kt @@ -0,0 +1,71 @@ +package dev.usbharu.hideout.core.domain.service.actor.local + +import dev.usbharu.hideout.core.domain.model.actor.ActorId +import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Assertions.assertInstanceOf +import org.junit.jupiter.api.Test + +class LocalActorMigrationCheckDomainServiceImplTest { + @Test + fun 自分自身に引っ越しできない(): Unit = runTest { + + val from = TestActorFactory.create() + val to = TestActorFactory.create() + + val localActorMigrationCheckDomainServiceImpl = LocalActorMigrationCheckDomainServiceImpl() + + val canAccountMigration = localActorMigrationCheckDomainServiceImpl.canAccountMigration(from, from) + + assertInstanceOf(AccountMigrationCheck.SelfReferences::class.java, canAccountMigration) + } + + @Test + fun 引越し先が引っ越している場合は引っ越しできない(): Unit = runTest { + + val from = TestActorFactory.create() + val to = TestActorFactory.create(moveTo = 100) + + val localActorMigrationCheckDomainServiceImpl = LocalActorMigrationCheckDomainServiceImpl() + + val canAccountMigration = localActorMigrationCheckDomainServiceImpl.canAccountMigration(from, to) + + assertInstanceOf(AccountMigrationCheck.AlreadyMoved::class.java, canAccountMigration) + } + + @Test + fun 自分自身が引っ越している場合は引っ越しできない() = runTest { + val from = TestActorFactory.create(moveTo = 100) + val to = TestActorFactory.create() + + val localActorMigrationCheckDomainServiceImpl = LocalActorMigrationCheckDomainServiceImpl() + + val canAccountMigration = localActorMigrationCheckDomainServiceImpl.canAccountMigration(from, to) + + assertInstanceOf(AccountMigrationCheck.AlreadyMoved::class.java, canAccountMigration) + } + + @Test + fun 引越し先のalsoKnownAsに引越し元が含まれてない場合失敗する() = runTest { + val from = TestActorFactory.create() + val to = TestActorFactory.create(alsoKnownAs = setOf(ActorId(100))) + + val localActorMigrationCheckDomainServiceImpl = LocalActorMigrationCheckDomainServiceImpl() + + val canAccountMigration = localActorMigrationCheckDomainServiceImpl.canAccountMigration(from, to) + + assertInstanceOf(AccountMigrationCheck.AlsoKnownAsNotFound::class.java, canAccountMigration) + } + + @Test + fun 正常に設定されている場合は成功する() = runTest { + val from = TestActorFactory.create() + val to = TestActorFactory.create(alsoKnownAs = setOf(from.id, ActorId(100))) + + val localActorMigrationCheckDomainServiceImpl = LocalActorMigrationCheckDomainServiceImpl() + + val canAccountMigration = localActorMigrationCheckDomainServiceImpl.canAccountMigration(from, to) + + assertInstanceOf(AccountMigrationCheck.CanAccountMigration::class.java, canAccountMigration) + } +} \ No newline at end of file