Fix failure in UserRouteTest

This commit is contained in:
William Brawner 2024-04-20 22:04:49 -06:00
parent 197fee1033
commit 955ee0f126
2 changed files with 8 additions and 4 deletions

View file

@ -12,6 +12,7 @@ import io.ktor.client.*
import io.ktor.client.plugins.contentnegotiation.* import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.* import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.testing.* import io.ktor.server.testing.*
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
open class ApiTest { open class ApiTest {
@ -70,7 +71,10 @@ open class ApiTest {
} }
val client = createClient { val client = createClient {
install(ContentNegotiation) { install(ContentNegotiation) {
json() json(json = Json {
encodeDefaults = true
explicitNulls = false
})
} }
} }
test(client) test(client)

View file

@ -77,7 +77,7 @@ class UserRouteTest : ApiTest() {
@Test @Test
fun `login with valid email and password returns 200`() = apiTest { client -> fun `login with valid email and password returns 200`() = apiTest { client ->
val request = LoginRequest(TEST_USER.email, TEST_USER.password) val request = LoginRequest(requireNotNull(TEST_USER.email), TEST_USER.password)
val response = client.post("/api/users/login") { val response = client.post("/api/users/login") {
header("Content-Type", "application/json") header("Content-Type", "application/json")
setBody(request) setBody(request)
@ -172,14 +172,14 @@ class UserRouteTest : ApiTest() {
val userResponse = response.body<UserResponse>() val userResponse = response.body<UserResponse>()
assert(userResponse.id.isNotBlank()) assert(userResponse.id.isNotBlank())
assertEquals(request.username, userResponse.username) assertEquals(request.username, userResponse.username)
assertEquals("", userResponse.email) assertEquals(null, userResponse.email)
assertEquals(initialUserCount + 1, userRepository.entities.size) assertEquals(initialUserCount + 1, userRepository.entities.size)
val savedUser: User? = userRepository.findAll("newuser").firstOrNull() val savedUser: User? = userRepository.findAll("newuser").firstOrNull()
assertNotNull(savedUser) assertNotNull(savedUser)
requireNotNull(savedUser) requireNotNull(savedUser)
assertEquals(userResponse.id, savedUser.id) assertEquals(userResponse.id, savedUser.id)
assertEquals(request.username, savedUser.name) assertEquals(request.username, savedUser.name)
assertEquals("", savedUser.email) assertEquals(null, savedUser.email)
assertEquals("newpass", savedUser.password) assertEquals("newpass", savedUser.password)
} }