test: DefaultPostReadAccessControlにテストを追加

This commit is contained in:
usbharu 2024-09-21 15:56:30 +09:00
parent d2b69b162a
commit 9bc5f01c9b
Signed by: usbharu
GPG Key ID: 95CBCF7046307B77
3 changed files with 242 additions and 7 deletions

View File

@ -77,12 +77,12 @@ class DefaultPostReadAccessControl(private val relationshipRepository: Relations
override suspend fun areAllows(postList: List<Post>, principal: Principal): List<Post> {
val actorIds = postList.map { it.actorId }
val relationshipList =
val blockedByList =
relationshipRepository.findByActorIdsAndTargetIdAndBlocking(actorIds, principal.actorId, true)
.map { it.actorId }
val inverseRelationshipList =
val followingList =
relationshipRepository.findByActorIdAndTargetIdsAndFollowing(principal.actorId, actorIds, true)
.map { it.actorId }
.map { it.targetActorId }
fun internalAllow(post: Post): Boolean {
// ポスト主は無条件で見れる
@ -90,7 +90,7 @@ class DefaultPostReadAccessControl(private val relationshipRepository: Relations
return true
}
if (relationshipList.contains(post.actorId)) {
if (blockedByList.contains(post.actorId)) {
return false
}
@ -106,7 +106,7 @@ class DefaultPostReadAccessControl(private val relationshipRepository: Relations
return true
}
if (post.visibility == Visibility.FOLLOWERS && inverseRelationshipList.contains(principal.actorId)) {
if (post.visibility == Visibility.FOLLOWERS && followingList.contains(post.actorId)) {
return true
}
return false

View File

@ -62,6 +62,25 @@ class FilterDomainServiceTest {
@Test
fun applyAll_filterContextの適用範囲にフィルターが適用される() {
val postList = listOf(
TestPostFactory.create(),
TestPostFactory.create(),
TestPostFactory.create(content = "aaaaaaaaaa"),
TestPostFactory.create(),
TestPostFactory.create()
)
val filter = Filter(
FilterId(1),
userDetailId = UserDetailId(1),
FilterName("filter"),
setOf(FilterContext.HOME),
filterAction = FilterAction.HIDE,
setOf(FilterKeyword(FilterKeywordId(1), FilterKeywordKeyword("test"), FilterMode.NONE))
)
val filteredPosts = FilterDomainService().applyAll(postList, FilterContext.HOME, filters = listOf(filter))
assertEquals(5, filteredPosts.size)
}
}

View File

@ -1,6 +1,7 @@
package dev.usbharu.hideout.core.domain.service.post
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.domain.model.post.TestPostFactory
import dev.usbharu.hideout.core.domain.model.post.Visibility
import dev.usbharu.hideout.core.domain.model.relationship.Relationship
@ -13,12 +14,14 @@ import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.whenever
import org.mockito.kotlin.*
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
@ExtendWith(MockitoExtension::class)
class DefaultPostReadAccessControlTest {
@ -155,4 +158,217 @@ class DefaultPostReadAccessControlTest {
assertFalse(actual)
}
@Test
fun ポスト主は無条件で見れる() = runTest {
val actual = service.isAllow(
TestPostFactory.create(actorId = 1, visibility = Visibility.DIRECT),
LocalUser(ActorId(1), UserDetailId(1), Acct("test", "example.com"))
)
assertTrue(actual)
}
@Test
fun areAllows_ポスト主は無条件で見れる() = runTest {
whenever(
relationshipRepository.findByActorIdsAndTargetIdAndBlocking(
any(),
anyValueClass(),
eq(true)
)
).doReturn(
emptyList()
)
whenever(
relationshipRepository.findByActorIdAndTargetIdsAndFollowing(
anyValueClass(),
any(),
eq(true)
)
).doReturn(
emptyList()
)
val postList = listOf<Post>(
TestPostFactory.create(actorId = 1, visibility = Visibility.DIRECT),
TestPostFactory.create(actorId = 1, visibility = Visibility.FOLLOWERS),
TestPostFactory.create(actorId = 1, visibility = Visibility.UNLISTED),
TestPostFactory.create(actorId = 1, visibility = Visibility.PUBLIC),
)
val actual = service.areAllows(postList, LocalUser(ActorId(1), UserDetailId(1), Acct("test", "example.com")))
assertContentEquals(postList, actual)
}
@Test
fun areFollows_ブロックされていたら見れない() = runTest {
whenever(
relationshipRepository.findByActorIdsAndTargetIdAndBlocking(
any(),
anyValueClass(),
eq(true)
)
).doReturn(
listOf(Relationship.default(actorId = ActorId(2), targetActorId = ActorId(1)))
)
whenever(
relationshipRepository.findByActorIdAndTargetIdsAndFollowing(
anyValueClass(),
any(),
eq(true)
)
).doReturn(
emptyList()
)
val postList = listOf<Post>(
TestPostFactory.create(actorId = 1, visibility = Visibility.DIRECT),
TestPostFactory.create(actorId = 2, visibility = Visibility.FOLLOWERS),
TestPostFactory.create(actorId = 1, visibility = Visibility.UNLISTED),
TestPostFactory.create(actorId = 1, visibility = Visibility.PUBLIC),
)
val actual = service.areAllows(postList, LocalUser(ActorId(1), UserDetailId(1), Acct("test", "example.com")))
assertEquals(3, actual.size)
assertAll(actual.map { { assertEquals(1, it.actorId.id) } })
}
@Test
fun areAllows_PUBLICとUNLISTEDは見れる() = runTest {
whenever(
relationshipRepository.findByActorIdsAndTargetIdAndBlocking(
any(),
anyValueClass(),
eq(true)
)
).doReturn(
emptyList()
)
whenever(
relationshipRepository.findByActorIdAndTargetIdsAndFollowing(
anyValueClass(),
any(),
eq(true)
)
).doReturn(
emptyList()
)
val postList = listOf<Post>(
TestPostFactory.create(actorId = 3, visibility = Visibility.DIRECT),
TestPostFactory.create(actorId = 3, visibility = Visibility.FOLLOWERS),
TestPostFactory.create(actorId = 3, visibility = Visibility.UNLISTED),
TestPostFactory.create(actorId = 3, visibility = Visibility.PUBLIC),
)
val actual = service.areAllows(postList, LocalUser(ActorId(1), UserDetailId(1), Acct("test", "example.com")))
assertEquals(2, actual.size)
kotlin.test.assertTrue(actual.all { it.visibility == Visibility.PUBLIC || it.visibility == Visibility.UNLISTED })
}
@Test
fun areAllows_Anonymousは見れない() = runTest {
whenever(
relationshipRepository.findByActorIdsAndTargetIdAndBlocking(
any(),
anyValueClass(),
eq(true)
)
).doReturn(
emptyList()
)
whenever(
relationshipRepository.findByActorIdAndTargetIdsAndFollowing(
anyValueClass(),
any(),
eq(true)
)
).doReturn(
emptyList()
)
val postList = listOf<Post>(
TestPostFactory.create(actorId = 3, visibility = Visibility.DIRECT),
TestPostFactory.create(actorId = 3, visibility = Visibility.FOLLOWERS),
TestPostFactory.create(actorId = 3, visibility = Visibility.UNLISTED),
TestPostFactory.create(actorId = 3, visibility = Visibility.PUBLIC),
)
val actual = service.areAllows(postList, Anonymous)
assertEquals(2, actual.size)
kotlin.test.assertTrue(actual.all { it.visibility == Visibility.PUBLIC || it.visibility == Visibility.UNLISTED })
}
@Test
fun areAllows_DIRECTはVisibleActorsに入っていたら見れる() = runTest {
whenever(
relationshipRepository.findByActorIdsAndTargetIdAndBlocking(
any(),
anyValueClass(),
eq(true)
)
).doReturn(
emptyList()
)
whenever(
relationshipRepository.findByActorIdAndTargetIdsAndFollowing(
anyValueClass(),
any(),
eq(true)
)
).doReturn(
emptyList()
)
val postList = listOf<Post>(
TestPostFactory.create(id = 1, actorId = 3, visibility = Visibility.DIRECT, visibleActors = listOf(1)),
TestPostFactory.create(id = 2, actorId = 3, visibility = Visibility.DIRECT, visibleActors = listOf(2)),
TestPostFactory.create(id = 3, actorId = 3, visibility = Visibility.DIRECT, visibleActors = listOf(3)),
TestPostFactory.create(
id = 4,
actorId = 3,
visibility = Visibility.DIRECT,
visibleActors = listOf(1, 2, 3, 4)
),
)
val actual = service.areAllows(postList, LocalUser(ActorId(1), UserDetailId(1), Acct("test", "example.com")))
assertEquals(2, actual.size)
kotlin.test.assertTrue(actual.all { it.id.id == 1L || it.id.id == 4L })
}
@Test
fun areAllows_FOLLOWERSはフォローされていたら見れる() = runTest {
whenever(
relationshipRepository.findByActorIdsAndTargetIdAndBlocking(
any(),
anyValueClass(),
eq(true)
)
).doReturn(
emptyList()
)
whenever(
relationshipRepository.findByActorIdAndTargetIdsAndFollowing(
anyValueClass(),
any(),
eq(true)
)
).doReturn(
listOf(Relationship.default(actorId = ActorId(1), targetActorId = ActorId(2)))
)
val postList = listOf<Post>(
TestPostFactory.create(actorId = 3, visibility = Visibility.FOLLOWERS),
TestPostFactory.create(actorId = 2, visibility = Visibility.FOLLOWERS),
TestPostFactory.create(actorId = 3, visibility = Visibility.FOLLOWERS),
TestPostFactory.create(actorId = 3, visibility = Visibility.FOLLOWERS),
)
val actual = service.areAllows(postList, LocalUser(ActorId(1), UserDetailId(1), Acct("test", "example.com")))
assertEquals(1, actual.size)
assertAll(actual.map { { assertEquals(2, it.actorId.id) } })
}
}