-
Notifications
You must be signed in to change notification settings - Fork 26
test: Add tests for named HTTPRouteRule sectionName targeting #943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tiago-Vier-Preto
wants to merge
4
commits into
Kuadrant:main
Choose a base branch
from
Tiago-Vier-Preto:feature/add-tests-for-named-httprouterule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9098d56
feat: Add tests for named HTTPRouteRule sectionName targeting
Tiago-Vier-Preto d102af9
fix format issues and add global variable
Tiago-Vier-Preto 7b0c23b
Update testsuite/gateway/gateway_api/route.py
Tiago-Vier-Preto 3bda78a
fixed lint errors
Tiago-Vier-Preto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
testsuite/tests/singlecluster/gateway/authpolicy/test_authpolicy_named_http_route_rule.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| Tests that an AuthPolicy is correctly applied to a specific named rule section of | ||
| an HTTPRoute, protecting only the traffic handled by that named rule. | ||
| """ | ||
|
|
||
| import pytest | ||
| from testsuite.kuadrant.policy.authorization.auth_policy import AuthPolicy | ||
|
|
||
| pytestmark = [pytest.mark.authorino, pytest.mark.kuadrant_only] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def route(route, backend): | ||
| """ | ||
| Overrides the default route to have two paths: one for a protected | ||
| service (/get) and one for a public one (/anything). | ||
| Rules are explicitly named. | ||
| """ | ||
| route.remove_all_rules() | ||
| route.add_backend(backend, "/get", name="get-rule") # This becomes the target "get-rule" | ||
| route.add_backend(backend, "/anything", name="anything-rule") # This is the public rule | ||
| return route | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def authorization(cluster, blame, module_label, oidc_provider, route): | ||
| """ | ||
| Creates an AuthPolicy that targets a specific named rule ('get-rule') within the | ||
| HTTPRoute. | ||
| """ | ||
| policy = AuthPolicy.create_instance( | ||
| cluster, | ||
| blame("authz"), | ||
| route, # Target is the HTTPRoute | ||
| section_name="get-rule", # Target the specific named rule | ||
| labels={"testRun": module_label}, | ||
| ) | ||
| policy.identity.add_oidc("basic", oidc_provider.well_known["issuer"]) | ||
| return policy | ||
|
|
||
|
|
||
| def test_authpolicy_section_name_targeting_named_http_route_rule(client, auth): | ||
| """ | ||
| Tests that an AuthPolicy attached to a specific explicitly named HTTPRoute rule protects | ||
| only the requests handled by that rule. | ||
| """ | ||
| # The '/anything' path is handled by a different, untargeted rule and should be public. | ||
| response = client.get("/anything") | ||
| assert response.status_code == 200 | ||
|
|
||
| # The '/get' path is handled by the targeted 'get-rule' and should require authentication. | ||
| response = client.get("/get") | ||
| assert response.status_code == 401 | ||
|
|
||
| # The '/get' path with a valid token should be allowed. | ||
| response = client.get("/get", auth=auth) | ||
| assert response.status_code == 200 |
36 changes: 36 additions & 0 deletions
36
testsuite/tests/singlecluster/limitador/section/test_named_route_rule.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """Tests that the RLP is correctly applied to the specific named route rule""" | ||
|
|
||
| import pytest | ||
|
|
||
| from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy | ||
|
|
||
| pytestmark = [pytest.mark.limitador] | ||
|
|
||
|
averevki marked this conversation as resolved.
|
||
| LIMIT = Limit(2, "10s") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def route(route, backend): | ||
| """Add two named backend rules for different paths to the route""" | ||
| route.remove_all_rules() | ||
| route.add_backend(backend, "/get", name="get-rule") | ||
| route.add_backend(backend, "/anything", name="anything-rule") | ||
| return route | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def rate_limit(cluster, blame, module_label, route): | ||
| """Add a RateLimitPolicy targeting the get-rule HTTPRoute Rule.""" | ||
| rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "get-rule", labels={"testRun": module_label}) | ||
| rlp.add_limit("basic", [LIMIT]) | ||
| return rlp | ||
|
|
||
|
|
||
| def test_limit_match_named_route_rule(client): | ||
| """Tests that RLP correctly applies to the specific named HTTPRoute Rule""" | ||
| responses = client.get_many("/get", LIMIT.limit) | ||
| responses.assert_all(status_code=200) | ||
| assert client.get("/get").status_code == 429 | ||
|
|
||
| response = client.get("/anything") | ||
| assert response.status_code == 200 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.