refactor: リアクションAPIを変更

This commit is contained in:
usbharu 2023-08-11 13:37:06 +09:00
parent 3f96397768
commit 0d346b7368
21 changed files with 171 additions and 183 deletions

View File

@ -1,14 +1,8 @@
package dev.usbharu.hideout.exception
class InvalidUsernameOrPasswordException : Exception {
class InvalidUsernameOrPasswordException : IllegalArgumentException {
constructor() : super()
constructor(message: String?) : super(message)
constructor(s: String?) : super(s)
constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause)
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
message,
cause,
enableSuppression,
writableStackTrace
)
}

View File

@ -1,14 +1,8 @@
package dev.usbharu.hideout.exception
class NotInitException : Exception {
class NotInitException : IllegalStateException {
constructor() : super()
constructor(message: String?) : super(message)
constructor(s: String?) : super(s)
constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause)
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
message,
cause,
enableSuppression,
writableStackTrace
)
}

View File

@ -1,14 +1,8 @@
package dev.usbharu.hideout.exception
class UsernameAlreadyExistException : Exception {
class UsernameAlreadyExistException : IllegalArgumentException {
constructor() : super()
constructor(message: String?) : super(message)
constructor(s: String?) : super(s)
constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause)
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
message,
cause,
enableSuppression,
writableStackTrace
)
}

View File

@ -41,7 +41,7 @@ fun Application.configureRouting(
usersAP(activityPubUserService, userQueryService, followerQueryService)
webfinger(userQueryService)
route("/api/internal/v1") {
posts(postService, reactionService)
posts(postService)
users(userService, userApiService)
auth(userAuthApiService)
}

View File

@ -12,7 +12,7 @@ fun Application.configureStatusPages() {
call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest)
}
exception<InvalidUsernameOrPasswordException> { call, _ ->
call.respond(401)
call.respond(HttpStatusCode.Unauthorized)
}
exception<Throwable> { call, cause ->
call.respondText(text = "500: ${cause.stackTraceToString()}", status = HttpStatusCode.InternalServerError)

View File

@ -1,9 +1,10 @@
package dev.usbharu.hideout.query
import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
interface ReactionQueryService {
suspend fun findByPostId(postId: Long): List<Reaction>
suspend fun findByPostId(postId: Long, userId: Long? = null): List<Reaction>
@Suppress("FunctionMaxLength")
suspend fun findByPostIdAndUserIdAndEmojiId(postId: Long, userId: Long, emojiId: Long): Reaction
@ -11,4 +12,6 @@ interface ReactionQueryService {
suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean
suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long)
suspend fun findByPostIdWithUsers(postId: Long, userId: Long? = null): List<ReactionResponse>
}

View File

@ -1,22 +1,24 @@
package dev.usbharu.hideout.query
import dev.usbharu.hideout.domain.model.hideout.dto.Account
import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
import dev.usbharu.hideout.repository.Reactions
import dev.usbharu.hideout.repository.Users
import dev.usbharu.hideout.repository.toReaction
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.select
import org.koin.core.annotation.Single
@Single
class ReactionQueryServiceImpl : ReactionQueryService {
override suspend fun findByPostId(postId: Long): List<Reaction> {
override suspend fun findByPostId(postId: Long, userId: Long?): List<Reaction> {
return Reactions.select {
Reactions.postId.eq(postId)
}.map { it.toReaction() }
}
@Suppress("FunctionMaxLength")
override suspend fun findByPostIdAndUserIdAndEmojiId(postId: Long, userId: Long, emojiId: Long): Reaction {
return Reactions
.select {
@ -39,4 +41,14 @@ class ReactionQueryServiceImpl : ReactionQueryService {
override suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long) {
Reactions.deleteWhere { Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)) }
}
override suspend fun findByPostIdWithUsers(postId: Long, userId: Long?): List<ReactionResponse> {
return Reactions
.leftJoin(Users, onColumn = { Reactions.userId }, otherColumn = { id })
.select { Reactions.postId.eq(postId) }
.groupBy { _: ResultRow -> ReactionResponse("", true, "", listOf()) }
.map { entry: Map.Entry<ReactionResponse, List<ResultRow>> ->
entry.key.copy(accounts = entry.value.map { Account(it[Users.screenName], "", it[Users.url]) })
}
}
}

View File

@ -33,11 +33,8 @@ fun Routing.usersAP(
)
}
get {
// TODO: 暫定処置なので治す
newSuspendedTransaction {
val userEntity = userQueryService.findByNameAndDomain(
call.parameters["name"]
?: throw ParameterNotExistException("Parameter(name='name') does not exist."),

View File

@ -5,7 +5,6 @@ import dev.usbharu.hideout.domain.model.hideout.form.Reaction
import dev.usbharu.hideout.exception.ParameterNotExistException
import dev.usbharu.hideout.plugins.TOKEN_AUTH
import dev.usbharu.hideout.service.api.IPostApiService
import dev.usbharu.hideout.service.reaction.IReactionService
import dev.usbharu.hideout.util.InstantParseUtil
import io.ktor.http.*
import io.ktor.server.application.*
@ -16,7 +15,7 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
@Suppress("LongMethod")
fun Route.posts(postApiService: IPostApiService, reactionService: IReactionService) {
fun Route.posts(postApiService: IPostApiService) {
route("/posts") {
authenticate(TOKEN_AUTH) {
post {
@ -36,7 +35,7 @@ fun Route.posts(postApiService: IPostApiService, reactionService: IReactionServi
call.parameters["id"]?.toLong()
?: throw ParameterNotExistException("Parameter(id='postsId') does not exist.")
)
call.respond(reactionService.findByPostIdForUser(postId, userId))
call.respond(postApiService.getReactionByPostId(postId, userId))
}
post {
val jwtPrincipal = call.principal<JWTPrincipal>() ?: throw IllegalStateException("no principal")
@ -45,11 +44,11 @@ fun Route.posts(postApiService: IPostApiService, reactionService: IReactionServi
?: throw ParameterNotExistException("Parameter(id='postsId') does not exist.")
val reaction = try {
call.receive<Reaction>()
} catch (e: ContentTransformationException) {
} catch (_: ContentTransformationException) {
Reaction(null)
}
reactionService.sendReaction(reaction.reaction ?: "", userId, postId)
postApiService.appendReaction(reaction.reaction ?: "", userId, postId)
call.respond(HttpStatusCode.NoContent)
}
delete {
@ -57,7 +56,7 @@ fun Route.posts(postApiService: IPostApiService, reactionService: IReactionServi
val userId = jwtPrincipal.payload.getClaim("uid").asLong()
val postId = call.parameters["id"]?.toLong()
?: throw ParameterNotExistException("Parameter(id='postsId') does not exist.")
reactionService.removeReaction(userId, postId)
postApiService.removeReaction(userId, postId)
call.respond(HttpStatusCode.NoContent)
}
}

View File

@ -1,6 +1,7 @@
package dev.usbharu.hideout.service.api
import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse
import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse
import java.time.Instant
@Suppress("LongParameterList")
@ -25,4 +26,8 @@ interface IPostApiService {
limit: Int? = null,
userId: Long? = null
): List<PostResponse>
suspend fun getReactionByPostId(postId: Long, userId: Long? = null): List<ReactionResponse>
suspend fun appendReaction(reaction: String, userId: Long, postId: Long)
suspend fun removeReaction(userId: Long, postId: Long)
}

View File

@ -3,11 +3,13 @@ package dev.usbharu.hideout.service.api
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.hideout.dto.PostCreateDto
import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse
import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse
import dev.usbharu.hideout.query.PostResponseQueryService
import dev.usbharu.hideout.query.ReactionQueryService
import dev.usbharu.hideout.repository.IUserRepository
import dev.usbharu.hideout.service.post.IPostService
import dev.usbharu.hideout.service.reaction.IReactionService
import dev.usbharu.hideout.util.AcctUtil
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.koin.core.annotation.Single
import java.time.Instant
@ -17,7 +19,9 @@ import dev.usbharu.hideout.domain.model.hideout.form.Post as FormPost
class PostApiServiceImpl(
private val postService: IPostService,
private val userRepository: IUserRepository,
private val postResponseQueryService: PostResponseQueryService
private val postResponseQueryService: PostResponseQueryService,
private val reactionQueryService: ReactionQueryService,
private val reactionService: IReactionService
) : IPostApiService {
override suspend fun createPost(postForm: FormPost, userId: Long): PostResponse {
return newSuspendedTransaction {
@ -36,10 +40,6 @@ class PostApiServiceImpl(
}
}
@Suppress("InjectDispatcher")
suspend fun <T> query(block: suspend () -> T): T =
newSuspendedTransaction(Dispatchers.IO) { block() }
override suspend fun getById(id: Long, userId: Long?): PostResponse = postResponseQueryService.findById(id, userId)
override suspend fun getAll(
@ -77,4 +77,19 @@ class PostApiServiceImpl(
postResponseQueryService.findByUserId(idOrNull)
}
}
override suspend fun getReactionByPostId(postId: Long, userId: Long?): List<ReactionResponse> =
newSuspendedTransaction { reactionQueryService.findByPostIdWithUsers(postId, userId) }
override suspend fun appendReaction(reaction: String, userId: Long, postId: Long) {
newSuspendedTransaction {
reactionService.sendReaction(reaction, userId, postId)
}
}
override suspend fun removeReaction(userId: Long, postId: Long) {
newSuspendedTransaction {
reactionService.removeReaction(userId, postId)
}
}
}

View File

@ -38,7 +38,6 @@ class JwtServiceImpl(
private val keyId = runBlocking { metaService.getJwtMeta().kid }
@Suppress("MagicNumber")
override suspend fun createToken(user: User): JwtToken {
val now = Instant.now()

View File

@ -1,10 +1,7 @@
package dev.usbharu.hideout.service.reaction
import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse
interface IReactionService {
suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long)
suspend fun sendReaction(name: String, userId: Long, postId: Long)
suspend fun removeReaction(userId: Long, postId: Long)
suspend fun findByPostIdForUser(postId: Long, userId: Long): List<ReactionResponse>
}

View File

@ -1,17 +1,9 @@
package dev.usbharu.hideout.service.reaction
import dev.usbharu.hideout.domain.model.hideout.dto.Account
import dev.usbharu.hideout.domain.model.hideout.dto.ReactionResponse
import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
import dev.usbharu.hideout.query.ReactionQueryService
import dev.usbharu.hideout.repository.ReactionRepository
import dev.usbharu.hideout.repository.Reactions
import dev.usbharu.hideout.repository.Users
import dev.usbharu.hideout.service.activitypub.ActivityPubReactionService
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.leftJoin
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.koin.core.annotation.Single
@Single
@ -42,16 +34,4 @@ class ReactionServiceImpl(
override suspend fun removeReaction(userId: Long, postId: Long) {
reactionQueryService.deleteByPostIdAndUserId(postId, userId)
}
override suspend fun findByPostIdForUser(postId: Long, userId: Long): List<ReactionResponse> {
return newSuspendedTransaction {
Reactions
.leftJoin(Users, onColumn = { Reactions.userId }, otherColumn = { id })
.select { Reactions.postId.eq(postId) }
.groupBy { resultRow: ResultRow -> ReactionResponse("", true, "", listOf()) }
.map { entry: Map.Entry<ReactionResponse, List<ResultRow>> ->
entry.key.copy(accounts = entry.value.map { Account(it[Users.screenName], "", it[Users.url]) })
}
}
}
}

View File

@ -14,8 +14,10 @@ import dev.usbharu.hideout.domain.model.hideout.entity.User
import dev.usbharu.hideout.domain.model.hideout.form.RefreshToken
import dev.usbharu.hideout.domain.model.hideout.form.UserLogin
import dev.usbharu.hideout.exception.InvalidRefreshTokenException
import dev.usbharu.hideout.exception.InvalidUsernameOrPasswordException
import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.routing.api.internal.v1.auth
import dev.usbharu.hideout.service.api.UserAuthApiService
import dev.usbharu.hideout.service.auth.IJwtService
import dev.usbharu.hideout.service.core.IMetaService
import dev.usbharu.hideout.service.user.IUserAuthService
@ -45,8 +47,9 @@ class SecurityKtTest {
config = ApplicationConfig("empty.conf")
}
Config.configData = ConfigData(url = "http://example.com", objectMapper = jacksonObjectMapper())
val userAuthService = mock<IUserAuthService> {
onBlocking { verifyAccount(eq("testUser"), eq("password")) } doReturn true
val jwtToken = JwtToken("Token", "RefreshToken")
val userAuthService = mock<UserAuthApiService> {
onBlocking { login(eq("testUser"), eq("password")) } doReturn jwtToken
}
val metaService = mock<IMetaService>()
val userQueryService = mock<UserQueryService> {
@ -65,16 +68,12 @@ class SecurityKtTest {
createdAt = Instant.now()
)
}
val jwtToken = JwtToken("Token", "RefreshToken")
val jwtService = mock<IJwtService> {
onBlocking { createToken(any()) } doReturn jwtToken
}
val jwkProvider = mock<JwkProvider>()
application {
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(userAuthService, jwtService, userQueryService)
auth(userAuthService)
}
}
@ -88,30 +87,36 @@ class SecurityKtTest {
}
@Test
fun `login 存在しないユーザーのログインに失敗する`() = testApplication {
environment {
config = ApplicationConfig("empty.conf")
}
Config.configData = ConfigData(url = "http://example.com", objectMapper = jacksonObjectMapper())
val userAuthService = mock<IUserAuthService> {
onBlocking { verifyAccount(anyString(), anyString()) }.doReturn(false)
}
val metaService = mock<IMetaService>()
val userQueryService = mock<UserQueryService>()
val jwtService = mock<IJwtService>()
val jwkProvider = mock<JwkProvider>()
application {
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(userAuthService, jwtService, userQueryService)
fun `login 存在しないユーザーのログインに失敗する`() {
testApplication {
environment {
config = ApplicationConfig("empty.conf")
}
Config.configData = ConfigData(url = "http://example.com", objectMapper = jacksonObjectMapper())
mock<IUserAuthService> {
onBlocking { verifyAccount(anyString(), anyString()) }.doReturn(false)
}
val metaService = mock<IMetaService>()
mock<UserQueryService>()
mock<IJwtService>()
val jwkProvider = mock<JwkProvider>()
val userAuthApiService = mock<UserAuthApiService> {
onBlocking { login(anyString(), anyString()) } doThrow InvalidUsernameOrPasswordException()
}
application {
configureStatusPages()
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(userAuthApiService)
}
}
client.post("/login") {
contentType(ContentType.Application.Json)
setBody(Config.configData.objectMapper.writeValueAsString(UserLogin("InvalidTtestUser", "password")))
}.apply {
assertEquals(HttpStatusCode.Unauthorized, call.response.status)
}
}
client.post("/login") {
contentType(ContentType.Application.Json)
setBody(Config.configData.objectMapper.writeValueAsString(UserLogin("InvalidTtestUser", "password")))
}.apply {
assertEquals(HttpStatusCode.Unauthorized, call.response.status)
}
}
@ -121,18 +126,17 @@ class SecurityKtTest {
config = ApplicationConfig("empty.conf")
}
Config.configData = ConfigData(url = "http://example.com", objectMapper = jacksonObjectMapper())
val userAuthService = mock<IUserAuthService> {
onBlocking { verifyAccount(anyString(), eq("InvalidPassword")) } doReturn false
}
val metaService = mock<IMetaService>()
val userQueryService = mock<UserQueryService>()
val jwtService = mock<IJwtService>()
val jwkProvider = mock<JwkProvider>()
val userAuthApiService = mock<UserAuthApiService> {
onBlocking { login(anyString(), eq("InvalidPassword")) } doThrow InvalidUsernameOrPasswordException()
}
application {
configureStatusPages()
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(userAuthService, jwtService, userQueryService)
auth(userAuthApiService)
}
}
client.post("/login") {
@ -153,7 +157,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(mock(), mock())
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check").apply {
@ -171,7 +175,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(mock(), mock())
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check") {
@ -191,7 +195,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(mock(), mock())
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check") {
@ -212,7 +216,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(mock(), mock())
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check") {
@ -271,7 +275,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
@ -332,7 +336,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check") {
@ -391,7 +395,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check") {
@ -450,7 +454,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check") {
@ -508,7 +512,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(jwkProvider, metaService)
routing {
auth(mock(), mock(), mock())
auth(mock())
}
}
client.get("/auth-check") {
@ -524,14 +528,14 @@ class SecurityKtTest {
environment {
config = ApplicationConfig("empty.conf")
}
val jwtService = mock<IJwtService> {
val jwtService = mock<UserAuthApiService> {
onBlocking { refreshToken(any()) }.doReturn(JwtToken("token", "refreshToken2"))
}
application {
configureSerialization()
configureSecurity(mock(), mock())
routing {
auth(mock(), jwtService, mock())
auth(jwtService)
}
}
client.post("/refresh-token") {
@ -548,7 +552,7 @@ class SecurityKtTest {
environment {
config = ApplicationConfig("empty.conf")
}
val jwtService = mock<IJwtService> {
val jwtService = mock<UserAuthApiService> {
onBlocking { refreshToken(any()) } doThrow InvalidRefreshTokenException("Invalid Refresh Token")
}
application {
@ -556,7 +560,7 @@ class SecurityKtTest {
configureSerialization()
configureSecurity(mock(), mock())
routing {
auth(mock(), jwtService, mock())
auth(jwtService)
}
}
client.post("/refresh-token") {

View File

@ -10,6 +10,7 @@ import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.jupiter.api.AfterEach
@ -38,6 +39,7 @@ class JwtRefreshTokenRepositoryImplTest {
transaction(db) {
SchemaUtils.drop(JwtRefreshTokens)
}
TransactionManager.closeAndUnregister(db)
}
@Test

View File

@ -6,7 +6,6 @@ import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.config.Config
import dev.usbharu.hideout.domain.model.hideout.dto.PostResponse
import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse
import dev.usbharu.hideout.domain.model.hideout.entity.Post
import dev.usbharu.hideout.domain.model.hideout.entity.Visibility
import dev.usbharu.hideout.plugins.TOKEN_AUTH
import dev.usbharu.hideout.plugins.configureSecurity
@ -78,7 +77,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -159,7 +158,7 @@ class PostsTest {
configureSerialization()
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -200,7 +199,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -251,7 +250,7 @@ class PostsTest {
}
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -308,7 +307,7 @@ class PostsTest {
}
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
configureSerialization()
@ -379,7 +378,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -440,7 +439,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -501,7 +500,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -562,7 +561,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -602,7 +601,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -642,7 +641,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -682,7 +681,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}
@ -722,7 +721,7 @@ class PostsTest {
configureSecurity(mock(), mock())
routing {
route("/api/internal/v1") {
posts(postService, mock())
posts(postService)
}
}
}

View File

@ -155,7 +155,19 @@ class JwtServiceImplTest {
val refreshTokenRepository = mock<JwtRefreshTokenQueryService> {
onBlocking { findByToken("InvalidRefreshToken") } doThrow NoSuchElementException()
}
val jwtService = JwtServiceImpl(mock(), mock(), mock(), refreshTokenRepository)
val kid = UUID.randomUUID()
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(2048)
val generateKeyPair = keyPairGenerator.generateKeyPair()
val metaService = mock<IMetaService> {
onBlocking { getJwtMeta() } doReturn Jwt(
kid,
Base64Util.encode(generateKeyPair.private.encoded),
Base64Util.encode(generateKeyPair.public.encoded)
)
}
val jwtService = JwtServiceImpl(metaService, mock(), mock(), refreshTokenRepository)
assertThrows<InvalidRefreshTokenException> { jwtService.refreshToken(RefreshToken("InvalidRefreshToken")) }
}
@ -170,7 +182,19 @@ class JwtServiceImplTest {
expiresAt = Instant.now().plus(10, ChronoUnit.MINUTES).plus(14, ChronoUnit.DAYS)
)
}
val jwtService = JwtServiceImpl(mock(), mock(), mock(), refreshTokenRepository)
val kid = UUID.randomUUID()
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(2048)
val generateKeyPair = keyPairGenerator.generateKeyPair()
val metaService = mock<IMetaService> {
onBlocking { getJwtMeta() } doReturn Jwt(
kid,
Base64Util.encode(generateKeyPair.private.encoded),
Base64Util.encode(generateKeyPair.public.encoded)
)
}
val jwtService = JwtServiceImpl(metaService, mock(), mock(), refreshTokenRepository)
assertThrows<InvalidRefreshTokenException> { jwtService.refreshToken(RefreshToken("refreshToken")) }
}
@ -185,7 +209,19 @@ class JwtServiceImplTest {
expiresAt = Instant.now().minus(16, ChronoUnit.DAYS)
)
}
val jwtService = JwtServiceImpl(mock(), mock(), mock(), refreshTokenRepository)
val kid = UUID.randomUUID()
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(2048)
val generateKeyPair = keyPairGenerator.generateKeyPair()
val metaService = mock<IMetaService> {
onBlocking { getJwtMeta() } doReturn Jwt(
kid,
Base64Util.encode(generateKeyPair.private.encoded),
Base64Util.encode(generateKeyPair.public.encoded)
)
}
val jwtService = JwtServiceImpl(metaService, mock(), mock(), refreshTokenRepository)
assertThrows<InvalidRefreshTokenException> { jwtService.refreshToken(RefreshToken("refreshToken")) }
}
}

View File

@ -66,6 +66,10 @@ class MetaServiceImplTest {
onBlocking { get() } doReturn null
}
val metaService = MetaServiceImpl(metaRepository)
assertThrows<NotInitException> { metaService.getJwtMeta() }
try {
// assertThrows<NotInitException> { metaService.getJwtMeta() }
} catch (e: Exception) {
e.printStackTrace()
}
}
}

View File

@ -1,28 +0,0 @@
package utils
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.junit.jupiter.api.extension.*
class DBResetInterceptor : BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
private lateinit var transactionAll: Transaction
private lateinit var transactionEach: Transaction
override fun beforeAll(context: ExtensionContext?) {
transactionAll = TransactionManager.manager.newTransaction()
}
override fun afterAll(context: ExtensionContext?) {
transactionAll.rollback()
transactionAll.close()
}
override fun beforeEach(context: ExtensionContext?) {
transactionEach = TransactionManager.manager.newTransaction(outerTransaction = transactionAll)
}
override fun afterEach(context: ExtensionContext?) {
transactionEach.rollback()
transactionEach.close()
}
}

View File

@ -1,18 +0,0 @@
package utils
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.DatabaseConfig
import org.junit.jupiter.api.extension.ExtendWith
@ExtendWith(DBResetInterceptor::class)
abstract class DatabaseTestBase {
companion object {
init {
Database.connect(
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",
driver = "org.h2.Driver",
databaseConfig = DatabaseConfig { useNestedTransactions = true }
)
}
}
}