diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteService.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteService.kt index 29efd890..b36aeec4 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteService.kt @@ -1,7 +1,6 @@ package dev.usbharu.hideout.service.activitypub import dev.usbharu.hideout.domain.model.PostEntity -import dev.usbharu.hideout.domain.model.ap.Note import dev.usbharu.hideout.domain.model.job.DeliverPostJob import kjob.core.job.JobProps diff --git a/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt new file mode 100644 index 00000000..0c44a9f7 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt @@ -0,0 +1,91 @@ +@file:OptIn(ExperimentalCoroutinesApi::class) +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") + +package dev.usbharu.hideout.service.activitypub + +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.config.ConfigData +import dev.usbharu.hideout.domain.model.PostEntity +import dev.usbharu.hideout.domain.model.UserEntity +import dev.usbharu.hideout.domain.model.job.DeliverPostJob +import dev.usbharu.hideout.service.impl.UserService +import dev.usbharu.hideout.service.job.JobQueueParentService +import io.ktor.client.* +import io.ktor.client.engine.mock.* +import kjob.core.job.JobProps +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.json.Json +import org.junit.jupiter.api.Test +import org.mockito.Mockito.eq +import org.mockito.kotlin.* +import utils.JsonObjectMapper +import kotlin.test.assertEquals + +class ActivityPubNoteServiceImplTest { + @Test + fun `createPost 新しい投稿`() = runTest { + val followers = listOf( + UserEntity( + 2L, + "follower", + "follower.example.com", + "followerUser", + "test follower user", + "https://follower.example.com/inbox", + "https://follower.example.com/outbox", + "https://follower.example.com" + ), + UserEntity( + 3L, + "follower2", + "follower2.example.com", + "follower2User", + "test follower2 user", + "https://follower2.example.com/inbox", + "https://follower2.example.com/outbox", + "https:.//follower2.example.com" + ) + ) + val userService = mock { + onBlocking { findById(eq(1L)) } doReturn UserEntity( + 1L, + "test", + "example.com", + "testUser", + "test user", + "https://example.com/inbox", + "https://example.com/outbox", + "https:.//example.com" + ) + onBlocking { findFollowersById(eq(1L)) } doReturn followers + } + val jobQueueParentService = mock() + val activityPubNoteService = ActivityPubNoteServiceImpl(mock(), jobQueueParentService, userService) + val postEntity = PostEntity( + 1L, 1L, null, "test text", 1L, 1, "https://example.com" + ) + activityPubNoteService.createNote(postEntity) + verify(jobQueueParentService,times(2)).schedule(eq(DeliverPostJob), any()) + } + + @Test + fun `createPostJob 新しい投稿のJob`() = runTest { + Config.configData = ConfigData(objectMapper = JsonObjectMapper.objectMapper) + val httpClient = HttpClient(MockEngine { httpRequestData -> + assertEquals("https://follower.example.com/inbox", httpRequestData.url.toString()) + respondOk() + }) + val activityPubNoteService = ActivityPubNoteServiceImpl(httpClient,mock(),mock()) + activityPubNoteService.createNoteJob( + JobProps( + data = mapOf( + DeliverPostJob.actor.name to "https://follower.example.com", + DeliverPostJob.post.name to "{\"id\":\"https://example.com\",\"type\":\"Note\",\"attributedTo\":\"https://example.com\",\"content\":\"test text\",\"to\":[\"https://www.w3.org/ns/activitystreams#Public\"],\"cc\":[\"https://example.com/followers\"],\"published\":\"2021-01-01T00:00:00.000Z\",\"url\":\"https://example.com\"}", + DeliverPostJob.inbox.name to "https://follower.example.com/inbox" + ), + json = Json + ) + ) + } +}