Skip to content

Commit 466a591

Browse files
authored
Enhance integration tests (#27)
* enhance HierarchyTest * enhance UserTest * enhance application test * remove hash assertion
1 parent 94a139b commit 466a591

File tree

5 files changed

+104
-89
lines changed

5 files changed

+104
-89
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.personia
2+
3+
import com.personia.plugins.configureRouting
4+
import io.ktor.client.*
5+
import io.ktor.client.plugins.contentnegotiation.*
6+
import io.ktor.serialization.gson.*
7+
import io.ktor.server.config.*
8+
import io.ktor.server.testing.*
9+
import kotlin.test.assertNotNull
10+
11+
fun ApplicationTestBuilder.configIntegrationTestApp(): HttpClient {
12+
val client = createClient {
13+
install(ContentNegotiation) {
14+
gson()
15+
}
16+
}
17+
assertNotNull(client)
18+
environment {
19+
config = ApplicationConfig("application-test.conf")
20+
}
21+
application {
22+
configureRouting(environment.config)
23+
}
24+
return client
25+
}
26+
27+
fun ApplicationTestBuilder.configureUnitTestApp(){
28+
createClient { }
29+
environment {
30+
config = ApplicationConfig("application-test.conf")
31+
}
32+
}
Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,58 @@
11
package com.personia.integrations
22

3+
import com.personia.configIntegrationTestApp
34
import com.personia.dto.Token
4-
import com.personia.plugins.configureRouting
55
import com.personia.utils.randomString
66
import io.ktor.client.*
77
import io.ktor.client.call.*
8-
import io.ktor.client.plugins.contentnegotiation.*
98
import io.ktor.client.request.*
109
import io.ktor.client.statement.*
1110
import io.ktor.http.*
12-
import io.ktor.serialization.gson.*
13-
import io.ktor.server.config.*
1411
import io.ktor.server.testing.*
1512
import kotlin.test.Test
1613
import kotlin.test.assertEquals
17-
import kotlin.test.assertNotNull
1814
import kotlin.test.assertTrue
1915

2016
class HierarchyTest {
21-
2217
@Test
2318
fun testHierarchy() = testApplication {
24-
val client = createClient {
25-
install(ContentNegotiation) {
26-
gson()
27-
}
28-
}
29-
assertNotNull(client)
30-
environment {
31-
config = ApplicationConfig("application-test.conf")
32-
}
33-
application {
34-
configureRouting(environment.config)
35-
}
36-
val responseLogin = authenticateClient(client)
37-
assertNotNull(responseLogin)
38-
val accessToken = responseLogin.body<Token?>()
39-
assertNotNull(accessToken)
40-
41-
// Testing hierarchy endpoint
19+
val client = configIntegrationTestApp()
20+
val token = authenticate(client)
4221
val responseHierarchy = client.post("/hierarchy") {
4322
contentType(ContentType.Application.Json)
44-
header("Authorization", "Bearer ${accessToken.token}")
23+
header("Authorization", "Bearer $token")
4524
setBody(mapOf("Nick" to "Sophie", "Sophie" to "Jonas", "Pete" to "Nick", "Barbara" to "Nick"))
4625
}
4726
assertEquals(HttpStatusCode.OK, responseHierarchy.status)
4827
assertTrue(responseHierarchy.bodyAsText().contains("Jonas"))
28+
}
4929

50-
// Testing supervisors
30+
@Test
31+
fun testSupervisor() = testApplication {
32+
val client = configIntegrationTestApp()
33+
val token = authenticate(client)
5134
val response = client.get("hierarchy/Nick/supervisors") {
5235
contentType(ContentType.Application.Json)
53-
header("Authorization", "Bearer ${accessToken.token}")
36+
header("Authorization", "Bearer $token")
5437
}
5538
assertEquals(HttpStatusCode.OK, response.status)
5639
assertTrue(response.bodyAsText().contains("Jonas"))
5740
}
5841

59-
private suspend fun authenticateClient(client: HttpClient): HttpResponse {
42+
private suspend fun authenticate(client: HttpClient): String?{
6043
val credential = mapOf(
6144
"username" to randomString(10),
6245
"password" to randomString(10)
6346
)
64-
65-
val responseSignUp = client.post("/signup") {
47+
client.post("/signup") {
6648
contentType(ContentType.Application.Json)
6749
setBody(credential)
6850
}
69-
assertEquals(HttpStatusCode.Created, responseSignUp.status)
70-
assertEquals("User singed up", responseSignUp.bodyAsText())
71-
72-
// Testing login endpoint
7351
val responseLogin = client.post("/login") {
7452
contentType(ContentType.Application.Json)
7553
setBody(credential)
7654
}
77-
assertEquals(HttpStatusCode.OK, responseLogin.status)
78-
return responseLogin
55+
return responseLogin.body<Token?>()?.token
56+
7957
}
8058
}

src/test/kotlin/com/personia/integrations/UserTest.kt

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,82 @@
11
package com.personia.integrations
22

3-
import com.personia.plugins.configureRouting
3+
import com.personia.configIntegrationTestApp
44
import com.personia.utils.randomString
5-
import io.ktor.client.plugins.contentnegotiation.*
65
import io.ktor.client.request.*
76
import io.ktor.client.statement.*
87
import io.ktor.http.*
9-
import io.ktor.serialization.gson.*
10-
import io.ktor.server.config.*
118
import io.ktor.server.testing.*
129
import kotlin.test.Test
1310
import kotlin.test.assertEquals
1411
import kotlin.test.assertTrue
1512

1613
class UserTest {
1714

15+
private val credentials = mapOf(
16+
"username" to randomString(10),
17+
"password" to randomString(10)
18+
)
1819
@Test
19-
fun testAuthFlow() = testApplication {
20-
val client = createClient {
21-
install(ContentNegotiation) {
22-
gson()
23-
}
24-
}
25-
environment {
26-
config = ApplicationConfig("application-test.conf")
27-
}
28-
application {
29-
configureRouting(environment.config)
30-
}
20+
fun testSignUp() = testApplication {
21+
val client = configIntegrationTestApp()
3122

32-
val credentials = mapOf(
33-
"username" to randomString(10),
34-
"password" to randomString(10)
35-
)
36-
37-
// Test: sing up user
3823
val responseSignUp = client.post("/signup") {
3924
contentType(ContentType.Application.Json)
4025
setBody(credentials)
4126
}
4227
assertEquals(HttpStatusCode.Created, responseSignUp.status)
4328
assertEquals("User singed up", responseSignUp.bodyAsText())
29+
}
4430

45-
// Test: duplicate username exception
31+
@Test
32+
fun testDuplicateSignup() = testApplication{
33+
val client = configIntegrationTestApp()
34+
client.post("/signup") {
35+
contentType(ContentType.Application.Json)
36+
setBody(credentials)
37+
}
4638
val responseDupSignUp = client.post("/signup") {
4739
contentType(ContentType.Application.Json)
4840
setBody(credentials)
4941
}
5042
assertEquals(HttpStatusCode.BadRequest, responseDupSignUp.status)
5143
assertTrue(responseDupSignUp.bodyAsText().contains("duplicate key value violates unique constraint."))
44+
}
5245

53-
// Test: login user
46+
@Test
47+
fun testLogin() = testApplication {
48+
val client = configIntegrationTestApp()
49+
client.post("/signup") {
50+
contentType(ContentType.Application.Json)
51+
setBody(credentials)
52+
}
5453
val responseLogin = client.post("/login") {
5554
contentType(ContentType.Application.Json)
5655
setBody(credentials)
5756
}
5857
assertEquals(HttpStatusCode.OK, responseLogin.status)
5958
assertTrue(responseLogin.bodyAsText().contains("token"))
59+
}
6060

61-
// Test: login with the wrong password
61+
@Test
62+
fun testLoginWrongPass()= testApplication {
63+
val client = configIntegrationTestApp()
64+
client.post("/signup") {
65+
contentType(ContentType.Application.Json)
66+
setBody(credentials)
67+
}
6268
val wrongPasswordCredential = credentials + mapOf("password" to randomString(15))
6369
val responseWrongPassword = client.post("/login") {
6470
contentType(ContentType.Application.Json)
6571
setBody(wrongPasswordCredential)
6672
}
6773
assertEquals(HttpStatusCode.BadRequest, responseWrongPassword.status)
6874
assertTrue(responseWrongPassword.bodyAsText().contains("Wrong Password"))
75+
}
6976

70-
// Test: login with username that doesn't exist in the system
77+
@Test
78+
fun testLoginForNonExistingUser() = testApplication {
79+
val client = configIntegrationTestApp()
7180
val wrongUsernameCredential = credentials + mapOf("username" to randomString(12))
7281
val responseUserNotFound = client.post("/login") {
7382
contentType(ContentType.Application.Json)

src/test/kotlin/com/personia/services/NodeServiceTest.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
package com.personia.services
22

3-
import io.ktor.server.config.*
3+
import com.personia.configureUnitTestApp
44
import io.ktor.server.testing.*
55
import org.junit.Test
66
import kotlin.test.assertNotEquals
77
import kotlin.test.assertNotNull
8+
89
class NodeServiceTest {
910

10-
private fun nodeServiceTest(test: suspend () -> Unit) = testApplication {
11-
createClient { }
12-
environment {
13-
config = ApplicationConfig("application-test.conf")
14-
}
15-
test()
16-
}
1711
@Test
18-
fun getHierarchy() = nodeServiceTest {
12+
fun getHierarchy() = testApplication {
13+
configureUnitTestApp()
1914
val hierarchy = nodeService.getHierarchy()
2015
assertNotNull(hierarchy)
2116
}
2217

2318
@Test
24-
fun findByName() = nodeServiceTest {
19+
fun findByName() = testApplication {
20+
configureUnitTestApp()
2521
nodeService.createHierarchy(
2622
mapOf(
2723
"Pete" to "Nick",
@@ -36,7 +32,8 @@ class NodeServiceTest {
3632
}
3733

3834
@Test
39-
fun createHierarchy() = nodeServiceTest {
35+
fun createHierarchy() = testApplication {
36+
configureUnitTestApp()
4037
val response = nodeService.createHierarchy(
4138
mapOf(
4239
"Pete" to "Nick",
@@ -49,7 +46,8 @@ class NodeServiceTest {
4946
}
5047

5148
@Test
52-
fun retrieveSupervisors() = nodeServiceTest {
49+
fun retrieveSupervisors() = testApplication {
50+
configureUnitTestApp()
5351
nodeService.createHierarchy(
5452
mapOf(
5553
"Pete" to "Nick",

src/test/kotlin/com/personia/services/UserServiceTest.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
package com.personia.services
22

3+
import com.personia.configureUnitTestApp
34
import com.personia.dto.User
45
import com.personia.utils.randomString
5-
import io.ktor.server.config.*
66
import io.ktor.server.testing.*
77
import org.junit.Test
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertNotEquals
810
import kotlin.test.assertNotNull
911
import kotlin.test.assertTrue
1012

1113
class UserServiceTest {
12-
13-
private fun userServiceTest(test: suspend () -> Unit) = testApplication {
14-
createClient { }
15-
environment {
16-
config = ApplicationConfig("application-test.conf")
17-
}
18-
test()
19-
}
14+
private val username = randomString(12)
15+
private val password = randomString(16)
2016

2117
@Test
22-
fun createUser() = userServiceTest {
18+
fun createUser() = testApplication {
19+
configureUnitTestApp()
2320
val user = userService.createUser(
2421
User(
25-
username = randomString(12),
26-
password = randomString(20)
22+
username = username,
23+
password = password
2724
)
2825
)
2926

3027
assertNotNull(user)
3128
assertNotNull(user.id)
29+
assertEquals(username, user.username)
30+
assertNotEquals(password, user.password)
3231
}
3332

3433
@Test
35-
fun loginUser() = userServiceTest {
36-
val username = randomString(12)
37-
val password = randomString(16)
34+
fun loginUser() = testApplication {
35+
configureUnitTestApp()
3836
userService.createUser(
3937
User(
4038
username = username,

0 commit comments

Comments
 (0)