From 948f24c451648b34f085d00b5486622b3d8f7954 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:05:28 +0900 Subject: [PATCH] =?UTF-8?q?test:=20MastodonAccountApiController=E3=81=AE?= =?UTF-8?q?=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 --- .../MastodonAccountApiControllerTest.kt | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiControllerTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiControllerTest.kt b/src/test/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiControllerTest.kt new file mode 100644 index 00000000..ff114d93 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/account/MastodonAccountApiControllerTest.kt @@ -0,0 +1,128 @@ +package dev.usbharu.hideout.mastodon.interfaces.api.account + +import dev.usbharu.hideout.application.config.ActivityPubConfig +import dev.usbharu.hideout.domain.mastodon.model.generated.CredentialAccount +import dev.usbharu.hideout.domain.mastodon.model.generated.CredentialAccountSource +import dev.usbharu.hideout.domain.mastodon.model.generated.Role +import dev.usbharu.hideout.mastodon.service.account.AccountApiService +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.Spy +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.whenever +import org.springframework.http.MediaType +import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.security.oauth2.jwt.Jwt +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken +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 +import utils.TestTransaction + +@ExtendWith(MockitoExtension::class) +class MastodonAccountApiControllerTest { + + private lateinit var mockMvc: MockMvc + + @Spy + private lateinit var testTransaction: TestTransaction + + @Mock + private lateinit var accountApiService: AccountApiService + + @InjectMocks + private lateinit var mastodonAccountApiController: MastodonAccountApiController + + @BeforeEach + fun setUp() { + mockMvc = MockMvcBuilders.standaloneSetup(mastodonAccountApiController).build() + } + + @Test + fun `apiV1AccountsVerifyCredentialsGet JWTで認証時に200が返ってくる`() = runTest { + + val createEmptyContext = SecurityContextHolder.createEmptyContext() + createEmptyContext.authentication = JwtAuthenticationToken( + Jwt.withTokenValue("a").header("alg", "RS236").claim("uid", "1234").build() + ) + SecurityContextHolder.setContext(createEmptyContext) + val credentialAccount = CredentialAccount( + id = "", + username = "", + acct = "", + url = "", + displayName = "", + note = "", + avatar = "", + avatarStatic = "", + header = "", + headerStatic = "", + locked = false, + fields = emptyList(), + emojis = emptyList(), + bot = false, + group = false, + discoverable = true, + createdAt = "", + lastStatusAt = "", + statusesCount = 0, + followersCount = 0, + source = CredentialAccountSource( + note = "", + fields = emptyList(), + privacy = CredentialAccountSource.Privacy.public, + sensitive = false, + followRequestsCount = 0 + ), + noindex = false, + moved = false, + suspendex = false, + limited = false, + followingCount = 0, + role = Role(0, "ADMIN", "", 0, false) + ) + whenever(accountApiService.verifyCredentials(eq(1234))).doReturn(credentialAccount) + + val objectMapper = ActivityPubConfig().objectMapper() + + mockMvc + .get("/api/v1/accounts/verify_credentials") + .asyncDispatch() + .andExpect { status { isOk() } } + .andExpect { content { json(objectMapper.writeValueAsString(credentialAccount)) } } + } + + @Test + fun `apiV1AccountsVerifyCredentialsGet POSTは405が返ってくる`() { + mockMvc.post("/api/v1/accounts/verify_credentials") + .andExpect { status { isMethodNotAllowed() } } + } + + @Test + fun `apiV1AccountsPost GETは405が返ってくる`() { + mockMvc.get("/api/v1/accounts") + .andExpect { status { isMethodNotAllowed() } } + } + + @Test + fun `apiV1AccountsPost アカウント作成成功時302とアカウントのurlが返ってくる`() { + mockMvc + .post("/api/v1/accounts") { + contentType = MediaType.APPLICATION_FORM_URLENCODED + param("username", "hoge") + param("password", "very_secure_password") + param("email", "email@example.com") + param("agreement", "true") + param("locale", "true") + }.asyncDispatch() + .andExpect { header { string("location", "/users/hoge") } } + .andExpect { status { isFound() } } + } +}