mirror of
https://github.com/rommapp/romm.git
synced 2026-12-05 23:44:11 +01:00
fix(audit): report only the rom fields a user edited
A save turns null cover and manual urls into empty strings, and a rename re-derives the sort name; neither is an edit of its own. Also fixes the test helpers the first local run caught. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -298,7 +298,6 @@ MATCH_ID_FIELDS: Final = tuple(
|
||||
# What an edit reports as changed, each read off one or more columns.
|
||||
_EDIT_AUDIT_FIELDS: Final[dict[str, tuple[str, ...]]] = {
|
||||
"name": ("name",),
|
||||
"sort_name": ("name_sort_key",),
|
||||
"fs_name": ("fs_name",),
|
||||
"summary": ("summary",),
|
||||
"cover": ("url_cover", "path_cover_l"),
|
||||
@@ -313,10 +312,13 @@ def _record_rom_update(request: Request, before: Rom, after: Rom) -> None:
|
||||
for f in MATCH_ID_FIELDS
|
||||
if getattr(before, f) != getattr(after, f)
|
||||
}
|
||||
# A save writes "" over a column that was null, which is no change.
|
||||
changed = [
|
||||
label
|
||||
for label, columns in _EDIT_AUDIT_FIELDS.items()
|
||||
if any(getattr(before, c) != getattr(after, c) for c in columns)
|
||||
if any(
|
||||
(getattr(before, c) or None) != (getattr(after, c) or None) for c in columns
|
||||
)
|
||||
]
|
||||
if not providers and not changed:
|
||||
return
|
||||
|
||||
@@ -14,18 +14,18 @@ def _add(
|
||||
minutes_ago: int = 0,
|
||||
**overrides,
|
||||
) -> AuditEvent:
|
||||
event = AuditEvent(
|
||||
occurred_at=datetime.now(timezone.utc) - timedelta(minutes=minutes_ago),
|
||||
actor_kind=AuditActorKind.USER if actor else AuditActorKind.SYSTEM,
|
||||
actor_id=actor.id if actor else None,
|
||||
actor_name=actor.username if actor else None,
|
||||
action=action,
|
||||
target_type="rom",
|
||||
target_id="1",
|
||||
target_name="Super Mario Bros.",
|
||||
data={},
|
||||
**overrides,
|
||||
)
|
||||
fields = {
|
||||
"occurred_at": datetime.now(timezone.utc) - timedelta(minutes=minutes_ago),
|
||||
"actor_kind": AuditActorKind.USER if actor else AuditActorKind.SYSTEM,
|
||||
"actor_id": actor.id if actor else None,
|
||||
"actor_name": actor.username if actor else None,
|
||||
"action": action,
|
||||
"target_type": "rom",
|
||||
"target_id": "1",
|
||||
"target_name": "Super Mario Bros.",
|
||||
"data": {},
|
||||
}
|
||||
event = AuditEvent(**{**fields, **overrides})
|
||||
db_audit_event_handler.add_events([event])
|
||||
return event
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import base64
|
||||
import uuid
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
@@ -160,11 +161,13 @@ class TestClientTokens:
|
||||
|
||||
|
||||
def test_a_new_permission_group_is_recorded(client: TestClient, access_token: str):
|
||||
# Groups outlive a test, so the name has to be one no earlier run left behind.
|
||||
name = f"Kids {uuid.uuid4().hex[:8]}"
|
||||
client.post(
|
||||
"/api/permissions/groups",
|
||||
json={"name": "Kids", "grants": []},
|
||||
json={"name": name, "grants": []},
|
||||
headers=_auth(access_token),
|
||||
)
|
||||
|
||||
[event] = _events()
|
||||
assert (event.action, event.target_name) == ("permission_group.create", "Kids")
|
||||
assert (event.action, event.target_name) == ("permission_group.create", name)
|
||||
|
||||
@@ -20,6 +20,7 @@ from handler.database.audit_events_handler import AuditEventFilters
|
||||
from handler.redis_handler import sync_cache
|
||||
from models.audit_event import AuditAction, AuditActorKind, AuditEvent, AuditTargetType
|
||||
from models.user import User
|
||||
from utils.datetime import to_utc
|
||||
|
||||
ROM = AuditTarget(AuditTargetType.ROM, 12, "Metroid")
|
||||
|
||||
@@ -156,7 +157,7 @@ class TestRecord:
|
||||
)
|
||||
|
||||
[event] = _events()
|
||||
assert event.occurred_at > now - timedelta(days=2)
|
||||
assert to_utc(event.occurred_at) > now - timedelta(days=2)
|
||||
|
||||
def test_for_user_id_without_a_user_is_the_system(self):
|
||||
assert AuditActor.for_user_id(None) == audit_handler.SYSTEM_ACTOR
|
||||
|
||||
Reference in New Issue
Block a user