From c4f8101729d16b804ee06b80c4e8384f1f2236d4 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 8 May 2023 16:07:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8A=95=E7=A8=BF=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 --- .../domain/model/hideout/dto/PostCreateDto.kt | 2 +- .../hideout/domain/model/hideout/form/Post.kt | 10 ++- .../hideout/routing/api/internal/v1/Posts.kt | 10 ++- .../usbharu/hideout/service/IPostService.kt | 4 +- .../hideout/service/impl/PostService.kt | 8 +- .../routing/api/internal/v1/PostsKtTest.kt | 84 +++++++++++++++++++ 6 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsKtTest.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/PostCreateDto.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/PostCreateDto.kt index d863489a..9d699fb7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/PostCreateDto.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/PostCreateDto.kt @@ -1,3 +1,3 @@ package dev.usbharu.hideout.domain.model.hideout.dto -data class PostCreateDto(val text: String, val username: String) +data class PostCreateDto(val text: String, val userId: Long) diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/Post.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/Post.kt index 7e654ab2..bc768d32 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/Post.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/Post.kt @@ -1,3 +1,11 @@ package dev.usbharu.hideout.domain.model.hideout.form -data class Post(val text: String) +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility + +data class Post( + val text: String, + val overview: String? = null, + val visibility: Visibility = Visibility.PUBLIC, + val repostId: Long? = null, + val replyId: Long? = null +) diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt index ab5fb85b..7477cf6c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/api/internal/v1/Posts.kt @@ -5,10 +5,12 @@ import dev.usbharu.hideout.domain.model.hideout.form.Post import dev.usbharu.hideout.plugins.TOKEN_AUTH import dev.usbharu.hideout.service.IPostService import dev.usbharu.hideout.util.InstantParseUtil +import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* import io.ktor.server.auth.jwt.* import io.ktor.server.request.* +import io.ktor.server.response.* import io.ktor.server.routing.* fun Route.posts(postService: IPostService) { @@ -16,11 +18,13 @@ fun Route.posts(postService: IPostService) { authenticate(TOKEN_AUTH) { post { val principal = call.principal() ?: throw RuntimeException("no principal") - val username = principal.payload.getClaim("uid").asString() + val userId = principal.payload.getClaim("uid").asLong() val receive = call.receive() - val postCreateDto = PostCreateDto(receive.text, username) - postService.create(postCreateDto) + val postCreateDto = PostCreateDto(receive.text, userId) + val create = postService.create(postCreateDto) + call.response.header("Location", create.url) + call.respond(HttpStatusCode.OK) } } authenticate(TOKEN_AUTH, optional = true) { diff --git a/src/main/kotlin/dev/usbharu/hideout/service/IPostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/IPostService.kt index bf64ac10..0b4b4d59 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/IPostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/IPostService.kt @@ -5,8 +5,8 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Post import java.time.Instant interface IPostService { - suspend fun create(post: Post) - suspend fun create(post: PostCreateDto) + suspend fun create(post: Post): Post + suspend fun create(post: PostCreateDto): Post suspend fun findAll( since: Instant? = null, until: Instant? = null, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt index d8ec4f5f..137075b1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/PostService.kt @@ -25,21 +25,23 @@ class PostService( private val logger = LoggerFactory.getLogger(this::class.java) - override suspend fun create(post: Post) { + override suspend fun create(post: Post): Post { logger.debug("create post={}", post) val postEntity = postRepository.save(post) activityPubNoteService.createNote(postEntity) + return post } - override suspend fun create(post: PostCreateDto) { + override suspend fun create(post: PostCreateDto): Post { logger.debug("create post={}", post) - val user = userService.findByNameLocalUser(post.username) + val user = userService.findById(post.userId) val id = postRepository.generateId() val postEntity = Post( id, user.id, null, post.text, Instant.now().toEpochMilli(), Visibility.PUBLIC, "${user.url}/posts/$id", null, null ) postRepository.save(postEntity) + return postEntity } override suspend fun findAll( diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsKtTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsKtTest.kt new file mode 100644 index 00000000..de9e768a --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/PostsKtTest.kt @@ -0,0 +1,84 @@ +package dev.usbharu.hideout.routing.api.internal.v1 + +import com.auth0.jwt.interfaces.Claim +import com.auth0.jwt.interfaces.Payload +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto +import dev.usbharu.hideout.domain.model.hideout.entity.Visibility +import dev.usbharu.hideout.domain.model.hideout.form.Post +import dev.usbharu.hideout.plugins.TOKEN_AUTH +import dev.usbharu.hideout.plugins.configureSerialization +import dev.usbharu.hideout.service.IPostService +import io.ktor.client.request.* +import io.ktor.http.* +import io.ktor.server.auth.* +import io.ktor.server.auth.jwt.* +import io.ktor.server.config.* +import io.ktor.server.routing.* +import io.ktor.server.testing.* +import org.junit.jupiter.api.Test +import org.mockito.kotlin.* +import java.time.Instant +import kotlin.test.assertEquals + +class PostsKtTest { + + + @Test + fun `posts-post postsにpostしたら投稿できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + val claim = mock { + on { asLong() } doReturn 1234 + } + val payload = mock { + on { getClaim(eq("uid")) } doReturn claim + } + val postService = mock { + onBlocking { create(any()) } doAnswer { + val argument = it.getArgument(0) + dev.usbharu.hideout.domain.model.hideout.entity.Post( + 123L, + argument.userId, + null, + argument.text, + Instant.now().toEpochMilli(), + Visibility.PUBLIC, + "https://example.com" + ) + } + } + application { + authentication { + + bearer(TOKEN_AUTH) { + authenticate { + println("aaaaaaaaaaaa") + JWTPrincipal(payload) + } + } + } + routing { + route("/api/internal/v1") { + posts(postService) + } + } + configureSerialization() + } + + val post = Post("test") + client.post("/api/internal/v1/posts") { + header("Authorization", "Bearer asdkaf") + contentType(ContentType.Application.Json) + setBody(Config.configData.objectMapper.writeValueAsString(post)) + }.apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals("https://example.com", headers["Location"]) + } + argumentCaptor { + verify(postService).create(capture()) + assertEquals(PostCreateDto("test", 1234), firstValue) + } + } +}