fix rate-limit valid login bug and fix sessions of redis and psql in tests
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user