From a2420622c5ef630b8a2edda25fbb44afaf04c5a5 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sun, 5 Nov 2023 13:28:04 +0900 Subject: [PATCH] =?UTF-8?q?test:=20actor=E3=81=AE=E3=82=A8=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=9D=E3=82=A4=E3=83=B3=E3=83=88=E3=81=AE=E3=83=86?= =?UTF-8?q?=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 --- .../api/actor/UserAPControllerImplTest.kt | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImplTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImplTest.kt new file mode 100644 index 00000000..9520eac2 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/actor/UserAPControllerImplTest.kt @@ -0,0 +1,94 @@ +package dev.usbharu.hideout.activitypub.interfaces.api.actor + +import dev.usbharu.hideout.activitypub.domain.model.Image +import dev.usbharu.hideout.activitypub.domain.model.Key +import dev.usbharu.hideout.activitypub.domain.model.Person +import dev.usbharu.hideout.activitypub.service.objects.user.APUserService +import dev.usbharu.hideout.application.config.ActivityPubConfig +import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException +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.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.get +import org.springframework.test.web.servlet.post +import org.springframework.test.web.servlet.setup.MockMvcBuilders + +@ExtendWith(MockitoExtension::class) +class UserAPControllerImplTest { + + private lateinit var mockMvc: MockMvc + + @Mock + private lateinit var apUserService: APUserService + + @InjectMocks + private lateinit var userAPControllerImpl: UserAPControllerImpl + + @BeforeEach + fun setUp() { + mockMvc = MockMvcBuilders.standaloneSetup(userAPControllerImpl).build() + } + + @Test + fun `userAp 存在するユーザーにGETするとPersonが返ってくる`(): Unit = runTest { + val person = Person( + name = "Hoge", + id = "https://example.com/users/hoge", + preferredUsername = "hoge", + summary = "fuga", + inbox = "https://example.com/users/hoge/inbox", + outbox = "https://example.com/users/hoge/outbox", + url = "https://example.com/users/hoge", + icon = Image( + name = "icon", + mediaType = "image/jpeg", + url = "https://example.com/users/hoge/icon.jpg" + ), + publicKey = Key( + name = "Public Key", + id = "https://example.com/users/hoge#pubkey", + owner = "https://example.com/users/hoge", + publicKeyPem = "-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----", + type = emptyList() + ), + endpoints = mapOf("sharedInbox" to "https://example.com/inbox"), + followers = "https://example.com/users/hoge/followers", + following = "https://example.com/users/hoge/following" + ) + whenever(apUserService.getPersonByName(eq("hoge"))).doReturn(person) + + val objectMapper = ActivityPubConfig().objectMapper() + + mockMvc + .get("/users/hoge") + .asyncDispatch() + .andExpect { status { isOk() } } + .andExpect { content { this.json(objectMapper.writeValueAsString(person)) } } + } + + @Test + fun `userAP 存在しないユーザーにGETすると404が返ってくる`() = runTest { + whenever(apUserService.getPersonByName(eq("fuga"))).doThrow(FailedToGetResourcesException::class) + + mockMvc + .get("/users/fuga") + .asyncDispatch() + .andExpect { status { isNotFound() } } + } + + @Test + fun `userAP POSTすると405が返ってくる`() { + mockMvc + .post("/users/hoge") + .andExpect { status { isMethodNotAllowed() } } + } +}