In a Polar policy, you can run built-in unit tests that evaluate literal-value queries against a set of temporary facts visible only during the test.Documentation Index
Fetch the complete documentation index at: https://www.osohq.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
Test Features
| Feature | Description |
|---|---|
test | Defines a test block containing unit tests. |
setup | Declares facts available for the lifetime of the test. |
assert | Passes if the expression evaluates to true. |
assert_not | Passes if the expression evaluates to false. |
iff | When used with a variable, passes if and only if all variable values that satisfy the expression are in a provided list. |
allow | Alias for has_permission (common shorthand). |
test fixture | Defines a reusable set of facts for multiple tests. |
fixture | Includes a test fixture’s facts in a setup block. |
Test Syntax
Simple Example
Here’s a simple example policy including tests, relying only on primitive types:Complex Example
Here is a more complex example that leverages Polar’s abstract types and more closely mirrors how we expect you to use Oso:Reuse Facts Across Tests
You can reuse facts across multiple tests by defining atest fixture block:
Variables in Assertions
Variables let you test multiple permissions in a single assertion.
Info: You can have at most one variable in any assert statement.
Suppose a Repository resource that defines three roles and four permissions:
name: Type syntax used for variables in the rest of Polar.
Using the iff Operator
The iff operator lets you assert that a statement holds if and only if all matching values for a variable are exactly the ones you list in the test.
When combined with a variable, iff checks two things in one assertion:
- Every listed value passes the assertion.
- No value outside the list passes the assertion.
- Specific actions were replaced with a variable:
action: String. - The allowed actions are listed in the
iff action in [...]expression.
- all actions in the list are allowed and
- all actions that aren’t in the list are denied
iff to confirm that no permissions are granted by passing an empty list:
alice and bob have the archive permission:
Using Wildcards
A wildcard variable asserts that any value of that variable satisfies the condition—useful for roles or permissions that should always be allowed, even as new permissions are added over time. For example, suppose your policy grants theadmin role every possible permission:
admin has all possible actions with a single wildcard variable:
alice can perform any action of type String on "cool-repo", whether that action exists now or is added later.
Important: Wildcard assertions will fail any previous iff tests for the same role, because iff checks that only the listed actions are allowed.
For example, if you previously had:
Testing Deny Logic
You can test denial rules either by:- Using
assert_notto check a specific permission is denied. - Using
assertwithiffto confirm that all permissions are denied.
For example, the following rule denies all permissions to users with theis_bannedfact: