Better login tests

This commit is contained in:
Hubert Van De Walle 2020-06-15 16:47:03 +02:00
parent 78454c5db5
commit 11740e5986

View File

@ -5,11 +5,11 @@ 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 io.mockk.verify
import org.amshove.kluent.*
import org.json.JSONObject
import org.junit.jupiter.api.*
@ -33,7 +33,7 @@ class AuthControllerKtTest {
user["id"] = 1
every { userService.getFromUsername("existing") } returns user
every { userService.userExists(1) } returns true
val user2 = User {
password = BCrypt.hashpw("right password", BCrypt.gensalt())
@ -66,6 +66,9 @@ class AuthControllerKtTest {
it["password"] = "password"
}
}
verify { userService.getFromUsername("existing") }
res.status() `should be equal to` HttpStatusCode.OK
val jsonObject = JSONObject(res.content)
@ -91,6 +94,9 @@ class AuthControllerKtTest {
it["password"] = "not this"
}
}
verify { userService.getFromUsername("wrong") }
res.status() `should be equal to` HttpStatusCode.Unauthorized
res.content `should strictly be equal to json` """{msg: "Unauthorized"}"""
}
@ -103,10 +109,21 @@ class AuthControllerKtTest {
it["password"] = "babababa"
}
}
verify { userService.getFromUsername("notExisting") }
res.status() `should be equal to` HttpStatusCode.Unauthorized
res.content `should strictly be equal to json` """{msg: "Unauthorized"}"""
}
@Test
fun `login without body`() {
val res = testEngine.post("/user/login") {
addHeader(HttpHeaders.ContentType, "application/json")
}
res.status() `should be equal to` HttpStatusCode.BadRequest
}
}