mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +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
3.0 KiB
Python
98 lines
3.0 KiB
Python
"""The netplay room listing must not reveal rooms for a hidden rom."""
|
|
|
|
from datetime import timedelta
|
|
from typing import cast
|
|
from unittest.mock import AsyncMock, Mock
|
|
|
|
import pytest
|
|
|
|
from config import OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
|
|
from handler.auth.base_handler import oauth_handler
|
|
from handler.auth.constants import Scope
|
|
from handler.database.base_handler import sync_session
|
|
from handler.netplay_handler import NetplayPlayerInfo, NetplayRoom, netplay_handler
|
|
from models.permission import HiddenEntity, PermEntity
|
|
from models.user import User
|
|
|
|
|
|
def _auth(user: User, scopes: list[Scope] | None = None) -> dict[str, str]:
|
|
# Re-reads the user's current (projected) scopes each call.
|
|
token = oauth_handler.create_access_token(
|
|
data={
|
|
"sub": user.username,
|
|
"iss": "romm:oauth",
|
|
"scopes": " ".join(scopes if scopes is not None else user.oauth_scopes),
|
|
},
|
|
expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS),
|
|
)
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
def _hide(entity: PermEntity, entity_id: int, user_id: int) -> None:
|
|
with sync_session.begin() as s:
|
|
s.add(HiddenEntity(entity=entity, entity_id=entity_id, user_id=user_id))
|
|
|
|
|
|
def _room(rom_id: int) -> NetplayRoom:
|
|
return NetplayRoom(
|
|
owner="sid",
|
|
players={
|
|
"p": NetplayPlayerInfo(
|
|
socketId="sid",
|
|
player_name="Player p",
|
|
userid=None,
|
|
playerId="p",
|
|
)
|
|
},
|
|
peers=[],
|
|
room_name="Room",
|
|
game_id=str(rom_id),
|
|
domain=None,
|
|
password=None,
|
|
max_players=4,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def rooms(mocker) -> Mock:
|
|
return cast(
|
|
Mock,
|
|
mocker.patch.object(netplay_handler, "get_all", AsyncMock(return_value={})),
|
|
)
|
|
|
|
|
|
def test_listing_shows_rooms_for_a_visible_rom(client, viewer_user, rom, rooms):
|
|
rooms.return_value = {"room-1": _room(rom.id)}
|
|
|
|
resp = client.get(f"/api/netplay/list?game_id={rom.id}", headers=_auth(viewer_user))
|
|
|
|
assert resp.status_code == 200
|
|
assert "room-1" in resp.json()
|
|
|
|
|
|
def test_listing_hides_rooms_for_a_hidden_rom(client, viewer_user, rom, rooms):
|
|
_hide(PermEntity.ROMS, rom.id, viewer_user.id)
|
|
rooms.return_value = {"room-1": _room(rom.id)}
|
|
|
|
resp = client.get(f"/api/netplay/list?game_id={rom.id}", headers=_auth(viewer_user))
|
|
|
|
assert resp.status_code == 404
|
|
rooms.assert_not_awaited()
|
|
|
|
|
|
def test_listing_of_a_missing_rom_is_not_found(client, viewer_user, rooms):
|
|
resp = client.get("/api/netplay/list?game_id=999999", headers=_auth(viewer_user))
|
|
|
|
assert resp.status_code == 404
|
|
rooms.assert_not_awaited()
|
|
|
|
|
|
def test_listing_needs_the_rom_read_scope(client, viewer_user, rooms):
|
|
"""The listing answers about a rom, so asset access alone is not enough."""
|
|
headers = _auth(viewer_user, scopes=[Scope.ASSETS_READ])
|
|
|
|
resp = client.get("/api/netplay/list?game_id=1", headers=headers)
|
|
|
|
assert resp.status_code == 403
|
|
rooms.assert_not_awaited()
|