mirror of https://github.com/usbharu/Hideout.git
feat: 投稿のテストを追加
This commit is contained in:
parent
27ae5f039c
commit
5b2d2956bd
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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<JWTPrincipal>() ?: throw RuntimeException("no principal")
|
||||
val username = principal.payload.getClaim("uid").asString()
|
||||
val userId = principal.payload.getClaim("uid").asLong()
|
||||
|
||||
val receive = call.receive<Post>()
|
||||
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) {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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<Claim> {
|
||||
on { asLong() } doReturn 1234
|
||||
}
|
||||
val payload = mock<Payload> {
|
||||
on { getClaim(eq("uid")) } doReturn claim
|
||||
}
|
||||
val postService = mock<IPostService> {
|
||||
onBlocking { create(any<PostCreateDto>()) } doAnswer {
|
||||
val argument = it.getArgument<PostCreateDto>(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<PostCreateDto> {
|
||||
verify(postService).create(capture())
|
||||
assertEquals(PostCreateDto("test", 1234), firstValue)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue