Refactor: Move bcrypt inside kodein module for easier testing
This commit is contained in:
@@ -3,6 +3,7 @@ package integration.routing
|
||||
import be.vandewalleh.auth.SimpleJWT
|
||||
import be.vandewalleh.entities.User
|
||||
import be.vandewalleh.features.Config
|
||||
import be.vandewalleh.features.PasswordHash
|
||||
import be.vandewalleh.mainModule
|
||||
import be.vandewalleh.module
|
||||
import be.vandewalleh.services.UserService
|
||||
@@ -19,7 +20,6 @@ import org.junit.jupiter.api.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.bind
|
||||
import org.kodein.di.generic.instance
|
||||
import org.mindrot.jbcrypt.BCrypt
|
||||
import utils.*
|
||||
import java.util.*
|
||||
|
||||
@@ -28,10 +28,18 @@ class AuthControllerKtTest {
|
||||
|
||||
private val userService = mockk<UserService>()
|
||||
|
||||
private val kodein = Kodein {
|
||||
import(mainModule, allowOverride = true)
|
||||
bind<UserService>(overrides = true) with instance(userService)
|
||||
}
|
||||
|
||||
private val passwordHash by kodein.instance<PasswordHash>()
|
||||
|
||||
|
||||
init {
|
||||
|
||||
val user = User {
|
||||
password = BCrypt.hashpw("password", BCrypt.gensalt())
|
||||
password = passwordHash.crypt("password")
|
||||
username = "existing"
|
||||
}
|
||||
user["id"] = 1
|
||||
@@ -43,7 +51,7 @@ class AuthControllerKtTest {
|
||||
}
|
||||
|
||||
val user2 = User {
|
||||
password = BCrypt.hashpw("right password", BCrypt.gensalt())
|
||||
password = passwordHash.crypt("right password")
|
||||
username = "wrong"
|
||||
}
|
||||
user["id"] = 2
|
||||
@@ -56,11 +64,6 @@ class AuthControllerKtTest {
|
||||
}
|
||||
|
||||
|
||||
private val kodein = Kodein {
|
||||
import(mainModule, allowOverride = true)
|
||||
bind<UserService>(overrides = true) with instance(userService)
|
||||
}
|
||||
|
||||
private val testEngine = TestApplicationEngine().apply {
|
||||
start()
|
||||
application.module(kodein)
|
||||
|
||||
@@ -52,7 +52,7 @@ class UserServiceTest {
|
||||
@Order(2)
|
||||
fun `test create same user`() {
|
||||
runBlocking {
|
||||
userService.create(username = "hubert", hashedPassword = "password") `should be` null
|
||||
userService.create(username = "hubert", password = "password") `should be` null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user