Skip to content

Commit

Permalink
Merge pull request #121 from jsmolar/metadata
Browse files Browse the repository at this point in the history
Move Mockserver fixture to higher conftest
  • Loading branch information
pehala authored Sep 29, 2022
2 parents 4552d17 + 7b241e4 commit b0877d3
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 49 deletions.
2 changes: 1 addition & 1 deletion testsuite/mockserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_expectation(self, expectation_id, path, opa_policy):
}
)
response.raise_for_status()
return response
return self.url + path

def clear_expectation(self, expectation_id):
"""Clears Expectation with specific ID"""
Expand Down
12 changes: 12 additions & 0 deletions testsuite/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from urllib.parse import urlparse

import pytest
from dynaconf import ValidationError
from keycloak import KeycloakAuthenticationError

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


@pytest.fixture(scope="module")
def mockserver(testconfig):
"""Returns mockserver"""
try:
testconfig.validators.validate(only=["mockserver"])
return Mockserver(testconfig["mockserver"]["url"])
except (KeyError, ValidationError) as exc:
return pytest.skip(f"Mockserver configuration item is missing: {exc}")


@pytest.fixture(scope="session")
def oidc_provider(rhsso) -> OIDCProvider:
"""Fixture which enables switching out OIDC providers for individual modules"""
Expand Down
25 changes: 0 additions & 25 deletions testsuite/tests/kuadrant/authorino/authorization/opa/conftest.py

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Conftest for OPA policy located on external registry"""
import pytest

from testsuite.utils import rego_allow_header


@pytest.fixture(scope="module")
def header():
"""Header used by OPA policy"""
return "opa", "opa-test"


@pytest.fixture(scope="module")
def opa_policy_expectation(request, mockserver, module_label, header):
"""Creates Mockserver Expectation that returns Rego query and returns its endpoint"""
request.addfinalizer(lambda: mockserver.clear_expectation(module_label))
return mockserver.create_expectation(module_label, f"/{module_label}/opa", rego_allow_header(*header))


@pytest.fixture(scope="module")
def authorization(authorization, opa_policy_expectation):
"""
Adds OPA policy. Rego query is located on external registry (Mockserver).
Policy accepts requests that contain `header`.
"""
authorization.add_external_opa_policy("opa", opa_policy_expectation, 1)
return authorization
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@ def updated_header():
@pytest.fixture(scope="module", autouse=True)
def update_external_opa(mockserver, module_label, updated_header):
"""Updates Expectation with updated header"""
mockserver.create_expectation(module_label, "/opa", rego_allow_header(*updated_header))
mockserver.create_expectation(module_label, f"/{module_label}/opa", rego_allow_header(*updated_header))
# Sleeps for 1 second to compensate auto-refresh cycle `authorization.opa.externalRegistry.ttl = 1`
time.sleep(1)


@pytest.fixture(scope="module")
def authorization(authorization, mockserver):
"""
Adds OPA policy. Rego query is located on external registry (Mockserver).
Policy accepts requests that contain `header`.
"""
authorization.add_external_opa_policy("opa", mockserver.url + "/opa", 1)
return authorization


def test_auto_refresh(client, auth, updated_header):
"""Tests auto-refresh of OPA policy from external registry."""
key, value = updated_header
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
"""Tests for Open Policy Agent (OPA) using Mockserver Expectations as http endpoint with Rego query"""

import pytest


@pytest.fixture(scope="module")
def authorization(mockserver, authorization):
"""
Adds OPA policy. Rego query is located on external registry (Mockserver).
Policy accepts requests that contain `header`.
"""
authorization.add_external_opa_policy("opa", mockserver.url + "/opa")
return authorization


def test_allowed_by_opa(client, auth, header):
"""Tests a request that should be authorized by OPA external registry declaration"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
from testsuite.utils import rego_allow_header


@pytest.fixture(scope="module")
def header():
"""Header used by OPA policy"""
return "opa", "opa-test"


@pytest.fixture(scope="module")
def authorization(authorization, header):
"""Adds OPA policy that accepts all requests that contain `header`"""
Expand Down

0 comments on commit b0877d3

Please sign in to comment.