Files
romm/backend/tests/endpoints/roms/test_notes.py
Sam Dornan 768de53b14 fix(roms): read note authors off the joined user (#4588)
`UserNoteSchema.username` was validated straight off the `RomNote` row, which
has no such column, so `GET /roms/{id}/notes` raised a ValidationError on any
rom carrying at least one note. The create/update routes dodged it by filling
the author fields from the request user.

Move the mapping into `UserNoteSchema.from_rom_note`, which takes the author
off the `user` relationship the note query already joins, and reuse it for the
list route and `DetailedRomSchema` (where the same mapping was inlined).

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-17 11:13:40 -05:00

42 lines
1.4 KiB
Python

"""Author identity on the note responses, which no note row stores itself."""
from fastapi import status
from fastapi.testclient import TestClient
from models.rom import Rom
from models.user import User
def _auth(token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {token}"}
def test_note_responses_carry_author_identity(
client: TestClient, access_token: str, rom: Rom, admin_user: User
):
created = client.post(
f"/api/roms/{rom.id}/notes",
headers=_auth(access_token),
json={
"title": "Route order",
"content": "Third phase first",
"is_public": True,
},
)
assert created.status_code == status.HTTP_200_OK
listed = client.get(f"/api/roms/{rom.id}/notes", headers=_auth(access_token))
assert listed.status_code == status.HTTP_200_OK
notes = listed.json()
assert [n["id"] for n in notes] == [created.json()["id"]]
assert notes[0]["username"] == admin_user.username
assert notes[0]["user_avatar_path"] == admin_user.avatar_path
assert notes[0]["user_updated_at"] is not None
detailed = client.get(f"/api/roms/{rom.id}", headers=_auth(access_token))
assert detailed.status_code == status.HTTP_200_OK
all_user_notes = detailed.json()["all_user_notes"]
assert [n["id"] for n in all_user_notes] == [created.json()["id"]]
assert all_user_notes[0]["username"] == admin_user.username