refactor: password and test api string

This commit is contained in:
SOR
2026-10-09 20:25:55 +08:00
parent 128a487afb
commit 03ee40f6bd
3 changed files with 53 additions and 6 deletions
@@ -53,12 +53,12 @@ fun Application.module() {
// 4. Initialize JWT
val jwtKey = environment.config.tryGetString("jwt.key")
?: "2837452984690238475628394756203847562038475"
?: "11111111111111111111111"
val jwtExpireHours = environment.config.tryGetString("jwt.expire-hours")?.toIntOrNull() ?: 168
val jwtUtils = JwtUtils(jwtKey, jwtExpireHours, redisService)
// 5. Initialize Mail
val mailHost = environment.config.tryGetString("mail.host") ?: "smtp.qq.com"
val mailHost = environment.config.tryGetString("mail.host") ?: "smtp.xxx.com"
val mailPort = environment.config.tryGetString("mail.port")?.toIntOrNull() ?: 465
val mailUser = environment.config.tryGetString("mail.username") ?: ""
val mailPass = environment.config.tryGetString("mail.password") ?: ""
@@ -8,7 +8,7 @@ import io.ktor.server.routing.*
fun Route.testRoutes(wsTokenService: WsTokenService) {
route("/api/test") {
get("/hello") {
call.respondText("Hello Kotlin WebFlux!")
call.respondText("Hello Kotlin Ktor!")
}
get("/wstoken") {
+50 -3
View File
@@ -1,23 +1,70 @@
package io.github.sor2171
package io.github.sor2171.backend
import io.github.sor2171.backend.database.AccountRepository
import io.github.sor2171.backend.database.DatabaseFactory
import io.github.sor2171.backend.entity.dto.Account
import io.github.sor2171.backend.utils.DateUtils
import io.github.sor2171.backend.utils.PasswordEncoder
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.config.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlinx.coroutines.runBlocking
import kotlin.test.*
class ServerTest {
@Test
fun `test default user and password hash`() = runBlocking {
val rawPassword = "123456"
val encodedPassword = PasswordEncoder.encode(rawPassword)
println("==================================================================")
println("Default password '$rawPassword' BCrypt hash: $encodedPassword")
println("==================================================================")
try {
val config = ApplicationConfig("application.yaml")
DatabaseFactory.init(config)
val repository = AccountRepository()
val existingAccount = repository.findByName("test")
if (existingAccount == null) {
val newAccount = Account(
id = null,
username = "test",
password = encodedPassword,
email = "1234567890@gmail.com",
role = "user",
registerTime = DateUtils.getCurrentDateTime()
)
val newId = repository.save(newAccount)
println("Created default test user 'test' in database (ID: $newId).")
} else {
repository.resetPasswordByEmail(existingAccount.email, encodedPassword)
println("Existing test user 'test' found (ID: ${existingAccount.id}), updated password hash.")
}
val verifiedAccount = repository.findByName("test")
assertNotNull(verifiedAccount)
assertTrue(PasswordEncoder.matches(rawPassword, verifiedAccount.password))
println("Verification passed: user 'test' password successfully matched with BCrypt.")
} catch (e: Exception) {
println("Database check skipped or failed (check if MySQL is running): ${e.message}")
}
}
@Test
fun `test basic test route`() = testApplication {
routing {
get("/api/test/hello") {
call.respondText("Hello Kotlin WebFlux!")
call.respondText("Hello Kotlin Ktor!")
}
}
val response = client.get("/api/test/hello")
assertEquals(HttpStatusCode.OK, response.status)
assertEquals("Hello Kotlin WebFlux!", response.bodyAsText())
assertEquals("Hello Kotlin Ktor!", response.bodyAsText())
}
}