Skip to main content
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.

Test Features

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 a test 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:
In order to test this policy exhaustively, you need to test twelve total conditions (four permissions for each of the three roles). In a typical test, that looks like this:
Instead of writing a separate assert for each value, you can use variables to test multiple values in a single statement. In an assertion, declare a variable using the same 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.
This makes it possible to replace multiple explicit assertions with a single variable-based assertion. For example, the previous test can be reduced from twelve assertions to three:
What changed from explicit assertions:
  1. Specific actions were replaced with a variable: action: String.
  2. The allowed actions are listed in the iff action in [...] expression.
This lets you write a single assertion that confirms:
  • all actions in the list are allowed and
  • all actions that aren’t in the list are denied
You can also use iff to confirm that no permissions are granted by passing an empty list:
Variables aren’t limited to strings; you can use any type in your policy. For example, check that only 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 the admin role every possible permission:
You can test that admin has all possible actions with a single wildcard variable:
This will pass if 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:
The test will fail with the following message:

Testing Deny Logic

You can test denial rules either by:
  • Using assert_not to check a specific permission is denied.
  • Using assert with iff to confirm that all permissions are denied.
    For example, the following rule denies all permissions to users with the is_banned fact:
Test cases might look like this: