mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
Importing any handler.auth submodule ran the package __init__, which built the auth handlers and so loaded authlib, joserfc and Socket.IO. models.user imports handler.auth.constants, so every process that touched the models paid for the whole auth stack. The handler instances now live next to their classes in handler.auth.base_handler. utils.context imported fastapi only for annotations; that import now sits under TYPE_CHECKING. An idle RQ worker drops from about 118 MB to 89 MB RSS. Each job's forked work horse now imports a little more on its own, about 0.2s on an x86 host. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gh1SRMfuK7BZmf6YxMqSyT
98 lines
2.4 KiB
Python
98 lines
2.4 KiB
Python
from datetime import timedelta
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from main import app
|
|
|
|
from config import (
|
|
OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS,
|
|
OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS,
|
|
)
|
|
from handler.auth.base_handler import oauth_handler
|
|
from handler.redis_handler import sync_cache
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
with TestClient(app) as client:
|
|
yield client
|
|
|
|
|
|
@pytest.fixture
|
|
def _isolated_assets_dir(tmp_path, monkeypatch):
|
|
"""Point the shared fs_asset_handler at the test's tmp dir."""
|
|
from pathlib import Path
|
|
|
|
from handler.filesystem import fs_asset_handler
|
|
|
|
new_base = Path(tmp_path).resolve()
|
|
monkeypatch.setattr(fs_asset_handler, "base_path", new_base)
|
|
return new_base
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clear_cache():
|
|
sync_cache.flushall()
|
|
yield
|
|
sync_cache.flushall()
|
|
|
|
|
|
@pytest.fixture()
|
|
def access_token(admin_user): # noqa
|
|
data = {
|
|
"sub": admin_user.username,
|
|
"iss": "romm:oauth",
|
|
"scopes": " ".join(admin_user.oauth_scopes),
|
|
}
|
|
|
|
return oauth_handler.create_access_token(
|
|
data=data, expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS)
|
|
)
|
|
|
|
|
|
@pytest.fixture()
|
|
def refresh_token(admin_user): # noqa
|
|
data = {
|
|
"sub": admin_user.username,
|
|
"iss": "romm:oauth",
|
|
"scopes": " ".join(admin_user.oauth_scopes),
|
|
}
|
|
|
|
return oauth_handler.create_refresh_token(
|
|
data=data, expires_delta=timedelta(seconds=OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS)
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def editor_access_token(editor_user): # noqa
|
|
return oauth_handler.create_access_token(
|
|
data={
|
|
"sub": editor_user.username,
|
|
"iss": "romm:oauth",
|
|
"scopes": " ".join(editor_user.oauth_scopes),
|
|
},
|
|
expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS),
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def headers(access_token: str) -> dict[str, str]:
|
|
return {"Authorization": f"Bearer {access_token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def editor_headers(editor_access_token: str) -> dict[str, str]:
|
|
return {"Authorization": f"Bearer {editor_access_token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def viewer_access_token(viewer_user): # noqa
|
|
return oauth_handler.create_access_token(
|
|
data={
|
|
"sub": viewer_user.username,
|
|
"iss": "romm:oauth",
|
|
"scopes": " ".join(viewer_user.oauth_scopes),
|
|
},
|
|
expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS),
|
|
)
|