Test login
This commit is contained in:
parent
47dc1a582b
commit
78454c5db5
113
api/test/integration/routing/AuthControllerKtTest.kt
Normal file
113
api/test/integration/routing/AuthControllerKtTest.kt
Normal file
@ -0,0 +1,113 @@
|
||||
package integration.routing
|
||||
|
||||
import be.vandewalleh.auth.SimpleJWT
|
||||
import be.vandewalleh.entities.User
|
||||
import be.vandewalleh.mainModule
|
||||
import be.vandewalleh.module
|
||||
import be.vandewalleh.services.UserService
|
||||
import com.auth0.jwt.exceptions.JWTVerificationException
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.testing.*
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.amshove.kluent.*
|
||||
import org.json.JSONObject
|
||||
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.*
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class AuthControllerKtTest {
|
||||
|
||||
private val userService = mockk<UserService>()
|
||||
|
||||
init {
|
||||
|
||||
val user = User {
|
||||
password = BCrypt.hashpw("password", BCrypt.gensalt())
|
||||
username = "existing"
|
||||
}
|
||||
user["id"] = 1
|
||||
|
||||
every { userService.getFromUsername("existing") } returns user
|
||||
|
||||
|
||||
val user2 = User {
|
||||
password = BCrypt.hashpw("right password", BCrypt.gensalt())
|
||||
username = "wrong"
|
||||
}
|
||||
user["id"] = 2
|
||||
every { userService.getFromUsername("wrong") } returns user2
|
||||
|
||||
every { userService.getFromUsername("notExisting") } returns null
|
||||
}
|
||||
|
||||
|
||||
private val kodein = Kodein {
|
||||
import(mainModule, allowOverride = true)
|
||||
bind<UserService>(overrides = true) with instance(userService)
|
||||
}
|
||||
|
||||
private val testEngine = TestApplicationEngine().apply {
|
||||
start()
|
||||
application.module(kodein)
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class Login {
|
||||
@Test
|
||||
fun `login existing user with valid password`() {
|
||||
val res = testEngine.post("/user/login") {
|
||||
json {
|
||||
it["username"] = "existing"
|
||||
it["password"] = "password"
|
||||
}
|
||||
}
|
||||
res.status() `should be equal to` HttpStatusCode.OK
|
||||
val jsonObject = JSONObject(res.content)
|
||||
|
||||
val hasToken = jsonObject.has("token")
|
||||
hasToken `should be equal to` true
|
||||
|
||||
jsonObject.keyList() `should be equal to` listOf("token", "refreshToken")
|
||||
|
||||
val authJwt by kodein.instance<SimpleJWT>(tag = "auth")
|
||||
val token = jsonObject.getString("token")
|
||||
authJwt.verifier.verify(token)
|
||||
|
||||
val refreshJwt by kodein.instance<SimpleJWT>(tag = "refresh")
|
||||
val refreshToken = jsonObject.getString("refreshToken")
|
||||
refreshJwt.verifier.verify(refreshToken)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `login existing user with invalid password`() {
|
||||
val res = testEngine.post("/user/login") {
|
||||
json {
|
||||
it["username"] = "wrong"
|
||||
it["password"] = "not this"
|
||||
}
|
||||
}
|
||||
res.status() `should be equal to` HttpStatusCode.Unauthorized
|
||||
res.content `should strictly be equal to json` """{msg: "Unauthorized"}"""
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `login not existing user`() {
|
||||
val res = testEngine.post("/user/login") {
|
||||
json {
|
||||
it["username"] = "notExisting"
|
||||
it["password"] = "babababa"
|
||||
}
|
||||
}
|
||||
res.status() `should be equal to` HttpStatusCode.Unauthorized
|
||||
res.content `should strictly be equal to json` """{msg: "Unauthorized"}"""
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
5
api/test/utils/OrgJsonExtensions.kt
Normal file
5
api/test/utils/OrgJsonExtensions.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package utils
|
||||
|
||||
import org.json.JSONObject
|
||||
|
||||
fun JSONObject.keyList(): List<Any?> = keys().asSequence().toList()
|
||||
Loading…
x
Reference in New Issue
Block a user