From 11740e5986e23b3d6a5adc945e30844aa995b1fc Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Mon, 15 Jun 2020 16:47:03 +0200 Subject: [PATCH] Better login tests --- .../routing/AuthControllerKtTest.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/api/test/integration/routing/AuthControllerKtTest.kt b/api/test/integration/routing/AuthControllerKtTest.kt index 5c7956b..3048716 100644 --- a/api/test/integration/routing/AuthControllerKtTest.kt +++ b/api/test/integration/routing/AuthControllerKtTest.kt @@ -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 + } + }