|
| 1 | +""" |
| 2 | +Tests for Open Policy Agent (OPA) policy pulled from external registry. |
| 3 | +Registry is represented by Mockserver Expectation that returns Rego query. |
| 4 | +""" |
| 5 | + |
| 6 | +from time import sleep |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from testsuite.utils import rego_allow_header |
| 11 | + |
| 12 | + |
| 13 | +pytestmark = [pytest.mark.authorino] |
| 14 | + |
| 15 | + |
| 16 | +KEY = "test-key" |
| 17 | +VALUE = "test-value" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="function", autouse=True) |
| 21 | +def reset_expectation(mockserver, module_label): |
| 22 | + """Updates Expectation with updated header""" |
| 23 | + mockserver.create_response_expectation(module_label, rego_allow_header(KEY, VALUE)) |
| 24 | + sleep(2) # waits for cache to reset because of ttl=1 |
| 25 | + |
| 26 | + |
| 27 | +def test_caching(client, auth, mockserver, blame, module_label): |
| 28 | + """Tests that external policy is cached""" |
| 29 | + response = client.get("/get", auth=auth, headers={KEY: VALUE}) |
| 30 | + assert response.status_code == 200 |
| 31 | + |
| 32 | + mockserver.create_response_expectation(module_label, rego_allow_header(blame(KEY), blame(VALUE))) |
| 33 | + |
| 34 | + response = client.get("/get", auth=auth, headers={KEY: VALUE}) |
| 35 | + assert response.status_code == 200 |
| 36 | + |
| 37 | + |
| 38 | +def test_cache_refresh(client, auth, mockserver, blame, module_label): |
| 39 | + """Tests that policy is pull again from external registry after ttl expiration""" |
| 40 | + response = client.get("/get", auth=auth, headers={KEY: VALUE}) |
| 41 | + assert response.status_code == 200 |
| 42 | + |
| 43 | + mockserver.create_response_expectation(module_label, rego_allow_header(blame(KEY), blame(VALUE))) |
| 44 | + sleep(2) |
| 45 | + |
| 46 | + response = client.get("/get", auth=auth, headers={KEY: VALUE}) |
| 47 | + assert response.status_code == 403 |
0 commit comments