From 3ed6b20dc51f8e14d221f10801002a0ffcfbce73 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 5 Nov 2023 00:24:56 +0900 Subject: [PATCH] =?UTF-8?q?test:=20inbox=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 --- .../api/inbox/InboxControllerImplTest.kt | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt new file mode 100644 index 00000000..75d76521 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt @@ -0,0 +1,104 @@ +package dev.usbharu.hideout.activitypub.interfaces.api.inbox + +import dev.usbharu.hideout.activitypub.domain.exception.JsonParseException +import dev.usbharu.hideout.activitypub.interfaces.api.common.ActivityPubStringResponse +import dev.usbharu.hideout.activitypub.service.common.APService +import dev.usbharu.hideout.activitypub.service.common.ActivityType +import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException +import io.ktor.http.* +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +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.doThrow +import org.mockito.kotlin.eq +import org.mockito.kotlin.whenever +import org.springframework.http.MediaType +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.post +import org.springframework.test.web.servlet.setup.MockMvcBuilders + +@ExtendWith(MockitoExtension::class) +class InboxControllerImplTest { + + private lateinit var mockMvc: MockMvc + + @Mock + private lateinit var apService: APService + + @InjectMocks + private lateinit var inboxController: InboxControllerImpl + + @BeforeEach + fun setUp() { + mockMvc = MockMvcBuilders.standaloneSetup(inboxController).build() + } + + @Test + fun `inbox 正常なPOSTリクエストをしたときAcceptが返ってくる`() = runTest { + + + val json = """{"type":"Follow"}""" + whenever(apService.parseActivity(eq(json))).doReturn(ActivityType.Follow) + whenever(apService.processActivity(eq(json), eq(ActivityType.Follow))).doReturn( + ActivityPubStringResponse( + HttpStatusCode.Accepted, "" + ) + ) + + mockMvc + .post("/inbox") { + content = json + contentType = MediaType.APPLICATION_JSON + } + .asyncDispatch() + .andExpect { + status { isAccepted() } + } + + } + + @Test + fun `inbox parseActivityに失敗したときAcceptが返ってくる`() = runTest { + val json = """{"type":"Hoge"}""" + whenever(apService.parseActivity(eq(json))).doThrow(JsonParseException::class) + + mockMvc + .post("/inbox") { + content = json + contentType = MediaType.APPLICATION_JSON + } + .asyncDispatch() + .andExpect { + status { isAccepted() } + } + + } + + @Test + fun `inbox processActivityに失敗したときAcceptが返ってくる`() = runTest { + val json = """{"type":"Follow"}""" + whenever(apService.parseActivity(eq(json))).doReturn(ActivityType.Follow) + whenever( + apService.processActivity( + eq(json), + eq(ActivityType.Follow) + ) + ).doThrow(FailedToGetResourcesException::class) + + mockMvc + .post("/inbox") { + content = json + contentType = MediaType.APPLICATION_JSON + } + .asyncDispatch() + .andExpect { + status { isAccepted() } + } + + } +}