mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* 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>
248 lines
7.5 KiB
Python
248 lines
7.5 KiB
Python
from collections.abc import Sequence
|
|
from datetime import datetime, timezone
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from handler import notification_handler
|
|
from handler.database import db_notification_handler, db_user_handler
|
|
from handler.notification_handler import (
|
|
NOTIFICATIONS_NEW_EVENT,
|
|
UnknownRecipientsError,
|
|
notify,
|
|
notify_admins,
|
|
notify_user_or_admins,
|
|
recipient_ids,
|
|
resolve_recipients,
|
|
send,
|
|
)
|
|
from handler.socket_handler import socket_handler
|
|
from models.notification import Notification, NotificationKind, NotificationLevel
|
|
|
|
|
|
def _stored(notifications: Sequence[Notification]) -> Sequence[Notification]:
|
|
for i, notification in enumerate(notifications, start=7):
|
|
notification.id = i
|
|
notification.created_at = datetime(2026, 9, 23, tzinfo=timezone.utc)
|
|
notification.read_at = None
|
|
notification.actor = None
|
|
return notifications
|
|
|
|
|
|
@pytest.fixture
|
|
def emit(mocker):
|
|
return mocker.patch.object(socket_handler, "emit_to_user", AsyncMock())
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def enqueue_channel_deliveries(mocker):
|
|
return mocker.patch.object(notification_handler, "enqueue_channel_deliveries")
|
|
|
|
|
|
@pytest.fixture
|
|
def add_notifications(mocker):
|
|
return mocker.patch.object(
|
|
db_notification_handler,
|
|
"add_notifications",
|
|
side_effect=_stored,
|
|
)
|
|
|
|
|
|
class TestNotify:
|
|
async def test_stores_then_pushes_the_stored_row(self, emit, add_notifications):
|
|
await notify(
|
|
3,
|
|
NotificationKind.SCAN_COMPLETED,
|
|
NotificationLevel.SUCCESS,
|
|
{"new_roms": 2},
|
|
)
|
|
|
|
[stored] = add_notifications.call_args.args[0]
|
|
assert stored.user_id == 3
|
|
assert stored.kind == NotificationKind.SCAN_COMPLETED
|
|
user_id, event, payload = emit.await_args.args
|
|
assert (user_id, event) == (3, NOTIFICATIONS_NEW_EVENT)
|
|
assert payload["id"] == 7
|
|
assert payload["data"] == {"new_roms": 2}
|
|
assert payload["created_at"].startswith("2026-09-23")
|
|
|
|
async def test_queues_the_stored_row_for_the_users_channels(
|
|
self, emit, add_notifications, enqueue_channel_deliveries
|
|
):
|
|
await notify(3, NotificationKind.SCAN_FAILED, NotificationLevel.ERROR)
|
|
|
|
[(user_id, schema)] = enqueue_channel_deliveries.call_args.args[0]
|
|
assert (user_id, schema.id) == (3, 7)
|
|
|
|
async def test_carries_its_own_content_for_a_custom_kind(
|
|
self, emit, add_notifications
|
|
):
|
|
await notify(
|
|
3,
|
|
"argosy.sync_done",
|
|
NotificationLevel.INFO,
|
|
title="Sync finished",
|
|
body="12 saves uploaded",
|
|
link="/rom/12",
|
|
icon="mdi-sync",
|
|
)
|
|
|
|
payload = emit.await_args.args[2]
|
|
assert payload["kind"] == "argosy.sync_done"
|
|
assert (payload["title"], payload["body"]) == (
|
|
"Sync finished",
|
|
"12 saves uploaded",
|
|
)
|
|
assert (payload["link"], payload["icon"]) == ("/rom/12", "mdi-sync")
|
|
|
|
async def test_a_storage_failure_pushes_nothing(self, mocker, emit):
|
|
mocker.patch.object(
|
|
db_notification_handler,
|
|
"add_notifications",
|
|
side_effect=RuntimeError("database gone"),
|
|
)
|
|
|
|
await notify(3, NotificationKind.TASK_FAILED, NotificationLevel.ERROR)
|
|
|
|
emit.assert_not_awaited()
|
|
|
|
|
|
class TestNotifyAdmins:
|
|
async def test_stores_one_row_per_admin_in_one_go(
|
|
self, mocker, emit, add_notifications
|
|
):
|
|
mocker.patch.object(notification_handler, "recipient_ids", return_value=[1, 4])
|
|
|
|
await notify_admins(NotificationKind.TASK_FAILED, NotificationLevel.ERROR)
|
|
|
|
add_notifications.assert_called_once()
|
|
assert [n.user_id for n in add_notifications.call_args.args[0]] == [1, 4]
|
|
assert emit.await_count == 2
|
|
|
|
|
|
class TestRecipientIds:
|
|
@pytest.fixture
|
|
def users(self, mocker):
|
|
return mocker.patch.object(
|
|
db_user_handler,
|
|
"get_users",
|
|
return_value=[
|
|
MagicMock(id=1, enabled=True),
|
|
MagicMock(id=2, enabled=False),
|
|
MagicMock(id=3, enabled=True),
|
|
],
|
|
)
|
|
|
|
def test_everyone_enabled(self, users):
|
|
assert recipient_ids("all") == [1, 3]
|
|
|
|
def test_only_the_named_enabled_users(self, users):
|
|
assert recipient_ids([3, 2, 3, 99]) == [3]
|
|
|
|
|
|
class TestNotifyUserOrAdmins:
|
|
@pytest.fixture
|
|
def targets(self, mocker):
|
|
return (
|
|
mocker.patch.object(notification_handler, "notify", AsyncMock()),
|
|
mocker.patch.object(notification_handler, "notify_admins", AsyncMock()),
|
|
)
|
|
|
|
async def test_the_starter_hears_first(self, targets):
|
|
notify_mock, admins_mock = targets
|
|
|
|
await notify_user_or_admins(
|
|
5,
|
|
NotificationKind.TASK_FAILED,
|
|
NotificationLevel.ERROR,
|
|
{},
|
|
admins_too=True,
|
|
)
|
|
|
|
notify_mock.assert_awaited_once()
|
|
admins_mock.assert_not_awaited()
|
|
|
|
@pytest.mark.parametrize("admins_too", [True, False])
|
|
async def test_nobody_started_it(self, targets, admins_too):
|
|
notify_mock, admins_mock = targets
|
|
|
|
await notify_user_or_admins(
|
|
None,
|
|
NotificationKind.TASK_FAILED,
|
|
NotificationLevel.ERROR,
|
|
{},
|
|
admins_too=admins_too,
|
|
)
|
|
|
|
notify_mock.assert_not_awaited()
|
|
assert admins_mock.await_count == int(admins_too)
|
|
|
|
|
|
class TestResolveRecipients:
|
|
@pytest.fixture
|
|
def users(self, mocker):
|
|
return mocker.patch.object(
|
|
db_user_handler,
|
|
"get_users",
|
|
return_value=[
|
|
MagicMock(id=1, enabled=True),
|
|
MagicMock(id=2, enabled=False),
|
|
MagicMock(id=3, enabled=True),
|
|
],
|
|
)
|
|
|
|
@pytest.mark.parametrize("recipients", [None, [3]])
|
|
def test_the_sender_alone_needs_no_lookup(self, users, recipients):
|
|
assert resolve_recipients(3, recipients) == [3]
|
|
users.assert_not_called()
|
|
|
|
def test_a_group_is_its_enabled_members(self, users):
|
|
assert resolve_recipients(1, "all") == [1, 3]
|
|
|
|
def test_names_the_users_it_cannot_reach(self, users):
|
|
with pytest.raises(UnknownRecipientsError) as raised:
|
|
resolve_recipients(1, [3, 2, 99])
|
|
|
|
assert raised.value.user_ids == {2, 99}
|
|
assert str(raised.value) == "No enabled user with id 2, 99"
|
|
|
|
|
|
class TestSend:
|
|
async def test_names_the_sender_to_everyone_else(self, emit, add_notifications):
|
|
await send(
|
|
1,
|
|
[1, 3],
|
|
"custom",
|
|
NotificationLevel.INFO,
|
|
{},
|
|
title="Maintenance",
|
|
body=None,
|
|
link="/platforms",
|
|
icon=None,
|
|
)
|
|
|
|
rows = add_notifications.call_args.args[0]
|
|
assert [(n.user_id, n.actor_id) for n in rows] == [(1, None), (3, 1)]
|
|
assert {n.link for n in rows} == {"/platforms"}
|
|
assert emit.await_count == 2
|
|
|
|
async def test_a_storage_failure_reaches_the_caller(self, mocker, emit):
|
|
mocker.patch.object(
|
|
db_notification_handler,
|
|
"add_notifications",
|
|
side_effect=RuntimeError("database gone"),
|
|
)
|
|
|
|
with pytest.raises(RuntimeError):
|
|
await send(
|
|
1,
|
|
[3],
|
|
"custom",
|
|
NotificationLevel.INFO,
|
|
{},
|
|
title="Maintenance",
|
|
body=None,
|
|
link=None,
|
|
icon=None,
|
|
)
|