Files
romm/backend/tests/endpoints/test_netplay.py
Sam Dornan bd16648c9a chore(mypy): switch to strict mode with listed exceptions (#4817)
* chore(mypy): switch to strict mode with listed exceptions

Replace the hand-picked flags with `strict = True`, turning off only the
four flags that aren't clean yet (disallow_any_generics,
warn_return_any, disallow_untyped_calls, disallow_untyped_decorators).
Any other strict flag, including those future mypy releases add to the
preset, is on by default.

That turns on no_implicit_reexport, strict_equality and extra_checks:
- Import each name from the module that defines it. About 240 test
  sites patched a dependency through the module that imported it
  (`scan_module.fs_rom_handler`); they now import the same object from
  its source, so every patch targets what it did before. Tests that
  replace a module attribute are unchanged.
- handler.streaming.memory_cards re-exports store_memory_card_version
  explicitly, since endpoints call it there and tests patch it there.
- mutagen gets implicit_reexport, as it re-exports without __all__.
- Test comparisons build typed values (CoverResult, a typed expected
  dict, enum .value) and the CORS lookup casts for its identity check.
- One ss_handler ** spread keeps a coded ignore: extra_checks wants its
  NotRequired dump_* keys spelled out, and the media dict never has them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

* test(upload): assert cleanup against the patched upload directory

The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches
the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:45:37 -05:00

94 lines
2.9 KiB
Python

"""The netplay room listing must not reveal rooms for a hidden rom."""
from datetime import timedelta
from unittest.mock import AsyncMock, Mock
import pytest
from config import OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
from handler.auth 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 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()