Files
romm/backend/tests/endpoints/test_export.py
Georges-Antoine Assi cca9f500f1 fix(auth): mask hidden platforms from the export endpoints
Both routes passed every caller-supplied platform_id straight to the
exporter. A permission group can grant platforms.write while hiding
individual platforms, so such a caller could export a platform hidden
from them -- reading its metadata and rewriting its gamelist.xml or
metadata.pegasus.txt.

Resolve and check every platform up front, the way update_platform and
delete_platform already do, so a rejected request writes nothing at all
rather than failing part way through a batch. A missing platform 404s
like a hidden one, so the response is not an oracle for whether the
platform exists.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-06 11:40:00 -04:00

81 lines
2.7 KiB
Python

from unittest.mock import AsyncMock, patch
import pytest
from fastapi import status
from utils.gamelist_exporter import GamelistExporter
from utils.pegasus_exporter import PegasusExporter
EXPORTERS = {
"/api/export/gamelist-xml": GamelistExporter,
"/api/export/pegasus": PegasusExporter,
}
with_endpoint = pytest.mark.parametrize("path", list(EXPORTERS))
@with_endpoint
def test_export_rejects_viewer(client, viewer_access_token: str, platform, path: str):
# Both endpoints write into the library, so reading ROMs is not enough.
response = client.post(
f"{path}?platform_ids={platform.id}",
headers={"Authorization": f"Bearer {viewer_access_token}"},
)
assert response.status_code == status.HTTP_403_FORBIDDEN
@with_endpoint
def test_export_rejects_anonymous(client, platform, path: str):
response = client.post(f"{path}?platform_ids={platform.id}")
assert response.status_code == status.HTTP_401_UNAUTHORIZED
@with_endpoint
def test_export_allows_editor(client, editor_access_token: str, platform, path: str):
with patch.object(
EXPORTERS[path],
"export_platform_to_file",
new_callable=AsyncMock,
return_value=True,
) as export_mock:
response = client.post(
f"{path}?platform_ids={platform.id}",
headers={"Authorization": f"Bearer {editor_access_token}"},
)
assert response.status_code == status.HTTP_200_OK
export_mock.assert_awaited_once()
@with_endpoint
def test_export_unknown_platform_is_404(client, editor_access_token: str, path: str):
# Matches the 404 a hidden platform gets, so the response is not an oracle
# for whether the platform exists (see test_permissions_visibility).
with patch.object(
EXPORTERS[path], "export_platform_to_file", new_callable=AsyncMock
) as export_mock:
response = client.post(
f"{path}?platform_ids=99999",
headers={"Authorization": f"Bearer {editor_access_token}"},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
export_mock.assert_not_awaited()
@with_endpoint
def test_export_rejects_batch_with_one_unknown_platform(
client, editor_access_token: str, platform, path: str
):
# The whole request is refused up front, so a partial batch never writes.
with patch.object(
EXPORTERS[path], "export_platform_to_file", new_callable=AsyncMock
) as export_mock:
response = client.post(
f"{path}?platform_ids={platform.id}&platform_ids=99999",
headers={"Authorization": f"Bearer {editor_access_token}"},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
export_mock.assert_not_awaited()