Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 2a7c376

Browse files
committed
Added test for authentication selectors
1 parent 4bb7a0f commit 2a7c376

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/main/kotlin/io/kotest/extensions/ktor/openapi/OpenApiPlugin.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ val OpenApi = createApplicationPlugin("OpenApi", createConfiguration = ::OpenApi
4444
// writer.write(this.pluginConfig.path)
4545
// }
4646

47-
fun Route.authentication(): List<String> {
48-
return selectors().filterIsInstance<AuthenticationRouteSelector>().flatMap { it.names }.filterNotNull()
49-
}
50-
5147
environment!!.monitor.subscribe(Routing.RoutingCallStarted) { call ->
5248
val trace = call.attributes[traceKey]
5349
trace.path = call.route.path()

src/main/kotlin/io/kotest/extensions/ktor/openapi/routes.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.kotest.extensions.ktor.openapi
22

3+
import io.ktor.server.auth.AuthenticationRouteSelector
34
import io.ktor.server.routing.PathSegmentConstantRouteSelector
45
import io.ktor.server.routing.PathSegmentOptionalParameterRouteSelector
56
import io.ktor.server.routing.PathSegmentParameterRouteSelector
@@ -42,3 +43,11 @@ fun Route.path() = selectors().mapNotNull {
4243
fun Route.pathParameters(): List<String> {
4344
return selectors().filterIsInstance<PathSegmentParameterRouteSelector>().map { it.name }
4445
}
46+
47+
/**
48+
* Extracts the [RouteSelector]s for this [Route] and returns the name of any authentication providers
49+
* that have been applied.
50+
*/
51+
fun Route.authentication(): List<String> {
52+
return selectors().filterIsInstance<AuthenticationRouteSelector>().flatMap { it.names }.filterNotNull()
53+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.kotest.extensions.ktor.openapi
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
import io.kotest.matchers.shouldBe
5+
import io.ktor.server.auth.Authentication
6+
import io.ktor.server.auth.authenticate
7+
import io.ktor.server.auth.basic
8+
import io.ktor.server.routing.get
9+
import io.ktor.server.testing.testApplication
10+
11+
class AuthenticationSelectorTest : FunSpec() {
12+
init {
13+
test("Route.authentication should return all applied authentication providers") {
14+
testApplication {
15+
install(Authentication) {
16+
basic("foo") {}
17+
basic("bar") {}
18+
}
19+
routing {
20+
authenticate("foo", "bar") {
21+
val r = get("/a") { }
22+
r.authentication() shouldBe listOf("foo", "bar")
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}

src/test/kotlin/io/kotest/extensions/ktor/openapi/PathParametersTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import io.ktor.server.testing.testApplication
88

99
class PathParametersTest : FunSpec() {
1010
init {
11-
test("Route.selectors should return all path parameters from parameter selectors") {
11+
test("Route.pathParameters should return all path parameters from parameter selectors") {
1212
testApplication {
1313
routing {
1414
route("/a") {

0 commit comments

Comments
 (0)