Files
romm/backend/tests/endpoints/conftest.py
Georges-Antoine Assi 1c491e0aee refactor(devices): pr-ready simplify pass for device installs
- Share one watched-transaction retry loop and live/gone split in the install handler
- Emit independent socket events concurrently
- Drop the dialog's redundant close watcher and repeated current-rom checks
- Reuse the add_device_token fixture and shared header fixtures in tests
- Fix mypy errors in the socket and handler tests

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 14:49:13 -04:00

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 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),
)