This tutorial assumes that you arere familiar with the basics of Polar, facts,
and policy tests.If you are not, check out the Introduction to
Polar and Testing with
Polar pages.
- Make a change to your policy
- Validate the policy syntax
- Add policy tests
- Run the policy tests against the Oso Dev Server
- Push the updated policy to the Oso Dev Server
- Test application code against the updated policy
Initial policy
To start with this workflow, you can use the policy below.policy.polar
User
, and a single resource,
Organization
.
The policy states that a User
has the view
permission on an Organization
if they have the member
role on that Organization
.Open the policy in an
editor. With VS Code, it will look like this:


Make a change to the policy
An Organization contains one or more Projects. Add a Project resource to the policy so you can model its authorization logic.policy.polar
Project
resource. It states that a User
can
inherit the member
role on a Project
from its parent Organization
. Next,
you will validate the policy syntax.
Validate the policy syntax
From VS Code
Syntax errors are indicated in the VSCode editor. For example, if you omit a trailing semi-colon, you’ll see something like this:
relations
on line 13 is
underlined in red. If you hover over relations
, you can see a description of the issue.


Using oso-cloud validate
You can also use the oso-cloud validate
CLI command to validate the syntax from the command line.
oso-cloud validate
passes.
Add policy tests
Next, you will add tests to confirm that the authorization logic for Projects is correct. The initial policy has a test that validates the Organization role logic.Project
resource is added, you can add tests to confirm the Project
authorization logic. You can create new tests or extend the existing one. For simplicity, we’ll extend the existing test.
Run the policy tests against the Oso Dev Server
Next, you will run the policy tests against the Oso Development Server running on your local machine.Start the Oso Dev Server
First, make sure the Oso Dev Server is running. The filename isstandalone
and you can start it from wherever you saved it.
oso-cloud
CLI by setting the OSO_URL
and OSO_AUTH
environment variables as follows:
oso-cloud
command.
Run the policy tests
Run the policy tests against the Oso Dev Server by issuing theoso-cloud test
command:
If your policy is split into multiple
.polar
files, you must pass all of
them to oso-cloud test
.Push the updated policy to the Oso Dev Server
Push the updated policy to the local binary with theoso-cloud policy
command:
If your policy is split into multiple
.polar
files, you must push each
.polar
file to the Oso Dev Server on any update.Verify the policy (optional)
You can verify that the correct policy is loaded by issuing theoso-cloud policy
command without an argument.
You can also start the Oso Dev Server with a list of policy files and the
--watch-for-changes
flag to instruct it to automatically reload your policy
files and rerun tests any time they change. See the
docs for more details.Test application code against the Oso Dev Server
Local integration tests and functional tests should run against the local Oso Dev Server after you’ve updated the policy.Clear old fact data
To ensure that the Oso Dev Server is in a known state before running local integration tests, you can delete all existing facts by either:- Issuing the
oso-cloud clear
command from the command line - Invoking the clear_data API endpoint at the Oso Dev Server
Make sure that you run these commands against your local Oso Dev Server
instance and not the live Oso Cloud environment.
Re-seed initial state
After deleting old facts from the Oso Dev Server, you can re-seed it with any initial facts that are not recreated by your integration tests. For example, if you have arole
table that contains pre-seeded roles, you should add facts
for those roles to the Oso Dev Server before starting your tests. If your tests
add new roles, you should not pre-seed the Oso Dev Server with facts for those roles.
Instead, you should let your application code do that as part of the tests.
Your integration tests should cover all of your code paths, including those
that add facts to Oso Cloud. If you preseed facts that are added by your
tests, the tests will not identify bugs in code adding those facts.However, if the testing database has an initial state, you should add facts to
the Oso Dev Server to mirror the database’s initial state.
Otherwise tests that rely on a consistent initial state will fail
oso-cloud tell
commands to the Oso Dev Server for all facts that need to be present before the integration test suite runs.
Initialize the SDK against the Oso Dev Server
To use the Oso Dev Server from your code, you instantiate the client in your application with the dev server’s URL. Refer to the your SDK’s documentation for details. For example, you’d initialize the Node.js client like this:Conclusion
With the Oso Cloud developer tools, you can build a local development workflow to iterate quickly on authorization code. That workflow follows these steps.- Make a change to your policy
- Validate the policy syntax
- Add policy tests
- Run the policy tests against the Oso Dev Server
- Push the updated policy to the Oso Dev Server
- Test application code against the updated policy