Replace process-local session cache with pluggable session cache backend
This commit is contained in:
@@ -209,13 +209,11 @@ class TestRequireAuthSessionCache:
|
||||
"""In-memory session token cache inside ``require_auth``."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_cache(self) -> Generator[None, None, None]:
|
||||
def reset_cache(self, client: AsyncClient) -> Generator[None, None, None]:
|
||||
"""Flush the session cache before and after every test in this class."""
|
||||
from app import dependencies
|
||||
|
||||
dependencies.clear_session_cache()
|
||||
client._transport.app.state.session_cache.clear()
|
||||
yield
|
||||
dependencies.clear_session_cache()
|
||||
client._transport.app.state.session_cache.clear()
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def enable_session_cache(self, client: AsyncClient) -> Generator[None, None, None]:
|
||||
@@ -237,9 +235,7 @@ class TestRequireAuthSessionCache:
|
||||
token = await _login(client)
|
||||
|
||||
# Ensure cache is empty so the first request definitely hits the DB.
|
||||
from app import dependencies
|
||||
|
||||
dependencies.clear_session_cache()
|
||||
client._transport.app.state.session_cache.clear()
|
||||
|
||||
call_count = 0
|
||||
original_get_session = session_repo.get_session
|
||||
@@ -268,27 +264,25 @@ class TestRequireAuthSessionCache:
|
||||
async def test_token_enters_cache_after_first_auth(
|
||||
self, client: AsyncClient
|
||||
) -> None:
|
||||
"""A successful auth request places the token in ``_session_cache``."""
|
||||
from app import dependencies
|
||||
"""A successful auth request places the token in the session cache."""
|
||||
|
||||
await _do_setup(client)
|
||||
token = await _login(client)
|
||||
|
||||
dependencies.clear_session_cache()
|
||||
assert token not in dependencies._session_cache
|
||||
client._transport.app.state.session_cache.clear()
|
||||
assert client._transport.app.state.session_cache.get(token) is None
|
||||
|
||||
await client.get(
|
||||
"/api/dashboard/status",
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
|
||||
assert token in dependencies._session_cache
|
||||
assert client._transport.app.state.session_cache.get(token) is not None
|
||||
|
||||
async def test_logout_evicts_token_from_cache(
|
||||
self, client: AsyncClient
|
||||
) -> None:
|
||||
"""Logout removes the session token from the in-memory cache immediately."""
|
||||
from app import dependencies
|
||||
"""Logout removes the session token from the session cache immediately."""
|
||||
|
||||
await _do_setup(client)
|
||||
token = await _login(client)
|
||||
@@ -298,14 +292,14 @@ class TestRequireAuthSessionCache:
|
||||
"/api/dashboard/status",
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
assert token in dependencies._session_cache
|
||||
assert client._transport.app.state.session_cache.get(token) is not None
|
||||
|
||||
# Logout must evict the entry.
|
||||
await client.post(
|
||||
"/api/auth/logout",
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
assert token not in dependencies._session_cache
|
||||
assert client._transport.app.state.session_cache.get(token) is None
|
||||
|
||||
response = await client.get("/api/health")
|
||||
assert response.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user