Skip to content

Commit b0877d3

Browse files
authored
Merge pull request #121 from jsmolar/metadata
Move Mockserver fixture to higher conftest
2 parents 4552d17 + 7b241e4 commit b0877d3

File tree

8 files changed

+47
-49
lines changed

8 files changed

+47
-49
lines changed

testsuite/mockserver.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def create_expectation(self, expectation_id, path, opa_policy):
2727
}
2828
)
2929
response.raise_for_status()
30-
return response
30+
return self.url + path
3131

3232
def clear_expectation(self, expectation_id):
3333
"""Clears Expectation with specific ID"""

testsuite/tests/conftest.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
from urllib.parse import urlparse
55

66
import pytest
7+
from dynaconf import ValidationError
78
from keycloak import KeycloakAuthenticationError
89

10+
from testsuite.mockserver import Mockserver
911
from testsuite.oidc import OIDCProvider
1012
from testsuite.config import settings
1113
from testsuite.oidc.auth0 import Auth0Provider
@@ -82,6 +84,16 @@ def auth0(testconfig):
8284
return pytest.skip(f"Auth0 configuration item is missing: {exc}")
8385

8486

87+
@pytest.fixture(scope="module")
88+
def mockserver(testconfig):
89+
"""Returns mockserver"""
90+
try:
91+
testconfig.validators.validate(only=["mockserver"])
92+
return Mockserver(testconfig["mockserver"]["url"])
93+
except (KeyError, ValidationError) as exc:
94+
return pytest.skip(f"Mockserver configuration item is missing: {exc}")
95+
96+
8597
@pytest.fixture(scope="session")
8698
def oidc_provider(rhsso) -> OIDCProvider:
8799
"""Fixture which enables switching out OIDC providers for individual modules"""

testsuite/tests/kuadrant/authorino/authorization/opa/conftest.py

-25
This file was deleted.

testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Conftest for OPA policy located on external registry"""
2+
import pytest
3+
4+
from testsuite.utils import rego_allow_header
5+
6+
7+
@pytest.fixture(scope="module")
8+
def header():
9+
"""Header used by OPA policy"""
10+
return "opa", "opa-test"
11+
12+
13+
@pytest.fixture(scope="module")
14+
def opa_policy_expectation(request, mockserver, module_label, header):
15+
"""Creates Mockserver Expectation that returns Rego query and returns its endpoint"""
16+
request.addfinalizer(lambda: mockserver.clear_expectation(module_label))
17+
return mockserver.create_expectation(module_label, f"/{module_label}/opa", rego_allow_header(*header))
18+
19+
20+
@pytest.fixture(scope="module")
21+
def authorization(authorization, opa_policy_expectation):
22+
"""
23+
Adds OPA policy. Rego query is located on external registry (Mockserver).
24+
Policy accepts requests that contain `header`.
25+
"""
26+
authorization.add_external_opa_policy("opa", opa_policy_expectation, 1)
27+
return authorization

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,11 @@ def updated_header():
1818
@pytest.fixture(scope="module", autouse=True)
1919
def update_external_opa(mockserver, module_label, updated_header):
2020
"""Updates Expectation with updated header"""
21-
mockserver.create_expectation(module_label, "/opa", rego_allow_header(*updated_header))
21+
mockserver.create_expectation(module_label, f"/{module_label}/opa", rego_allow_header(*updated_header))
2222
# Sleeps for 1 second to compensate auto-refresh cycle `authorization.opa.externalRegistry.ttl = 1`
2323
time.sleep(1)
2424

2525

26-
@pytest.fixture(scope="module")
27-
def authorization(authorization, mockserver):
28-
"""
29-
Adds OPA policy. Rego query is located on external registry (Mockserver).
30-
Policy accepts requests that contain `header`.
31-
"""
32-
authorization.add_external_opa_policy("opa", mockserver.url + "/opa", 1)
33-
return authorization
34-
35-
3626
def test_auto_refresh(client, auth, updated_header):
3727
"""Tests auto-refresh of OPA policy from external registry."""
3828
key, value = updated_header

testsuite/tests/kuadrant/authorino/authorization/opa/test_external_registry.py testsuite/tests/kuadrant/authorino/authorization/opa/external_registry/test_external_registry.py

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
"""Tests for Open Policy Agent (OPA) using Mockserver Expectations as http endpoint with Rego query"""
22

3-
import pytest
4-
5-
6-
@pytest.fixture(scope="module")
7-
def authorization(mockserver, authorization):
8-
"""
9-
Adds OPA policy. Rego query is located on external registry (Mockserver).
10-
Policy accepts requests that contain `header`.
11-
"""
12-
authorization.add_external_opa_policy("opa", mockserver.url + "/opa")
13-
return authorization
14-
153

164
def test_allowed_by_opa(client, auth, header):
175
"""Tests a request that should be authorized by OPA external registry declaration"""

testsuite/tests/kuadrant/authorino/authorization/opa/test_inline_rego.py

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
from testsuite.utils import rego_allow_header
55

66

7+
@pytest.fixture(scope="module")
8+
def header():
9+
"""Header used by OPA policy"""
10+
return "opa", "opa-test"
11+
12+
713
@pytest.fixture(scope="module")
814
def authorization(authorization, header):
915
"""Adds OPA policy that accepts all requests that contain `header`"""

0 commit comments

Comments
 (0)