From fcb3d39881b0bab1885b0a3191f4ea1ff8a5b4f4 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 29 Nov 2023 12:49:18 +0900 Subject: [PATCH] =?UTF-8?q?test:=20apps=20api=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 --- build.gradle.kts | 4 +- gradle.properties | 2 +- src/intTest/kotlin/mastodon/apps/AppTest.kt | 88 +++++++++++++++++++++ src/intTest/resources/application.yml | 10 +-- 4 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 src/intTest/kotlin/mastodon/apps/AppTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index b5f5cb83..a17c93a4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,7 +13,7 @@ plugins { kotlin("jvm") version "1.8.21" id("org.graalvm.buildtools.native") version "0.9.21" id("io.gitlab.arturbosch.detekt") version "1.23.1" - id("org.springframework.boot") version "3.1.3" + id("org.springframework.boot") version "3.2.0" kotlin("plugin.spring") version "1.8.21" id("org.openapi.generator") version "7.0.1" id("org.jetbrains.kotlinx.kover") version "0.7.4" @@ -206,6 +206,8 @@ dependencies { intTestImplementation("org.springframework.boot:spring-boot-starter-test") intTestImplementation("org.springframework.security:spring-security-test") + intTestImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") + intTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4") } diff --git a/gradle.properties b/gradle.properties index a7ffe13f..5516464a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ ktor_version=2.3.0 -kotlin_version=1.8.21 +kotlin_version=1.9.21 logback_version=1.4.6 kotlin.code.style=official exposed_version=0.44.0 diff --git a/src/intTest/kotlin/mastodon/apps/AppTest.kt b/src/intTest/kotlin/mastodon/apps/AppTest.kt new file mode 100644 index 00000000..f835d3be --- /dev/null +++ b/src/intTest/kotlin/mastodon/apps/AppTest.kt @@ -0,0 +1,88 @@ +package mastodon.apps + +import dev.usbharu.hideout.SpringApplication +import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.RegisteredClient +import org.assertj.core.api.Assertions.assertThat +import org.jetbrains.exposed.sql.select +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.http.MediaType +import org.springframework.security.test.context.support.WithAnonymousUser +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.post +import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder +import org.springframework.test.web.servlet.setup.MockMvcBuilders +import org.springframework.transaction.annotation.Transactional +import org.springframework.web.context.WebApplicationContext + +@SpringBootTest(classes = [SpringApplication::class]) +@AutoConfigureMockMvc +@Transactional +class AppTest { + + @Autowired + private lateinit var context: WebApplicationContext + + private lateinit var mockMvc: MockMvc + + + @BeforeEach + fun setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(context) + .apply(SecurityMockMvcConfigurers.springSecurity()) + .build() + } + + @Test + @WithAnonymousUser + fun apiV1AppsPostにformで匿名でappを作成できる() { + mockMvc + .post("/api/v1/apps") { + contentType = MediaType.APPLICATION_FORM_URLENCODED + param("client_name", "test-client") + param("redirect_uris", "https://example.com") + param("scopes", "write read") + param("website", "https://example.com") + } + .asyncDispatch() + .andExpect { status { isOk() } } + + + val app = RegisteredClient + .select { RegisteredClient.clientName eq "test-client" } + .single() + + assertThat(app[RegisteredClient.clientName]).isEqualTo("test-client") + assertThat(app[RegisteredClient.redirectUris]).isEqualTo("https://example.com") + assertThat(app[RegisteredClient.scopes]).isEqualTo("read,write") + } + + @Test + @WithAnonymousUser + fun apiV1AppsPostにjsonで匿名でappを作成できる() { + mockMvc + .post("/api/v1/apps") { + contentType = MediaType.APPLICATION_JSON + content = """{ + "client_name": "test-client-2", + "redirect_uris": "https://example.com", + "scopes": "write read", + "website": "https;//example.com" +}""" + } + .asyncDispatch() + .andExpect { status { isOk() } } + + val app = RegisteredClient + .select { RegisteredClient.clientName eq "test-client-2" } + .single() + + assertThat(app[RegisteredClient.clientName]).isEqualTo("test-client-2") + assertThat(app[RegisteredClient.redirectUris]).isEqualTo("https://example.com") + assertThat(app[RegisteredClient.scopes]).isEqualTo("read,write") + } +} diff --git a/src/intTest/resources/application.yml b/src/intTest/resources/application.yml index 258014ca..3de298a1 100644 --- a/src/intTest/resources/application.yml +++ b/src/intTest/resources/application.yml @@ -18,10 +18,10 @@ hideout: spring: flyway: - enabled: false + enabled: true datasource: driver-class-name: org.h2.Driver - url: "jdbc:h2:mem:test;MODE=POSTGRESQL;DB_CLOSE_DELAY=-1" + url: "jdbc:h2:mem:test;MODE=POSTGRESQL;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=true;TRACE_LEVEL_FILE=4;" username: "" password: data: @@ -34,8 +34,8 @@ spring: console: enabled: true - exposed: - generate-ddl: true - excluded-packages: dev.usbharu.hideout.core.infrastructure.kjobexposed +# exposed: +# generate-ddl: true +# excluded-packages: dev.usbharu.hideout.core.infrastructure.kjobexposed server: port: 8080