From 2b51986a3134b9d710d64f6bbb7431b257c49a51 Mon Sep 17 00:00:00 2001 From: Jakub Smolar Date: Thu, 8 Feb 2024 15:53:01 +0100 Subject: [PATCH] Proposal: new many request assert --- testsuite/httpx/__init__.py | 16 ++++++++++++++-- .../tests/kuadrant/limitador/test_basic_limit.py | 4 +--- .../limitador/test_multiple_iterations.py | 4 +--- .../tests/kuadrant/limitador/test_route_rule.py | 12 +++++++++--- .../tests/kuadrant/test_rate_limit_anonymous.py | 10 ++++------ .../tests/kuadrant/test_rate_limit_authz.py | 5 ++--- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/testsuite/httpx/__init__.py b/testsuite/httpx/__init__.py index ac289987..ae070c0b 100644 --- a/testsuite/httpx/__init__.py +++ b/testsuite/httpx/__init__.py @@ -65,6 +65,18 @@ def __str__(self): return f"Result[error={self.error}]" +class ResultList(list): + """List-like object for Result""" + + def assert_all(self, status_code): + """Assert all responses that contain certain status code""" + for request in self: + assert request.status_code == status_code, ( + f"Status code assertion failed for request {self.index(request)+1} out of {len(self)} requests: " + f"{request} != {status_code}" + ) + + class KuadrantClient(Client): """Httpx client which retries unstable requests""" @@ -139,9 +151,9 @@ def request( def get(self, *args, **kwargs) -> Result: return super().get(*args, **kwargs) - def get_many(self, url, count, *, params=None, headers=None, auth=None) -> list[Result]: + def get_many(self, url, count, *, params=None, headers=None, auth=None) -> ResultList: """Send multiple `GET` requests.""" - responses = [] + responses = ResultList() for _ in range(count): responses.append(self.get(url, params=params, headers=headers, auth=auth)) diff --git a/testsuite/tests/kuadrant/limitador/test_basic_limit.py b/testsuite/tests/kuadrant/limitador/test_basic_limit.py index d546161a..e7eb284e 100644 --- a/testsuite/tests/kuadrant/limitador/test_basic_limit.py +++ b/testsuite/tests/kuadrant/limitador/test_basic_limit.py @@ -33,7 +33,5 @@ def rate_limit(rate_limit, limit): def test_limit(client, limit): """Tests that simple limit is applied successfully""" responses = client.get_many("/get", limit.limit) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" + responses.assert_all(status_code=200) assert client.get("/get").status_code == 429 diff --git a/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py b/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py index acf564c6..62e9fd96 100644 --- a/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py +++ b/testsuite/tests/kuadrant/limitador/test_multiple_iterations.py @@ -23,8 +23,6 @@ def test_multiple_iterations(client): """Tests that simple limit is applied successfully and works for multiple iterations""" for _ in range(10): responses = client.get_many("/get", 5) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" + responses.assert_all(status_code=200) assert client.get("/get").status_code == 429 sleep(10) diff --git a/testsuite/tests/kuadrant/limitador/test_route_rule.py b/testsuite/tests/kuadrant/limitador/test_route_rule.py index 168dc2f9..cd7cfaeb 100644 --- a/testsuite/tests/kuadrant/limitador/test_route_rule.py +++ b/testsuite/tests/kuadrant/limitador/test_route_rule.py @@ -34,9 +34,15 @@ def rate_limit(rate_limit): def test_rule(client): """Tests that RLP correctly applies to the given HTTPRoute rule""" responses = client.get_many("/get", 5) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" + responses.assert_all(status_code=200) + assert client.get("/get").status_code == 429 + response = client.get("/anything") assert response.status_code == 200 + + +def test_rule_missmatch(client): + """Tests that RLP is not applied for not existing route rule""" + responses = client.get_many("/anything/test", 7) + responses.assert_all(status_code=200) diff --git a/testsuite/tests/kuadrant/test_rate_limit_anonymous.py b/testsuite/tests/kuadrant/test_rate_limit_anonymous.py index 1118a482..97cab373 100644 --- a/testsuite/tests/kuadrant/test_rate_limit_anonymous.py +++ b/testsuite/tests/kuadrant/test_rate_limit_anonymous.py @@ -46,17 +46,15 @@ def auth(oidc_provider): def test_no_limit_for_auth_user(client, auth): """Test that no limit is not applied for authenticated user""" responses = client.get_many("/get", 7, auth=auth) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" + responses.assert_all(status_code=200) def test_anonymous_identity(client, auth): """Test that an anonymous requests are correctly limited""" assert client.get("/get", auth=auth).status_code == 200 + responses = client.get_many("/get", 5) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" + responses.assert_all(status_code=200) + assert client.get("/get").status_code == 429 assert client.get("/get", auth=auth).status_code == 200 diff --git a/testsuite/tests/kuadrant/test_rate_limit_authz.py b/testsuite/tests/kuadrant/test_rate_limit_authz.py index 78dda07f..52ce13c1 100644 --- a/testsuite/tests/kuadrant/test_rate_limit_authz.py +++ b/testsuite/tests/kuadrant/test_rate_limit_authz.py @@ -47,8 +47,7 @@ def auth2(rhsso, blame): def test_authz_limit(client, auth, auth2): """Tests that rate limit is applied for two users independently""" responses = client.get_many("/get", 5, auth=auth) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" + responses.assert_all(status_code=200) + assert client.get("/get", auth=auth).status_code == 429 assert client.get("/get", auth=auth2).status_code == 200