fix rate-limit valid login bug and fix sessions of redis and psql in tests

This commit is contained in:
2026-09-05 21:55:31 +03:00
parent c4a6a88d05
commit 9fd44c0ad9
8 changed files with 50 additions and 30 deletions
+17 -1
View File
@@ -1,6 +1,9 @@
import pytest_asyncio
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from src.models.configs_read.env import env_settings
from src.service.auth.jwt import HashService, JwtService
from src.service.users_crud.users_crud import CrudService
@pytest_asyncio.fixture
@@ -11,4 +14,17 @@ async def jwt_service()->JwtService:
@pytest_asyncio.fixture
async def hash_service()->HashService:
hash_service=HashService()
return hash_service
return hash_service
@pytest_asyncio.fixture
async def crud_service():
test_engine = create_async_engine(f"postgresql+asyncpg://{env_settings.DB_USER}:{env_settings.DB_PASSWORD}@{env_settings.DB_HOST}:{env_settings.DB_PORT}/{env_settings.DB_POSTGRESS}",
pool_size=20,
max_overflow=10,
pool_timeout=30,
pool_pre_ping=True
)
crud_service = CrudService()
crud_service.crud_db_actions.Session = async_sessionmaker(bind=test_engine)
yield crud_service
await test_engine.dispose()
+2
View File
@@ -12,8 +12,10 @@ class TestAuth:
response = await requests_async.post(f"{target_url}/protected/token")
response.raise_for_status()
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:
+10 -9
View File
@@ -1,22 +1,23 @@
import pytest_asyncio
from fastapi import Request
from src.cache.redis_client import RedisClient
from src.service.auth.auth import CurrentUserService
from src.service.users_crud.users_crud import CrudService
@pytest_asyncio.fixture
async def current_user_service()->CurrentUserService:
current_user_service=CurrentUserService()
return current_user_service
async def current_user_service(monkeypatch):
test_redis = RedisClient()
monkeypatch.setattr("src.service.auth.auth.redis_client", test_redis)
service = CurrentUserService()
yield service
await test_redis.aclose()
@pytest_asyncio.fixture
async def requests(mocker):
fake_request = mocker.MagicMock(spec=Request)
fake_request.headers = {"user-agent": "pytest-agent", "x-forwarded-for":"127.0.0.1"}
return fake_request
@pytest_asyncio.fixture
async def crud_service()->CrudService:
crud_service=CrudService()
return crud_service
+6 -5
View File
@@ -118,7 +118,8 @@ class TestAuth:
with allure.step("create fake refresh token"):
token=await jwt_service.create_refresh_token({"sub":str(uuid4())})
refresh_token=await jwt_service.create_refresh_token({"sub":str(uuid4())})
access_token=await jwt_service.create_access_token({"sub":str(uuid4)})
with allure.step("patching db call functions"):
@@ -126,7 +127,7 @@ class TestAuth:
with allure.step("test logout with fake data"):
status=await current_user_service.logout(token[0])
status=await current_user_service.logout(refresh_token[0], access_token)
assert status is True
@pytest.mark.parametrize("jti,db_result, expected_exception, expected_status",[
@@ -148,11 +149,11 @@ class TestAuth:
with allure.step("create fake refresh token"):
token=await fake_create_refresh_token({"sub":str(uuid4()), "jti":jti, "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)})
refresh_token=await fake_create_refresh_token({"sub":str(uuid4()), "jti":jti, "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(days=45)})
access_token=await fake_create_refresh_token({"sub":str(uuid4()), "jti":jti, "token_type":"refresh", "exp":datetime.now(UTC)+timedelta(minutes=30)})
with allure.step("test logout with fake data"), pytest.raises(expected_exception) as exc_info:
await current_user_service.logout(token)
await current_user_service.logout(refresh_token, access_token)
if expected_exception is HTTPException:
assert exc_info.value.status_code==expected_status
-9
View File
@@ -1,9 +0,0 @@
import pytest_asyncio
from src.service.users_crud.users_crud import CrudService
@pytest_asyncio.fixture
async def crud_service()->CrudService:
crud_service=CrudService()
return crud_service