From bafd9669096cb5ec36bb0d8fae93db085505035f Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 21 Apr 2023 01:07:24 +0900 Subject: [PATCH] =?UTF-8?q?test:=20post=E3=81=AE=E4=BD=9C=E6=88=90?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=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 --- .../activitypub/ActivityPubNoteService.kt | 1 - .../ActivityPubNoteServiceImplTest.kt | 91 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubNoteServiceImplTest.kt 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 + ) + ) + } +}