# Test a policy safely

> There's no report-only mode, so try a new rule in a policy set nothing enforces, ask the realm what it would decide, then move it in.

A policy decides real calls the moment it's active in a set an enforcement point reads, and one deny refuses
everything it covers. The realm has no report-only mode and no sign-in simulation. What it has is enough to test
safely: a **test set** that no enforcement point reads, and the realm's **decision API**, which answers what a set
would decide for any caller you describe. This guide writes a rule in a test set, checks the answers, then moves it
into the real set.

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). Your session can ask the decision API.
- **Where:** today this is done through the API, with your session in `$CVYUH_SESSION`
  ([Call the API](/develop/api/)).
- **What you need:** the rule you want, such as one from [Common access policies](../common-access-policies/), and
  the claims of a token for each kind of caller you want to try.

<Steps>

1. **Make a test set** for the same resource type, with the same settings as the real one. Read `default` first and
   copy its `comparator`, `subjectTypes` and `conditionTypes`:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/policySet?id=default-test" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "comparator": "prefix",
           "conditionTypes": ["OAuth2Scope", "OAuth2AuthorizationDetails", "IPv4", "IPv6", "SimpleTime",
                              "AuthenticateToService", "AND", "OR", "NOT"] }'

   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policySet/default-test/relationships/resourceTypes \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "url" }'
   ```

   No enforcement point reads `default-test`, so nothing in it decides a real call.

2. **Write the rule in the test set,** active, and link it to `default-test` and `url`, as on
   [Policies](../../../authorization/policies/#set-it-up). Add copies of the real set's policies that the rule works
   beside, so the test answers as the real set will.

3. **Ask what it decides,** once for each kind of caller: one it should allow, one it should refuse, and one at the
   edge. Name the test set in `context.policySet`:

   ```sh
   curl https://foobar.cvyuh.com/access/v1/evaluation \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "subject": { "type": "user", "id": "<maya id>",
                        "properties": { "claims": { "sub": "<maya id>", "acr": "urn:example:acr:mfa" } } },
           "resource": { "type": "url", "id": "https://admin.example.com:443/users" },
           "action": { "name": "DELETE" },
           "context": { "policySet": "default-test", "ip": "198.51.100.4" } }'
   ```

   ```json
   { "decision": false, "context": { "reason": "policy_denied", "decidingPolicy": "office-network-only" } }
   ```

   Leave `groups` out and the realm reads the user's groups itself. To see who a rule would let in, ask
   `POST https://foobar.cvyuh.com/access/v1/search/subject` with the same body and `"subject": { "type": "user" }`
   ([Ask the realm](/gateway/decisions/ask-the-realm/#search)).

4. **Move it into the real set** once every answer is what you meant. Take the link to the test set away, and link
   the policy to `default`:

   ```sh
   curl -X DELETE https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/office-network-only/relationships/policySet/default-test \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"

   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/office-network-only/relationships/policySet \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "default" }'
   ```

   To change the moment it starts deciding, write it in `default` with `"active": false` instead, and set
   `"active": true` when you're ready.

</Steps>

<Aside type="tip" title="Testing an issuance policy">
  Do the same before naming a set in `issuancePolicySet`: ask with `"resource": { "type": "client", "id":
  "client://finance-app" }`, `"action": { "name": "issue" }`, the subject's `entitlements` and `risk_class` in its
  claims, and `"context": { "policySet": "issuance" }`. Until the set is named, nothing asks it.
</Aside>

## Check it worked

- **In the real set,** ask the same questions with `"policySet": "default"`: the answers match the test set's.
- **At the gateway,** a refused call answers `403` with the same reason.
- **On the audit trail,** each deny, and each decision about an agent, is recorded as `POLICY_DECIDED`
  (**Audit → Authentication**). Watch it for the first hour for refusals you didn't expect. To keep every allow too
  while you watch, set `authorizationSettings.auditDecisions` to `all`, and back to `agents` after
  ([What is recorded](/audit-reports/audit-trail/what-is-recorded/#policy-decisions)).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `404` from the decision API | The set or resource type named doesn't exist | Check `context.policySet` and `resource.type` |
| Every answer is `no_matching_policy` | The policy isn't linked to the test set and the type, or it's inactive | Link both, and set `active: true` |
| The answer is `policies_excluded` | A policy in the set breaks the set's rules | Fix it, or widen the set's `conditionTypes` |
| The test set allows, the real set refuses | The real set holds a deny the test set doesn't | Copy the real set's denies into the test set and ask again |
| The answer is `subject_not_active` | The user or agent you named isn't active | Name an active one |

## Undo it

Delete the test set's policies, then the test set. In the real set, set a policy's `active` to `false` to take it out
of every decision at once.

## Related

- [Common access policies](../common-access-policies/): rules to test.
- [Policies](../../../authorization/policies/): how a decision is reached, and every reason.
- [Ask the realm](/gateway/decisions/ask-the-realm/): the decision API in full.
