Testing Policy#
Policy is code, and it deserves tests. The behaviors worth covering are the ones the scenario demonstrates: a route allows the right callers, denies the wrong ones, redacts the right fields, and carries taint across a session. Because APL is declarative and evaluated by the runtime, you can test a route by loading a policy and driving operations through it, asserting the outcome, without standing up a live backend.
What to test#
For each route, cover the outcomes its policy produces:
- Allow: a caller with the required attributes passes and the operation forwards.
- Deny: a caller missing a required attribute is rejected, with the expected reason code.
- Redaction: a field is present for an entitled caller and redacted for an unentitled one (the “same request, different data” outcomes).
- Information flow: a session that acquired a taint label is blocked on a later operation that gates on it.
- Delegation: a passing caller mints a token with the requested scope, and a post-check denies when the granted scope is short.
A table-driven policy test#
Load a policy into an engine, drive routes through it with a fake backend, and assert the outcome. The tutorial ships this as a working template you can copy: examples/tutorial/tests/policy_tests.rs. The setup is a small helper:
async fn engine_with(policy: &str) -> Arc<PolicyEngine> {
let engine = Arc::new(PolicyEngine::default());
cpex::install_builtins(&engine);
cpex_tutorial::install_http_transport(&engine); // reaches the local IdP; not for production
cpex_tutorial::audit::register(&engine); // the tutorial's own audit plugin
engine.load_config_yaml(policy).expect("policy should load");
engine.initialize().await.expect("initialize");
engine
}A policy that fails to load fails the test at load_config_yaml, with the same message a deployment would log: an unknown key, a removed hook name, or a plugin no policy reaches. That makes a “every policy loads” test cheap and worth having (the tutorial’s every_module_policy_loads).
Then a table keeps the allow/deny matrix readable, one row per case:
#[tokio::test]
async fn external_email_denied_with_custom_code() {
let engine = engine_with(POLICY).await;
let outcome = mediate(
&engine,
&Caller::anonymous(),
"send_email",
json!({ "to": "x@evil.example", "external": true }),
|args| backends::dispatch("send_email", args),
)
.await;
assert!(matches!(
outcome,
Outcome::Denied { code, .. } if code == "email.external_blocked"
));
}mediate() here is the tutorial’s harness wrapper around the host dispatch loop, not a CPEX API; in your own host you would drive the same route through your own loop and assert on the result. Anonymous callers are enough to exercise structural rules (authentication gates, argument guards, result pipelines) with no IdP. For identity-dependent rules, mint a token the way the tutorial’s idp helper does.
A stateful taint test follows the same shape but shares one session id across two calls: read a sensitive route, then assert a later send_email on the same session is denied on the taint label. Tutorial module 7 is the worked example; module 10 walks through the test file above.
Scenario checks#
Beyond unit tests, each tutorial module binary supports a --check flag that runs its scripted scenario and exits non-zero if the outcome drifts. make tutorial-check boots the tutorial IdP, runs every module’s check, and tears it down. This is a lightweight way to pin end-to-end behavior (including the identity- and delegation-backed paths) in CI.
Integration coverage#
Unit-evaluating a route proves the policy logic. It does not prove the plugins it dispatches behave correctly end to end. For effects that call out (a PDP resolver, a delegator, a host plugin), add an integration test that exercises the real plugin through the engine, so the interaction is covered and not just the policy’s intent. Test the failure paths too: a PDP that denies, a token exchange that returns a short scope, an IdP that is unreachable, a JWKS endpoint the transport refuses.
To test those failure paths without a server, a Rust test can install a scripted transport instead of a real one. cpex::praxis_policy_core::http_testing::FakeTransport (enable test-util on a praxis-policy-core = "=0.5.0" dev-dependency; PPE 0.5.0 does not forward this test feature through its facade) scripts responses, timeouts, connect failures and key rotation between two fetches, without sleeping.
A test that talks to a mock IdP on localhost needs a transport that allows private destinations (HyperTransport::new().with_allow_private_destinations() in Rust, allow_private_destinations=True in Python, cpex.WithPrivateDestinations() in Go). Keep that setting in test and demo hosts only. Those are the branches policy exists to handle.
Running#
cargo test -p cpex-tutorial # the policy tests above
cargo test --workspace # everything: facade, FFI, Python host, tutorialCopy examples/tutorial/tests/policy_tests.rs as the starting point for tests against your own policy.