SimpleNotes/api/test/services/UserServiceTest.kt

89 lines
2.5 KiB
Kotlin

package services
import be.vandewalleh.migrations.Migration
import be.vandewalleh.services.NotesService
import be.vandewalleh.services.UserService
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import me.liuwj.ktorm.database.*
import org.amshove.kluent.*
import org.junit.jupiter.api.*
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
import org.kodein.di.generic.singleton
import org.testcontainers.containers.MariaDBContainer
import javax.sql.DataSource
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class UserServiceTest {
class KMariadbContainer : MariaDBContainer<KMariadbContainer>()
private val mariadb = KMariadbContainer().apply {
this.start()
}
private val hikariConfig = HikariConfig().apply {
jdbcUrl = mariadb.jdbcUrl
username = mariadb.username
password = mariadb.password
}
private val dataSource = HikariDataSource(hikariConfig)
private val db = Database.Companion.connect(dataSource)
private val kodein = Kodein {
bind<DataSource>() with singleton { dataSource }
bind<Database>() with singleton { db }
bind<Migration>() with singleton { Migration(this.kodein) }
bind<UserService>() with singleton { UserService(this.kodein) }
bind<NotesService>() with singleton { NotesService(this.kodein) }
}
private val migration by kodein.instance<Migration>()
init {
migration.migrate()
}
private val userService by kodein.instance<UserService>()
@Test
@Order(1)
fun `test create user`() {
val username = "hubert"
val email = "a@a"
val password = "password"
println(userService.createUser(username, email, password))
val id = userService.getUserId(email)
id `should not be` null
userService.getUserInfo(id!!)!!.let {
it.username `should be equal to` username
it.email `should be equal to` email
}
}
@Test
@Order(2)
fun `test create same user`() {
userService.createUser(username = "hubert", hashedPassword = "password", email = "a@a") `should be` null
}
@Test
@Order(3)
fun `test delete user`() {
val email = "a@a"
val id = userService.getUserId(email)!!
userService.deleteUser(id)
userService.getUserId(email) `should be` null
userService.getUserInfo(id) `should be` null
}
}