From 5cdcd342dcff52dcd425a689cff36335299da28e Mon Sep 17 00:00:00 2001 From: "MH.Dmitrii" Date: Sat, 5 Sep 2026 22:31:54 +0300 Subject: [PATCH] add tests for the redis --- pyproject.toml | 4 +++- src/cache/rate_limit.py | 6 +----- tests/e2e/test_auth.py | 42 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1475da4..841d794 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,7 +49,9 @@ omit = [ "*/database/*", "*/errors/*", "__init__.py", - "*/docker/*" + "*/docker/*", + "*/rate_limit.py", + "*/logger.py" ] [tool.coverage.report] diff --git a/src/cache/rate_limit.py b/src/cache/rate_limit.py index 6ed4fa2..b508532 100644 --- a/src/cache/rate_limit.py +++ b/src/cache/rate_limit.py @@ -1,5 +1,3 @@ -from fastapi import Request - from src.cache.redis_client import redis_client from src.errors.http_errors.errors import Errors @@ -23,6 +21,4 @@ class RateLimit: await self.rate_limit(client_ip) -rate_limiter=RateLimit() - - +rate_limiter=RateLimit() \ No newline at end of file diff --git a/tests/e2e/test_auth.py b/tests/e2e/test_auth.py index 45618f9..39484ea 100644 --- a/tests/e2e/test_auth.py +++ b/tests/e2e/test_auth.py @@ -4,7 +4,7 @@ import requests_async from httpx import HTTPStatusError -class TestAuth: +class TestPermissions: async def test_get_access_token_positive(self, target_url:str)->None: @@ -15,7 +15,6 @@ class TestAuth: assert exc_info.value.response.status_code != 403 assert exc_info.value.response.status_code != 401 - assert exc_info.value.response.status_code != 429 async def test_get_refresh_token_positive(self, target_url:str)->None: @@ -26,7 +25,7 @@ class TestAuth: assert exc_info.value.response.status_code != 403 assert exc_info.value.response.status_code != 401 - async def test_get_root_positive(self, target_url:str)->None: + async def test_get_root_unauthorized(self, target_url:str)->None: with allure.step("get_root"), pytest.raises(HTTPStatusError) as exc_info: @@ -45,4 +44,41 @@ class TestAuth: response.raise_for_status() assert exc_info.value.response.status_code != 403 assert exc_info.value.response.status_code != 401 + + +class TestRedis: + + @pytest.mark.parametrize("wrong_user_data, expected_status",[ + pytest.param({"username":"Wrong_user", "password":"Wrong_password"},429,id="Wrong_user_creds") + ]) + async def test_rate_limit_positive(self, wrong_user_data:dict, target_url:str, expected_status:int): + + with allure.step("logging with invalid creds"): + + for i in range(5): + with pytest.raises(HTTPStatusError) as exc_info: + response = await requests_async.post(target_url + "/protected/token", data=wrong_user_data) + response.raise_for_status() + assert exc_info.value.response.status_code == 401, f"Attempt {i+1} should be 401" + + with allure.step("verify rate_limit works"),pytest.raises(HTTPStatusError) as exc_info: + response = await requests_async.post(target_url + "/protected/token", data=wrong_user_data) + response.raise_for_status() + assert exc_info.value.response.status_code == expected_status + + + @pytest.mark.parametrize("test_user_fixture", [([], [])], indirect=True) + async def test_get_logout_revoke_positive(self,test_user_fixture, target_url:str)->None: + + session=test_user_fixture[0] + + with allure.step("logout"): + + response = await session.get(f"{target_url}/protected/logout") + response.raise_for_status() + + with allure.step("verify token is revoked"), pytest.raises(HTTPStatusError) as exc_info: + response = await session.get(f"{target_url}/protected") + response.raise_for_status() + assert exc_info.value.response.status_code==429 \ No newline at end of file