Polar Tests
In your policy (i.e. the collection of rules for your environment), Polar has a built-in test feature that lets you run unit tests of literal-value queries using a set of facts only visible for the lifetime of the test.
Feature | Description |
---|---|
test | Introduces a test block, which lets you run a set of unit tests |
setup | Declares facts to make accessible for the lifetime of your test |
assert | Succeeds if the expression evaluates to true |
assert_not | Succeeds if the expression evaluates to false |
iff | When used with a variable, causes an assert to succeed if and only if all values of the variable that satisfy the assertion exist in a list provided to the assertion |
allow | By default, Polar includes an alias to has_permission , which is a common shorthand rule |
test fixture | Declare a test fixture block comprising a set of facts that you can reuse across multiple tests |
fixture | Reference a test fixture inside a setup block to include its facts in the setup block |
Here's an annotated pseudo-example demonstrating the 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 data across multiple tests
You can reuse facts across multiple tests by defining a test fixture
block.
Here's an example:
Test multiple permissions in a single assertion
Oso Policy tests support variables in assertions. These allow you to test multiple permissions with a single assert
statement. We'll illustrate this with some examples.
assert
statement.Suppose a Repository
resource that defines 3 roles and 4 permissions.
In order to test this policy exhaustively, you need to test 12 total conditions (4 permissions for each of the 3 roles). In a typical test, that looks like this:
Instead of explicitly specifying each tested value, you can use variables to consolidate multiple assertions into a single statement. You express a variable in an assertion using the same name: Type
syntax that you use for variables elsewhere in Polar.
Using the iff
operator
When used with a variable, the iff
operator allows you to assert that a statement passes if and only if all of the values of variable that satisfy the statement exist in a list that you supply to the assertion.
For example, you can use the iff
operator to reduce the previous test from 12 assertions to 3
There are two changes:
- The specific actions in the original assertions have been replaced by the variable
action: String
- The values of
action
that are expected to satisfy the assertion are defined in a list associated with eachiff action in
statement
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 assert that a user has no permissions on a resource by passing an empty list to iff
.
You can use variables for any types in your policy, not just strings. For example, this assertion includes a variable user
to confirm that alice
and bob
have the archive
permission, but charlie
does not.
Using wildcards
You can use a wildcard to assert that any value of a variable satisfies a condition. For example, you may have a global admin role that should always be able to perform any action, whether that action exists today or not. You can use a wildcard to assert this in a single statement, instead of having to remember to add explicit assertions each time you add a new permission.
Here's a version of the above policy that grants the admin
role any permission:
You can use a wildcard variable to validate that the admin
role is granted any possible permission.
Note that the previous iff
assertions will now fail, because the new policy allows actions that don't exist in the list. For example, if you invoke the previous assertion for alice
:
The test will fail with the following message:
Testing deny logic
You can use assert_not
to test denial logic against a specific permission, or assert
and iff
to test denial of all permissions. For example, the following rule denies all permissions to users with the is_banned
fact:
You can test the denial logic like this:
Up next
- Patterns to understand common ways to model your applications.
Talk to an Oso engineer
If you want to learn more about Polar, schedule a 1x1 with an Oso engineer. We're happy to help.