Files
romm/backend/tests/endpoints/sockets/test_devices.py
Sam Dornan 79fbce6beb test(backend): build test rows through shared factories and enforce them with ruff (#4972)
* test(backend): build test ROMs through one make_rom factory

Tests created Rom rows by hand in dozens of places, each spelling out the
same slug and filename fields and deriving the slug inline. Production never
computes a slug from a name, so this was test-only filler duplicated across
files.

tests/factories.py adds make_rom(platform, name, *, fs_stem, fs_extension,
**overrides), which fills the slug and filename columns and persists through
db_rom_handler.add_rom. The shared conftest fixtures, scan_stubs.add_rom and
the local helpers and inline constructors across 35 test files now use it.
Each test keeps its filename shape (underscored stems in the FULLTEXT search
tests, spaced "Name (USA)" stems in the sibling tests), and fixtures shared
across files keep their exact slugs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(backend): keep extensionless filenames intact in scan_stubs.add_rom

rpartition(".") on a name with no dot returned an empty stem and the whole
name as the extension, so make_rom stored ".README" for "README".
os.path.splitext leaves such names whole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(backend): build test saves, states and screenshots through factories

Tests built Save, State and Screenshot rows by hand across about 20 files,
each spelling out file_name_no_tags, file_name_no_ext and file_extension
although BaseAsset already derives them from file_name.

tests/factories.py adds make_save, make_state and make_screenshot. Each sets
the rom and user ids, the file name, and a default file_path under the rom's
platform, applies overrides last, and persists through the matching db
handler. The conftest asset fixtures and the persisted constructors and local
helpers now use them. Rows a test observes keep their file_path, emulator,
slot, size and hash, and a filename part stays an override wherever it
differs from what the model derives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(backend): ban direct add_* persistence in tests via ruff TID251

The factories only stay the one home for test rows if nothing routes
around them. Ruff's banned-api rule now flags db_rom_handler.add_rom,
db_save_handler.add_save, db_state_handler.add_state and
db_screenshot_handler.add_screenshot anywhere under tests/ except
tests/factories.py. Production code is unaffected.

The remaining add_rom calls move onto make_rom: the unique-path, soundtrack
and id-index tests in test_roms_handler.py, the scan tests in
test_fastapi.py, physical and missing rows in the feed, download and model
tests, and the name=None exporter rows. Scan tests keep a filename-derived
placeholder as the name, which scan_rom treats the same as no name. Three
calls keep a noqa: the tests of how add_rom merges a partial scanned row,
and the rescan test that persists scan_rom's output as a scan does.

The backend skill and the architecture doc now point at tests/factories.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(backend): move the firmware and client-token factories into tests/factories.py

The add_firmware and add_device_token conftest fixtures returned a builder,
unlike the importable make_* functions. They become make_firmware and
make_device_token alongside them, with the same defaults, and their callers
import them rather than requesting a fixture. make_device_token now draws the
raw credential from auth_handler.generate_client_token, so it carries the
real rmm_ prefix instead of restating it, and the hand-built client tokens in
the auth tests use it too.

The ruff TID251 ban now also covers db_firmware_handler.add_firmware and
db_client_token_handler.add_token under tests/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: point at every test factory, not just the ROM and asset ones

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 14:01:56 -05:00

416 lines
14 KiB
Python

from contextlib import asynccontextmanager
from datetime import datetime, timedelta, timezone
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import pytest
from socketio import exceptions as socketio_exceptions
from tests.factories import make_device_token
from endpoints.responses.device.install import InstallRequestSchema, InstallStatus
from endpoints.sockets import devices
from handler.auth.hybrid_auth import resolve_client_token
from handler.database import db_client_token_handler, db_device_handler
from handler.redis_handler import as_text, async_cache
from handler.socket_handler import DEVICES_NAMESPACE, socket_handler
from models.client_token import ClientToken
from models.device import Device
from models.user import User
NAMESPACE = DEVICES_NAMESPACE
async def _members(key: str) -> set[str]:
return {as_text(member) for member in await async_cache.smembers(key)}
@pytest.fixture
def socket_session(mocker) -> dict[str, Any]:
stored: dict[str, Any] = {}
@asynccontextmanager
async def session(sid: str, namespace: str | None = None):
yield stored
mocker.patch.object(socket_handler.socket_server, "session", session)
return stored
@pytest.fixture
def fire_and_forget(mocker) -> MagicMock:
schedule = MagicMock()
mocker.patch.object(devices, "fire_and_forget", schedule)
return schedule
@pytest.fixture
def background(mocker, fire_and_forget) -> MagicMock:
"""The ``_keep_tracked`` a connect schedules, without running it."""
keep_tracked = MagicMock()
mocker.patch.object(devices, "_keep_tracked", keep_tracked)
return keep_tracked
@pytest.fixture
def enter_room(mocker, socket_session, background):
return mocker.patch.object(socket_handler.socket_server, "enter_room", AsyncMock())
@pytest.fixture
def device_token():
"""A token for a new ``dev-socket`` device, or an unbound one."""
def _make(
user: User, *, device_bound: bool = True, **kwargs
) -> tuple[ClientToken, str]:
device_id = None
if device_bound:
device_id = db_device_handler.add_device(
Device(id="dev-socket", user_id=user.id, name="Handheld")
).id
return make_device_token(user, device_id, **kwargs)
return _make
class TestConnect:
async def test_joins_the_devices_room_from_the_auth_payload(
self, enter_room, socket_session, admin_user, device_token
):
token, raw = device_token(admin_user)
await devices.connect("sid-1", {}, {"token": raw})
enter_room.assert_awaited_once_with(
"sid-1", "device:dev-socket", namespace=NAMESPACE
)
assert socket_session == {
devices.CLIENT_TOKEN_ID_KEY: token.id,
devices.DEVICE_ID_KEY: "dev-socket",
}
async def test_accepts_a_bearer_header(self, enter_room, admin_user, device_token):
_, raw = device_token(admin_user)
await devices.connect("sid-1", {"HTTP_AUTHORIZATION": f"Bearer {raw}"}, None)
enter_room.assert_awaited_once()
async def test_tracks_the_socket_for_revocation_and_presence(
self, enter_room, admin_user, device_token
):
token, raw = device_token(admin_user)
await devices.connect("sid-1", {}, {"token": raw})
token_key = f"device_token_sockets:{token.id}"
presence_key = "device_presence:dev-socket"
assert await _members(token_key) == {"sid-1"}
assert await _members(presence_key) == {"sid-1"}
assert (
0 < await async_cache.ttl(token_key) <= devices.SOCKET_TRACKING_TTL_SECONDS
)
assert 0 < await async_cache.ttl(presence_key) <= devices.PRESENCE_TTL_SECONDS
async def test_refuses_a_token_revoked_before_the_socket_was_tracked(
self, mocker, enter_room, admin_user, device_token
):
token, raw = device_token(admin_user)
mocker.patch.object(
devices,
"resolve_client_token",
side_effect=[resolve_client_token(raw), None],
)
with pytest.raises(socketio_exceptions.ConnectionRefusedError):
await devices.connect("sid-1", {}, {"token": raw})
assert not await async_cache.exists(f"device_token_sockets:{token.id}")
assert await devices.online_device_ids(["dev-socket"]) == []
enter_room.assert_not_awaited()
async def test_keeps_the_tracking_fresh_while_connected(
self, enter_room, background, fire_and_forget, admin_user, device_token
):
token, raw = device_token(admin_user)
await devices.connect("sid-1", {}, {"token": raw})
background.assert_called_once_with(
"sid-1", devices.BoundToken(token.id, "dev-socket", None)
)
fire_and_forget.assert_called_once_with(background.return_value)
async def test_bumps_the_tokens_last_use(
self, enter_room, admin_user, device_token
):
token, raw = device_token(admin_user)
await devices.connect("sid-1", {}, {"token": raw})
[stored] = db_client_token_handler.get_tokens_by_user(admin_user.id)
assert stored.id == token.id
assert stored.last_used_at is not None
async def test_never_writes_the_devices_capabilities(
self, enter_room, admin_user, device_token
):
_, raw = device_token(admin_user)
await devices.connect(
"sid-1", {}, {"token": raw, "capabilities": {"remote_install": True}}
)
stored = db_device_handler.get_device(
device_id="dev-socket", user_id=admin_user.id
)
assert stored is not None
assert stored.capabilities is None
@pytest.mark.parametrize(
"token_kwargs",
[
{"device_bound": False},
{"scopes": "roms.read"},
{"expires_at": datetime.now(timezone.utc) - timedelta(minutes=1)},
],
)
async def test_refuses_a_token_that_does_not_speak_for_a_device(
self, enter_room, admin_user, device_token, token_kwargs
):
_, raw = device_token(admin_user, **token_kwargs)
with pytest.raises(socketio_exceptions.ConnectionRefusedError):
await devices.connect("sid-1", {}, {"token": raw})
enter_room.assert_not_awaited()
@pytest.mark.parametrize("auth", [None, {"token": "rmm_unknown"}, {"token": "x"}])
async def test_refuses_a_missing_or_unknown_token(self, enter_room, auth):
with pytest.raises(socketio_exceptions.ConnectionRefusedError):
await devices.connect("sid-1", {}, auth)
enter_room.assert_not_awaited()
async def test_never_accepts_a_browser_session(self, enter_room):
environ = {"HTTP_COOKIE": "romm_session=abc"}
with pytest.raises(socketio_exceptions.ConnectionRefusedError):
await devices.connect("sid-1", environ, None)
async def test_refuses_every_device_while_install_is_disabled(
self, mocker, enter_room, admin_user, device_token
):
_, raw = device_token(admin_user)
mocker.patch.object(devices, "DEVICE_INSTALL_ENABLED", False)
with pytest.raises(socketio_exceptions.ConnectionRefusedError):
await devices.connect("sid-1", {}, {"token": raw})
enter_room.assert_not_awaited()
BOUND = devices.BoundToken(42, "dev-1", None)
class TestKeepTracked:
@pytest.fixture(autouse=True)
def sleep(self, mocker) -> AsyncMock:
sleep = AsyncMock()
mocker.patch.object(socket_handler.socket_server, "sleep", sleep)
return sleep
async def test_refreshes_while_the_socket_is_connected(self, mocker):
mocker.patch.object(
socket_handler.socket_server.manager,
"is_connected",
side_effect=[True, False],
)
await devices._keep_tracked("sid-1", BOUND)
assert await _members("device_presence:dev-1") == {"sid-1"}
assert 0 < await async_cache.ttl("device_presence:dev-1")
async def test_keeps_a_long_lived_socket_revocable(self, mocker):
mocker.patch.object(
socket_handler.socket_server.manager,
"is_connected",
side_effect=[True, False],
)
await async_cache.sadd("device_token_sockets:42", "sid-1")
await async_cache.expire("device_token_sockets:42", 5)
await devices._keep_tracked("sid-1", BOUND)
assert await _members("device_token_sockets:42") == {"sid-1"}
assert (
await async_cache.ttl("device_token_sockets:42")
> devices.SOCKET_TRACKING_TTL_SECONDS - 60
)
async def test_stops_once_the_socket_is_gone(self, mocker):
mocker.patch.object(
socket_handler.socket_server.manager, "is_connected", return_value=False
)
await devices._keep_tracked("sid-1", BOUND)
assert not await async_cache.exists("device_presence:dev-1")
assert not await async_cache.exists("device_token_sockets:42")
async def test_closes_the_socket_once_its_token_expires(self, mocker):
mocker.patch.object(
socket_handler.socket_server.manager,
"is_connected",
side_effect=[True, False],
)
disconnect = mocker.patch.object(
socket_handler.socket_server, "disconnect", AsyncMock()
)
expired = BOUND._replace(
expires_at=datetime.now(timezone.utc) - timedelta(seconds=1)
)
await devices._keep_tracked("sid-1", expired)
disconnect.assert_awaited_once_with("sid-1", namespace=NAMESPACE)
assert not await async_cache.exists("device_presence:dev-1")
async def test_keeps_a_socket_whose_token_has_not_expired(self, mocker):
mocker.patch.object(
socket_handler.socket_server.manager,
"is_connected",
side_effect=[True, False],
)
disconnect = mocker.patch.object(
socket_handler.socket_server, "disconnect", AsyncMock()
)
later = BOUND._replace(
expires_at=datetime.now(timezone.utc) + timedelta(days=1)
)
await devices._keep_tracked("sid-1", later)
disconnect.assert_not_awaited()
assert await _members("device_presence:dev-1") == {"sid-1"}
class TestEmitToDevice:
async def test_cancelled_goes_to_the_devices_room(self, mocker):
emit = AsyncMock()
mocker.patch.object(
socket_handler, "write_manager", return_value=MagicMock(emit=emit)
)
request = InstallRequestSchema(
id="req-1",
user_id=1,
device_id="dev-1",
rom_id=7,
file_ids=[10],
status=InstallStatus.CANCELLED,
reason=None,
created_at=datetime.now(timezone.utc),
updated_at=datetime.now(timezone.utc),
)
await devices.emit_install_cancelled(request)
emit.assert_awaited_once_with(
"install:cancelled",
{"id": "req-1", "rom_id": 7},
namespace=NAMESPACE,
room="device:dev-1",
)
class TestDisconnect:
async def test_forgets_the_socket(self, mocker):
await async_cache.sadd("device_token_sockets:42", "sid-1", "sid-2")
await async_cache.sadd("device_presence:dev-1", "sid-1")
mocker.patch.object(
socket_handler.socket_server,
"get_session",
AsyncMock(
return_value={
devices.CLIENT_TOKEN_ID_KEY: 42,
devices.DEVICE_ID_KEY: "dev-1",
}
),
)
await devices.disconnect("sid-1")
assert await _members("device_token_sockets:42") == {"sid-2"}
assert await devices.online_device_ids(["dev-1"]) == []
def test_is_registered_on_the_devices_namespace_only(self):
handlers = socket_handler.socket_server.handlers
assert handlers[NAMESPACE]["disconnect"] is devices.disconnect
assert handlers[NAMESPACE]["connect"] is devices.connect
assert handlers["/"]["connect"] is not devices.connect
class TestPresence:
async def test_a_device_with_a_fresh_presence_is_online(self):
await async_cache.sadd("device_presence:dev-1", "sid-1")
assert await devices.online_device_ids(["dev-1", "dev-2"]) == ["dev-1"]
async def test_keeps_the_callers_order(self):
await async_cache.sadd("device_presence:dev-1", "sid-1")
await async_cache.sadd("device_presence:dev-2", "sid-2")
assert await devices.online_device_ids(["dev-2", "dev-1"]) == [
"dev-2",
"dev-1",
]
async def test_a_revocation_entry_alone_is_not_presence(self):
await async_cache.sadd("device_token_sockets:42", "sid-1")
assert await devices.online_device_ids(["dev-1"]) == []
async def test_no_devices_asks_nothing(self):
assert await devices.online_device_ids([]) == []
class TestEmit:
@pytest.fixture
def manager(self, mocker) -> MagicMock:
manager = MagicMock(emit=AsyncMock())
mocker.patch.object(socket_handler, "write_manager", return_value=manager)
return manager
def _request(self) -> InstallRequestSchema:
now = datetime.now(timezone.utc)
return InstallRequestSchema(
id="r1",
user_id=5,
device_id="dev-1",
rom_id=7,
file_ids=[1],
status=InstallStatus.PENDING,
reason=None,
created_at=now,
updated_at=now,
)
async def test_queued_reaches_only_the_devices_room_on_its_namespace(self, manager):
await devices.emit_install_queued(self._request())
manager.emit.assert_awaited_once_with(
"install:queued",
{"id": "r1", "rom_id": 7},
namespace=NAMESPACE,
room="device:dev-1",
)
async def test_updated_reaches_the_owners_tabs(self, manager):
await devices.emit_install_updated(self._request())
event, payload = manager.emit.await_args.args
assert event == "install:updated"
assert payload["id"] == "r1"
assert manager.emit.await_args.kwargs == {"room": "user:5"}