From 7d15a6892fa929b99bedcf3b6b16063d8980cc3d Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 5 May 2023 13:19:35 +0900 Subject: [PATCH] =?UTF-8?q?test:=20JwtRefreshTokenRepository=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 --- .../JwtRefreshTokenRepositoryImplTest.kt | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImplTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImplTest.kt new file mode 100644 index 00000000..396ecc08 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImplTest.kt @@ -0,0 +1,89 @@ +@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalCoroutinesApi::class) + +package dev.usbharu.hideout.repository + +import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken +import dev.usbharu.hideout.service.IdGenerateService +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +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.transaction +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import java.time.Clock +import java.time.Instant +import java.time.ZoneId +import java.time.temporal.ChronoUnit +import kotlin.test.assertEquals + +class JwtRefreshTokenRepositoryImplTest { + + lateinit var db: Database + + @BeforeEach + fun setUp() { + db = Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") + transaction(db) { + SchemaUtils.create(JwtRefreshTokens) + } + } + + @AfterEach + fun tearDown() { + transaction(db) { + SchemaUtils.drop(JwtRefreshTokens) + } + } + + @Test + fun `save 存在しない場合はinsertする`() = runTest { + val repository = JwtRefreshTokenRepositoryImpl(db, object : IdGenerateService { + override suspend fun generateId(): Long { + TODO("Not yet implemented") + } + }) + val now = Instant.now(Clock.tickMillis(ZoneId.systemDefault())) + val expiresAt = now.plus(10, ChronoUnit.MINUTES) + + val expect = JwtRefreshToken(1L, 2L, "refreshToken", now, expiresAt) + repository.save(expect) + val actual = repository.findById(1L) + assertEquals(expect, actual) + } + + @Test + fun `save 存在する場合はupdateする`() = runTest { + val repository = JwtRefreshTokenRepositoryImpl(db, object : IdGenerateService { + override suspend fun generateId(): Long { + TODO("Not yet implemented") + } + }) + transaction { + JwtRefreshTokens.insert { + it[id] = 1L + it[userId] = 2L + it[refreshToken] = "refreshToken1" + it[createdAt] = Instant.now().toEpochMilli() + it[expiresAt] = Instant.now().plus(10, ChronoUnit.MINUTES).toEpochMilli() + } + } + + repository.save( + JwtRefreshToken( + id = 1L, + userId = 2L, + refreshToken = "refreshToken2", + createdAt = Instant.now(), + expiresAt = Instant.now().plus(10, ChronoUnit.MINUTES) + ) + ) + transaction { + val toJwtRefreshToken = JwtRefreshTokens.select { JwtRefreshTokens.id.eq(1L) }.single().toJwtRefreshToken() + assertEquals("refreshToken2", toJwtRefreshToken.refreshToken) + } + } +}