Skip to content

Commit 938aed4

Browse files
author
Jakub Smolar
authored
Merge pull request #588 from jsmolar/jsmolar3
Fix OPA tests
2 parents be42494 + f955995 commit 938aed4

File tree

4 files changed

+49
-43
lines changed

4 files changed

+49
-43
lines changed

testsuite/tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def cfssl(testconfig, skip_or_fail):
166166
return client
167167

168168

169-
@pytest.fixture(scope="module")
169+
@pytest.fixture(scope="session")
170170
def mockserver(testconfig, skip_or_fail):
171171
"""Returns mockserver"""
172172
try:

testsuite/tests/singlecluster/authorino/authorization/opa/external_registry/test_auto_refresh_policy.py

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

testsuite/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def cert_builder(
9292

9393
def rego_allow_header(key, value):
9494
"""Rego query that allows all requests that contain specific header with`key` and `value`"""
95-
return f'allow {{ input.context.request.http.headers.{key} == "{value}" }}'
95+
return f'allow {{ input.context.request.http.headers["{key}"] == "{value}" }}'
9696

9797

9898
def add_port(url_str: str, return_netloc=True) -> Union[ParseResult, str]:

0 commit comments

Comments
 (0)