Skip to content

Commit

Permalink
Added test for rate limit with autz
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmolar committed Jun 20, 2023
1 parent 6240ed9 commit 7fbd5ff
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions testsuite/tests/kuadrant/test_rate_limit_authz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Tests for authenticated rate limiting
http://kuadrant.io/docs/kuadrant-operator/user-guides/authenticated-rl-with-jwt-and-k8s-authnz.html
"""

import pytest

from testsuite.httpx.auth import HttpxOidcClientAuth
from testsuite.utils import fire_requests


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
rate_limit.add_limit(5, 60, variable="userID")
return rate_limit


@pytest.fixture(scope="module")
def authorization(authorization):
"""Adds JSON injection, that wraps the response as Envoy Dynamic Metadata for rate limit"""
authorization.responses.add(
{
"name": "auth-json",
"json": {
"properties": [{"name": "userID", "valueFrom": {"authJSON": "auth.identity.sub"}}],
},
"wrapper": "envoyDynamicMetadata",
"wrapperKey": "ext_auth_data",
},
)
return authorization


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


@pytest.fixture(scope="module")
def auth2(rhsso):
"""Creates new RHSSO user and returns its authentication object for HTTPX"""
user = rhsso.realm.create_user("user2", "password", email="[email protected]")
return HttpxOidcClientAuth.from_user(rhsso.get_token, user=user)


def test_multiple_iterations(client, auth, auth2):
"""Tests that simple limit is applied successfully and works for multiple iterations"""
assert client.get("/get", auth=auth).status_code == 200
assert client.get("/get", auth=auth).status_code == 200
assert client.get("/get", auth=auth).status_code == 429
assert client.get("/get", auth=auth2).status_code == 200

0 comments on commit 7fbd5ff

Please sign in to comment.