Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance integration tests #27

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/test/kotlin/com/personia/Configuration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.personia

import com.personia.plugins.configureRouting
import io.ktor.client.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.gson.*
import io.ktor.server.config.*
import io.ktor.server.testing.*
import kotlin.test.assertNotNull

fun ApplicationTestBuilder.configIntegrationTestApp(): HttpClient {
val client = createClient {
install(ContentNegotiation) {
gson()
}
}
assertNotNull(client)
environment {
config = ApplicationConfig("application-test.conf")
}
application {
configureRouting(environment.config)
}
return client
}

fun ApplicationTestBuilder.configureUnitTestApp(){
createClient { }
environment {
config = ApplicationConfig("application-test.conf")
}
}
50 changes: 14 additions & 36 deletions src/test/kotlin/com/personia/integrations/HierarchyTest.kt
Original file line number Diff line number Diff line change
@@ -1,80 +1,58 @@
package com.personia.integrations

import com.personia.configIntegrationTestApp
import com.personia.dto.Token
import com.personia.plugins.configureRouting
import com.personia.utils.randomString
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.gson.*
import io.ktor.server.config.*
import io.ktor.server.testing.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class HierarchyTest {

@Test
fun testHierarchy() = testApplication {
val client = createClient {
install(ContentNegotiation) {
gson()
}
}
assertNotNull(client)
environment {
config = ApplicationConfig("application-test.conf")
}
application {
configureRouting(environment.config)
}
val responseLogin = authenticateClient(client)
assertNotNull(responseLogin)
val accessToken = responseLogin.body<Token?>()
assertNotNull(accessToken)

// Testing hierarchy endpoint
val client = configIntegrationTestApp()
val token = authenticate(client)
val responseHierarchy = client.post("/hierarchy") {
contentType(ContentType.Application.Json)
header("Authorization", "Bearer ${accessToken.token}")
header("Authorization", "Bearer $token")
setBody(mapOf("Nick" to "Sophie", "Sophie" to "Jonas", "Pete" to "Nick", "Barbara" to "Nick"))
}
assertEquals(HttpStatusCode.OK, responseHierarchy.status)
assertTrue(responseHierarchy.bodyAsText().contains("Jonas"))
}

// Testing supervisors
@Test
fun testSupervisor() = testApplication {
val client = configIntegrationTestApp()
val token = authenticate(client)
val response = client.get("hierarchy/Nick/supervisors") {
contentType(ContentType.Application.Json)
header("Authorization", "Bearer ${accessToken.token}")
header("Authorization", "Bearer $token")
}
assertEquals(HttpStatusCode.OK, response.status)
assertTrue(response.bodyAsText().contains("Jonas"))
}

private suspend fun authenticateClient(client: HttpClient): HttpResponse {
private suspend fun authenticate(client: HttpClient): String?{
val credential = mapOf(
"username" to randomString(10),
"password" to randomString(10)
)

val responseSignUp = client.post("/signup") {
client.post("/signup") {
contentType(ContentType.Application.Json)
setBody(credential)
}
assertEquals(HttpStatusCode.Created, responseSignUp.status)
assertEquals("User singed up", responseSignUp.bodyAsText())

// Testing login endpoint
val responseLogin = client.post("/login") {
contentType(ContentType.Application.Json)
setBody(credential)
}
assertEquals(HttpStatusCode.OK, responseLogin.status)
return responseLogin
return responseLogin.body<Token?>()?.token

}
}
61 changes: 35 additions & 26 deletions src/test/kotlin/com/personia/integrations/UserTest.kt
Original file line number Diff line number Diff line change
@@ -1,73 +1,82 @@
package com.personia.integrations

import com.personia.plugins.configureRouting
import com.personia.configIntegrationTestApp
import com.personia.utils.randomString
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.gson.*
import io.ktor.server.config.*
import io.ktor.server.testing.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class UserTest {

private val credentials = mapOf(
"username" to randomString(10),
"password" to randomString(10)
)
@Test
fun testAuthFlow() = testApplication {
val client = createClient {
install(ContentNegotiation) {
gson()
}
}
environment {
config = ApplicationConfig("application-test.conf")
}
application {
configureRouting(environment.config)
}
fun testSignUp() = testApplication {
val client = configIntegrationTestApp()

val credentials = mapOf(
"username" to randomString(10),
"password" to randomString(10)
)

// Test: sing up user
val responseSignUp = client.post("/signup") {
contentType(ContentType.Application.Json)
setBody(credentials)
}
assertEquals(HttpStatusCode.Created, responseSignUp.status)
assertEquals("User singed up", responseSignUp.bodyAsText())
}

// Test: duplicate username exception
@Test
fun testDuplicateSignup() = testApplication{
val client = configIntegrationTestApp()
client.post("/signup") {
contentType(ContentType.Application.Json)
setBody(credentials)
}
val responseDupSignUp = client.post("/signup") {
contentType(ContentType.Application.Json)
setBody(credentials)
}
assertEquals(HttpStatusCode.BadRequest, responseDupSignUp.status)
assertTrue(responseDupSignUp.bodyAsText().contains("duplicate key value violates unique constraint."))
}

// Test: login user
@Test
fun testLogin() = testApplication {
val client = configIntegrationTestApp()
client.post("/signup") {
contentType(ContentType.Application.Json)
setBody(credentials)
}
val responseLogin = client.post("/login") {
contentType(ContentType.Application.Json)
setBody(credentials)
}
assertEquals(HttpStatusCode.OK, responseLogin.status)
assertTrue(responseLogin.bodyAsText().contains("token"))
}

// Test: login with the wrong password
@Test
fun testLoginWrongPass()= testApplication {
val client = configIntegrationTestApp()
client.post("/signup") {
contentType(ContentType.Application.Json)
setBody(credentials)
}
val wrongPasswordCredential = credentials + mapOf("password" to randomString(15))
val responseWrongPassword = client.post("/login") {
contentType(ContentType.Application.Json)
setBody(wrongPasswordCredential)
}
assertEquals(HttpStatusCode.BadRequest, responseWrongPassword.status)
assertTrue(responseWrongPassword.bodyAsText().contains("Wrong Password"))
}

// Test: login with username that doesn't exist in the system
@Test
fun testLoginForNonExistingUser() = testApplication {
val client = configIntegrationTestApp()
val wrongUsernameCredential = credentials + mapOf("username" to randomString(12))
val responseUserNotFound = client.post("/login") {
contentType(ContentType.Application.Json)
Expand Down
22 changes: 10 additions & 12 deletions src/test/kotlin/com/personia/services/NodeServiceTest.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.personia.services

import io.ktor.server.config.*
import com.personia.configureUnitTestApp
import io.ktor.server.testing.*
import org.junit.Test
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull

class NodeServiceTest {

private fun nodeServiceTest(test: suspend () -> Unit) = testApplication {
createClient { }
environment {
config = ApplicationConfig("application-test.conf")
}
test()
}
@Test
fun getHierarchy() = nodeServiceTest {
fun getHierarchy() = testApplication {
configureUnitTestApp()
val hierarchy = nodeService.getHierarchy()
assertNotNull(hierarchy)
}

@Test
fun findByName() = nodeServiceTest {
fun findByName() = testApplication {
configureUnitTestApp()
nodeService.createHierarchy(
mapOf(
"Pete" to "Nick",
Expand All @@ -36,7 +32,8 @@ class NodeServiceTest {
}

@Test
fun createHierarchy() = nodeServiceTest {
fun createHierarchy() = testApplication {
configureUnitTestApp()
val response = nodeService.createHierarchy(
mapOf(
"Pete" to "Nick",
Expand All @@ -49,7 +46,8 @@ class NodeServiceTest {
}

@Test
fun retrieveSupervisors() = nodeServiceTest {
fun retrieveSupervisors() = testApplication {
configureUnitTestApp()
nodeService.createHierarchy(
mapOf(
"Pete" to "Nick",
Expand Down
28 changes: 13 additions & 15 deletions src/test/kotlin/com/personia/services/UserServiceTest.kt
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
package com.personia.services

import com.personia.configureUnitTestApp
import com.personia.dto.User
import com.personia.utils.randomString
import io.ktor.server.config.*
import io.ktor.server.testing.*
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class UserServiceTest {

private fun userServiceTest(test: suspend () -> Unit) = testApplication {
createClient { }
environment {
config = ApplicationConfig("application-test.conf")
}
test()
}
private val username = randomString(12)
private val password = randomString(16)

@Test
fun createUser() = userServiceTest {
fun createUser() = testApplication {
configureUnitTestApp()
val user = userService.createUser(
User(
username = randomString(12),
password = randomString(20)
username = username,
password = password
)
)

assertNotNull(user)
assertNotNull(user.id)
assertEquals(username, user.username)
assertNotEquals(password, user.password)
}

@Test
fun loginUser() = userServiceTest {
val username = randomString(12)
val password = randomString(16)
fun loginUser() = testApplication {
configureUnitTestApp()
userService.createUser(
User(
username = username,
Expand Down
Loading