Warden Help

Testing

Everything in Warden is a plain object, so policies, services and routes can be tested without a server, a database or any mocking.

Test policies directly

Policy.checkAuthorized is an ordinary function. The decision point evaluates the allow list as an AnyOf, so wrap your policies in one and call it with hand-built requests:

class BlogPoliciesTest { private val ada = User(id = "ada", roles = listOf("Author")) private val bob = User(id = "bob", roles = emptyList()) private val article = Article(id = "a1", title = "Warden", authorId = "ada") /** * A policy is a plain function of the request; `AnyOf` over the list is what the decision point * does. */ private val policy = AnyOf(articlePolicies) private fun request(user: User, action: ArticleAction) = AccessRequest(subject = user.atts(), action = action.atts(), resource = article.atts()) @Test fun `anyone can read`() { assertThat(policy.checkAuthorized(request(bob, ArticleAction.READ)).access) .isInstanceOf(Access.Granted::class) } @Test fun `only the author can delete`() { assertThat(policy.checkAuthorized(request(ada, ArticleAction.DELETE)).access) .isInstanceOf(Access.Granted::class) assertThat(policy.checkAuthorized(request(bob, ArticleAction.DELETE)).access) .isInstanceOf(Access.Denied::class) } @Test fun `only authors can create`() { assertThat(policy.checkAuthorized(request(ada, ArticleAction.CREATE)).access) .isEqualTo(Access.Granted()) assertThat(policy.checkAuthorized(request(bob, ArticleAction.CREATE)).access) .isEqualTo(Access.Denied()) } }

These tests pin down the rules independently of any code that uses them. A rule that is hard to test this way is usually a rule that needs an information point.

Test a service

Build the service with EnforcementPointDefault(policies) and assert that denied operations throw before they change anything:

class BlogServiceTest { private val ada = User(id = "ada", roles = listOf("Author")) private val bob = User(id = "bob", roles = emptyList()) private val adasArticle = Article(id = "a1", title = "Warden", authorId = "ada") private val bobsArticle = Article(id = "b1", title = "Ktor", authorId = "bob") private val articles = ArticleStore(listOf(adasArticle, bobsArticle)) private val service = ArticleService(articles, EnforcementPointDefault(articlePolicies)) @Test fun `a granted request proceeds`() { runBlocking { val article = service.readArticle(bob, "a1") assertThat(article).isEqualTo(adasArticle) } } @Test fun `a denied request throws before anything happens`() { runBlocking { assertFailsWith<NotAuthorizedException> { service.deleteArticle(bob, "a1") } assertThat(articles.all()).containsExactly(adasArticle, bobsArticle) } } @Test fun `filtering keeps only the resources the user may act on`() { runBlocking { assertThat(service.listArticles(bob)).containsExactly(adasArticle, bobsArticle) } } }

Test the decision point

A decision point with information points and deny policies is tested the same way. Pass functions for what the information points fetch, so the test controls the roles and the clock:

class DecisionsTest { private val roles = mapOf("ada" to listOf("Author")) private var hour = 10 private val decisionPoint = blogDecisionPoint(rolesOf = { roles[it] ?: emptyList() }, hourOfDay = { hour }) /** The subject carries only its type and ID; the information point adds the roles. */ private val adaById = UserType.withAtts("id" to "ada") @Test fun `the information point supplies what the policy needs`() { runBlocking { val draft = Article(id = "a2", title = "Draft", authorId = "ada") val response = decisionPoint.checkAuthorized( AccessRequest( subject = adaById, action = ArticleAction.CREATE.atts(), resource = draft.atts(), )) assertThat(response.access).isInstanceOf(Access.Granted::class) assertThat(response.request.subject["roles"]).isEqualTo(listOf("Author")) } } @Test fun `a deny policy overrides an allow policy`() { runBlocking { hour = 22 val article = Article(id = "a1", title = "Warden", authorId = "ada") val response = decisionPoint.checkAuthorized( AccessRequest( subject = adaById, action = ArticleAction.DELETE.atts(), resource = article.atts(), )) assertThat(response.access).isInstanceOf(Access.Denied::class) } } @Test fun `a resource type without policies is denied`() { runBlocking { val response = decisionPoint.checkAuthorized( AccessRequest( subject = adaById, action = ArticleAction.READ.atts(), resource = mapOf("type" to "Comment"), )) assertThat(response.access).isInstanceOf(Access.Denied::class) } } }

Test a Ktor application

Ktor's testApplication runs the module in process. Besides the granted and denied cases, assert that a route which forgets to enforce is refused by the plugin, and that the routes you meant to leave open are open:

class KtorAppTest { private val users = mapOf( "ada" to User(id = "ada", roles = listOf("Author")), "bob" to User(id = "bob", roles = emptyList()), ) private val articles = listOf(Article(id = "a1", title = "Warden", authorId = "ada")) @Test fun `a route that enforces authorization responds normally`() = testApplication { application { blogModule(users, ArticleStore(articles)) } val response = client.get("/articles/a1") { header("X-User", "bob") } assertThat(response.status).isEqualTo(HttpStatusCode.OK) assertThat(response.bodyAsText()).isEqualTo("Warden") } @Test fun `a denied request gets the status from StatusPages`() = testApplication { application { blogModule(users, ArticleStore(articles)) } val response = client.delete("/articles/a1") { header("X-User", "bob") } assertThat(response.status).isEqualTo(HttpStatusCode.Forbidden) assertThat(response.bodyAsText()).isEqualTo("Not Authorized") } @Test fun `a route that forgets to enforce authorization is refused by the plugin`() = testApplication { application { blogModule(users, ArticleStore(articles)) } val response = client.get("/drafts") { header("X-User", "ada") } assertThat(response.status).isEqualTo(HttpStatusCode.Forbidden) assertThat(response.bodyAsText()).isEqualTo("Not Authorized: EnforcementPoint not enforced") } @Test fun `routes left open respond without authorization`() = testApplication { application { blogModule(users, ArticleStore(articles)) } assertThat(client.get("/health").status).isEqualTo(HttpStatusCode.OK) assertThat(client.get("/about").status).isEqualTo(HttpStatusCode.OK) } @Test fun `a websocket is authorized before the upgrade`() = testApplication { application { blogModule(users, ArticleStore(articles)) } val client = createClient { install(WebSockets) } client.webSocket("/feed", { header("X-User", "bob") }) { assertThat((incoming.receive() as Frame.Text).readText()).isEqualTo("Warden") } try { client.webSocket("/feed") {} fail("The handshake should fail without a user") } catch (_: Exception) {} } }

The refusal has a fixed body, Not Authorized: EnforcementPoint not enforced, which distinguishes it from a denial mapped by StatusPages.

14 November 2026