User service is now non blocking

This commit is contained in:
2020-06-15 20:57:09 +02:00
parent ebd897093c
commit 6688b35a9b
5 changed files with 92 additions and 70 deletions
@@ -10,7 +10,8 @@ import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import io.ktor.http.*
import io.ktor.server.testing.*
import io.mockk.every
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import org.amshove.kluent.*
@@ -36,9 +37,9 @@ class AuthControllerKtTest {
}
user["id"] = 1
every { userService.getFromUsername("existing") } returns user
every { userService.userExists(1) } returns true
every { userService.getUserInfo(1) } returns User {
coEvery { userService.getFromUsername("existing") } returns user
coEvery { userService.userExists(1) } returns true
coEvery { userService.getUserInfo(1) } returns User {
username = "existing"
email = "existing@mail.com"
}
@@ -48,12 +49,12 @@ class AuthControllerKtTest {
username = "wrong"
}
user["id"] = 2
every { userService.getFromUsername("wrong") } returns user2
coEvery { userService.getFromUsername("wrong") } returns user2
every { userService.getFromUsername("notExisting") } returns null
coEvery { userService.getFromUsername("notExisting") } returns null
every { userService.userExists(3) } returns false
every { userService.getUserInfo(3) } returns null
coEvery { userService.userExists(3) } returns false
coEvery { userService.getUserInfo(3) } returns null
}
@@ -78,7 +79,7 @@ class AuthControllerKtTest {
}
}
verify { userService.getFromUsername("existing") }
coVerify { userService.getFromUsername("existing") }
res.status() `should be equal to` HttpStatusCode.OK
val jsonObject = JSONObject(res.content)
@@ -106,7 +107,7 @@ class AuthControllerKtTest {
}
}
verify { userService.getFromUsername("wrong") }
coVerify { userService.getFromUsername("wrong") }
res.status() `should be equal to` HttpStatusCode.Unauthorized
res.content `should strictly be equal to json` """{msg: "Unauthorized"}"""
@@ -121,7 +122,7 @@ class AuthControllerKtTest {
}
}
verify { userService.getFromUsername("notExisting") }
coVerify { userService.getFromUsername("notExisting") }
res.status() `should be equal to` HttpStatusCode.Unauthorized
res.content `should strictly be equal to json` """{msg: "Unauthorized"}"""
@@ -154,7 +155,7 @@ class AuthControllerKtTest {
val jsonObject = JSONObject(res.content)
jsonObject.keyList() `should be equal to` listOf("token", "refreshToken")
verify { userService.userExists(1) }
coVerify { userService.userExists(1) }
res.status() `should be equal to` HttpStatusCode.OK
}
@@ -169,7 +170,7 @@ class AuthControllerKtTest {
}
}
verify { userService.userExists(3) }
coVerify { userService.userExists(3) }
res.status() `should be equal to` HttpStatusCode.Unauthorized
res.content `should strictly be equal to json` """{msg: "Unauthorized"}"""
}
@@ -7,7 +7,7 @@ import be.vandewalleh.module
import be.vandewalleh.services.UserService
import io.ktor.http.*
import io.ktor.server.testing.*
import io.mockk.every
import io.mockk.coEvery
import io.mockk.mockk
import org.amshove.kluent.*
import org.junit.jupiter.api.*
@@ -24,31 +24,31 @@ class UserControllerKtTest {
init {
// new user
every { userService.userExists("new", "new@test.com") } returns false
every { userService.createUser("new", "new@test.com", any()) } returns User {
coEvery { userService.userExists("new", "new@test.com") } returns false
coEvery { userService.createUser("new", "new@test.com", any()) } returns User {
this.createdAt = LocalDateTime.now()
this.username = "new"
this.email = "new@test.com"
}
// existing user
every { userService.userExists("existing", "existing@test.com") } returns true
every { userService.createUser("existing", "existing@test.com", any()) } returns null
every { userService.getUserId("existing@test.com") } returns 1
every { userService.deleteUser(1) } returns true andThen false
coEvery { userService.userExists("existing", "existing@test.com") } returns true
coEvery { userService.createUser("existing", "existing@test.com", any()) } returns null
coEvery { userService.getUserId("existing@test.com") } returns 1
coEvery { userService.deleteUser(1) } returns true andThen false
// modified user
every { userService.userExists("modified", "modified@test.com") } returns true
every {
coEvery { userService.userExists("modified", "modified@test.com") } returns true
coEvery {
userService.userExists(
and(not("modified"), not("existing")),
and(not("modified@test.com"), not("existing@test.com"))
)
} returns false
every { userService.userExists(1) } returns true
every { userService.createUser("modified", "modified@test.com", any()) } returns null
every { userService.getUserId("modified@test.com") } returns 1
every { userService.updateUser(1, "ThisIsMyNewName", "ThisIsMyNewName@mail.com", any()) } returns Unit
coEvery { userService.userExists(1) } returns true
coEvery { userService.createUser("modified", "modified@test.com", any()) } returns null
coEvery { userService.getUserId("modified@test.com") } returns 1
coEvery { userService.updateUser(1, "ThisIsMyNewName", "ThisIsMyNewName@mail.com", any()) } returns Unit
}
@@ -3,6 +3,7 @@ package integration.services
import be.vandewalleh.mainModule
import be.vandewalleh.migrations.Migration
import be.vandewalleh.services.UserService
import kotlinx.coroutines.runBlocking
import org.amshove.kluent.*
import org.junit.jupiter.api.*
import org.kodein.di.Kodein
@@ -36,34 +37,41 @@ class UserServiceTest {
@Test
@Order(1)
fun `test create user`() {
val username = "hubert"
val email = "a@a"
val password = "password"
println(userService.createUser(username, email, password))
runBlocking {
val username = "hubert"
val email = "a@a"
val password = "password"
val id = userService.getUserId(email)
id `should not be` null
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
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
runBlocking {
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)
runBlocking {
val email = "a@a"
val id = userService.getUserId(email)!!
userService.deleteUser(id)
userService.getUserId(email) `should be` null
userService.getUserInfo(id) `should be` null
userService.getUserId(email) `should be` null
userService.getUserInfo(id) `should be` null
}
}
}