Skip to content

Commit

Permalink
Add tests for defaults
Browse files Browse the repository at this point in the history
Signed-off-by: averevki <[email protected]>
  • Loading branch information
averevki committed Jul 31, 2024
1 parent b387844 commit fd37511
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
Empty file.
36 changes: 36 additions & 0 deletions testsuite/tests/singlecluster/defaults/test_basic_authorization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Test basic enforcement of the rules inside the 'defaults' block of the AuthPolicy"""

import pytest

from testsuite.httpx.auth import HttpxOidcClientAuth

pytestmark = [pytest.mark.kuadrant_only]


@pytest.fixture(scope="module")
def authorization(authorization, oidc_provider):
"""Add oidc identity to defaults block of AuthPolicy"""
authorization.defaults.identity.add_oidc("default", oidc_provider.well_known["issuer"])
return authorization


@pytest.fixture(scope="module")
def auth(oidc_provider):
"""Returns Authentication object for HTTPX"""
return HttpxOidcClientAuth(oidc_provider.get_token, "authorization")


@pytest.fixture(scope="module")
def rate_limit():
"""No RateLimitPolicy is required for this test"""
return None


@pytest.mark.parametrize("authorization", ["route", "gateway"], indirect=True)
def test_basic_authorization(authorization, route, client, auth):
"""Test that default identity is applied successfully and shows affected status in the route"""
route.refresh()
assert route.is_affected_by(authorization)

assert client.get("/get").status_code == 401
assert client.get("/get", auth=auth).status_code == 200 # assert that AuthPolicy is enforced
33 changes: 33 additions & 0 deletions testsuite/tests/singlecluster/defaults/test_basic_rate_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Test basic enforcement of the rules inside the 'defaults' block of the RateLimitPolicy"""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit

pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]

LIMIT = Limit(3, 5)


@pytest.fixture(scope="module")
def authorization():
"""No authorization is required for this test"""
return None


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add basic requests limit to defaults block of RateLimitPolicy"""
rate_limit.defaults.add_limit("basic", [LIMIT])
return rate_limit


@pytest.mark.parametrize("rate_limit", ["route", "gateway"], indirect=True)
def test_basic_rate_limit(rate_limit, route, client):
"""Test that default rate limit is applied successfully and shows affected status in the route"""
route.refresh()
assert route.is_affected_by(rate_limit)

responses = client.get_many("/get", LIMIT.limit)
responses.assert_all(status_code=200)
assert client.get("/get").status_code == 429 # assert that RateLimitPolicy is enforced
35 changes: 35 additions & 0 deletions testsuite/tests/singlecluster/defaults/test_rules_exclusivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Test mutual exclusivity of defaults block and implicit defaults"""

import pytest
from openshift_client import OpenShiftPythonException

from testsuite.kuadrant.policy.authorization.auth_policy import AuthPolicy
from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit

pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def commit():
"""We need to try to commit objects during the actual test"""
return None


def test_rules_exclusivity_authorization(cluster, route, oidc_provider, module_label, blame):
"""Test that server will reject object with implicit and explicit defaults defined simultaneously in AuthPolicy"""
authorization = AuthPolicy.create_instance(cluster, blame("authz"), route, labels={"testRun": module_label})
authorization.defaults.identity.add_oidc("inside-defaults", oidc_provider.well_known["issuer"])
authorization.identity.add_oidc("outside-defaults", oidc_provider.well_known["issuer"])

with pytest.raises(OpenShiftPythonException, match="Implicit and explicit defaults are mutually exclusive"):
authorization.commit()


def test_rules_exclusivity_rate_limit(cluster, route, module_label, blame):
"""Test that server will reject object with implicit and explicit defaults simultaneously in RateLimitPolicy"""
rate_limit = RateLimitPolicy.create_instance(cluster, blame("limit"), route, labels={"testRun": module_label})
rate_limit.defaults.add_limit("inside-defaults", [Limit(2, 5)])
rate_limit.add_limit("outside-defaults", [Limit(2, 5)])

with pytest.raises(OpenShiftPythonException, match="Implicit and explicit defaults are mutually exclusive"):
rate_limit.commit()

0 comments on commit fd37511

Please sign in to comment.