Skip to content

Commit

Permalink
Add test for gateway attached to AuthPolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
averevki committed Nov 27, 2023
1 parent 024bbb1 commit 48a22ff
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 8 deletions.
6 changes: 3 additions & 3 deletions testsuite/openshift/objects/auth_config/auth_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ def create_instance( # type: ignore
cls,
openshift: OpenShiftClient,
name,
route: Referencable,
targetRef: Referencable,
labels: Dict[str, str] = None,
):
): # pylint: disable=invalid-name
"""Creates base instance"""
model: Dict = {
"apiVersion": "kuadrant.io/v1beta2",
"kind": "AuthPolicy",
"metadata": {"name": name, "namespace": openshift.project, "labels": labels},
"spec": {
"targetRef": route.reference,
"targetRef": targetRef.reference,
},
}

Expand Down
9 changes: 8 additions & 1 deletion testsuite/openshift/objects/gateway_api/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ def remove_all_hostnames(self):
self.model.spec.hostnames = []

@modify
def set_match(self, backend: "Httpbin", path_prefix: str = None):
def set_path_match(self, path_prefix: str):
"""TODO"""
self.model.spec.rules.append(
{"matches": [{"path": {"value": path_prefix, "type": "PathPrefix"}}]}
)

@modify
def set_backend_match(self, backend: "Httpbin", path_prefix: str = None):
"""Limits HTTPRoute to a certain path"""
match = {}
if path_prefix:
Expand Down
4 changes: 1 addition & 3 deletions testsuite/tests/kuadrant/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def authorization_name(blame):
def authorization(authorino, kuadrant, oidc_provider, route, authorization_name, openshift, module_label):
"""Authorization object (In case of Kuadrant AuthPolicy)"""
if kuadrant:
policy = AuthPolicy.create_instance(openshift, authorization_name, route, labels={"testRun": module_label})
policy.identity.add_oidc("rhsso", oidc_provider.well_known["issuer"])
return policy
return AuthPolicy.create_instance(openshift, authorization_name, route, labels={"testRun": module_label})
return None


Expand Down
Empty file.
15 changes: 15 additions & 0 deletions testsuite/tests/kuadrant/gateway/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""TODO"""
import pytest


@pytest.fixture(scope="module", autouse=True)
def gateway_wait_for_ready(gateway):
"""Waits for gateway to be ready"""
gateway.wait_for_ready()


@pytest.fixture(scope="module", autouse=True)
def commit(request, authorization):
"""TODO"""
request.addfinalizer(authorization.delete)
authorization.commit()
63 changes: 63 additions & 0 deletions testsuite/tests/kuadrant/gateway/test_authpolicy_to_gateway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""TODO"""
import pytest

from testsuite.openshift.objects.auth_config.auth_policy import AuthPolicy
from testsuite.openshift.objects.gateway_api.route import HTTPRoute


@pytest.fixture(scope="module")
def gateway_httproute(request, gateway, wildcard_domain, module_label, blame):
"""TODO"""
route = HTTPRoute.create_instance(gateway.openshift, blame("gw-route"), gateway, {"app": module_label})
route.add_hostname(wildcard_domain)
route.set_path_match("/")

request.addfinalizer(route.delete)
route.commit()
return route


@pytest.fixture(scope="module")
def gateway_authorization(request, gateway, authorization_name, openshift, module_label):
"""TODO"""
auth_policy = AuthPolicy.create_instance(
openshift, f"gw-{authorization_name}", gateway, labels={"testRun": module_label}
)
auth_policy.authorization.add_opa_policy("deny-all", "allow { false }")
request.addfinalizer(auth_policy.delete)
return auth_policy


@pytest.fixture(scope="module")
def nonexistent_hostname_client(gateway, exposer, blame):
"""TODO"""
hostname = exposer.expose_hostname(blame("nonexistent-hostname"), gateway)
client = hostname.client()
yield client
client.close()


@pytest.fixture(scope="module")
def sub_nonexistent_hostname_client(gateway, exposer, blame):
"""TODO"""
hostname = exposer.expose_hostname(blame("sub.nonexistent-hostname"), gateway)
client = hostname.client()
yield client
client.close()


def test_authpolicy_attached_to_gateway(client, gateway_httproute, gateway_authorization, nonexistent_hostname_client, sub_nonexistent_hostname_client):
"""TODO"""
response = client.get("/get")
assert response.status_code == 200

response = nonexistent_hostname_client.get("/get")
assert response.status_code == 500

gateway_authorization.commit()

response = nonexistent_hostname_client.get("/get")
assert response.status_code == 403

response = sub_nonexistent_hostname_client.get("/get")
assert response.status_code == 403
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_matches(client, backend, route, resilient_request):
response = client.get("/get")
assert response.status_code == 200

route.set_match(backend, path_prefix="/anything")
route.set_backend_match(backend, path_prefix="/anything")

response = resilient_request("/get", expected_status=404)
assert response.status_code == 404, "Matches were not reconciled"
Expand Down

0 comments on commit 48a22ff

Please sign in to comment.