-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d7d057
commit ab18fc9
Showing
5 changed files
with
95 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
56 changes: 13 additions & 43 deletions
56
src/test/kotlin/com/personia/integrations/HierarchyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +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 { | ||
|
||
private fun hierarchyTestApp(test: suspend ApplicationTestBuilder.() -> Unit) = testApplication { | ||
val client = createClient { | ||
install(ContentNegotiation) { | ||
gson() | ||
} | ||
} | ||
assertNotNull(client) | ||
environment { | ||
config = ApplicationConfig("application-test.conf") | ||
} | ||
application { | ||
configureRouting(environment.config) | ||
} | ||
test() | ||
} | ||
|
||
@Test | ||
fun testHierarchy() = hierarchyTestApp { | ||
val responseLogin = authenticateClient(client) | ||
assertNotNull(responseLogin) | ||
val accessToken = responseLogin.body<Token?>() | ||
assertNotNull(accessToken) | ||
fun testHierarchy() = testApplication { | ||
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")) | ||
} | ||
|
||
@Test | ||
fun testSupervisor() = hierarchyTestApp { | ||
val responseLogin = authenticateClient(client) | ||
assertNotNull(responseLogin) | ||
val accessToken = responseLogin.body<Token?>() | ||
assertNotNull(accessToken) | ||
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 | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters