Files
romm/backend/tests/endpoints/test_config.py
Georges-Antoine Assi 2a65371a64 fix: only return config parse error to authenticated users
The raw PyYAML parser error embeds the config file path and line context.
The /config endpoint is unauthenticated, so gate CONFIG_FILE_PARSE_ERROR
behind auth like EJS_NETPLAY_ICE_SERVERS, returning None to anonymous callers.

Generated-By: PostHog Code
Task-Id: 3d4f28a8-5157-4ca4-97a6-f283f5d6329e
2026-07-09 20:18:12 -04:00

90 lines
3.3 KiB
Python

from unittest.mock import patch
from fastapi import status
from config.config_manager import (
DEFAULT_EXCLUDED_DIRS,
DEFAULT_EXCLUDED_EXTENSIONS,
DEFAULT_EXCLUDED_FILES,
)
from config.config_manager import config_manager as cm
def test_config(client):
response = client.get("/api/config")
assert response.status_code == status.HTTP_200_OK
config = response.json()
assert config.get("EXCLUDED_PLATFORMS") == sorted(DEFAULT_EXCLUDED_DIRS)
assert config.get("EXCLUDED_SINGLE_EXT") == sorted(
e.lower() for e in DEFAULT_EXCLUDED_EXTENSIONS
)
assert config.get("EXCLUDED_SINGLE_FILES") == sorted(DEFAULT_EXCLUDED_FILES)
assert config.get("EXCLUDED_MULTI_FILES") == sorted(DEFAULT_EXCLUDED_DIRS)
assert config.get("EXCLUDED_MULTI_PARTS_EXT") == sorted(
e.lower() for e in DEFAULT_EXCLUDED_EXTENSIONS
)
assert config.get("EXCLUDED_MULTI_PARTS_FILES") == sorted(DEFAULT_EXCLUDED_FILES)
assert config.get("PLATFORMS_BINDING") == {}
assert not config.get("SKIP_HASH_CALCULATION")
assert config.get("GAMELIST_MEDIA_THUMBNAIL") == "box2d"
assert config.get("GAMELIST_MEDIA_IMAGE") == "screenshot"
def test_config_parse_error_gated_by_auth(client, access_token: str):
# The raw parser error can leak the config file path, so it is only
# returned to authenticated users.
cfg = cm.get_config()
cfg.CONFIG_FILE_PARSE_ERROR = 'in "/data/config.yml", line 2'
with patch.object(cm, "get_config", return_value=cfg):
anon = client.get("/api/config")
assert anon.status_code == status.HTTP_200_OK
assert anon.json().get("CONFIG_FILE_PARSE_ERROR") is None
auth = client.get(
"/api/config",
headers={"Authorization": f"Bearer {access_token}"},
)
assert auth.status_code == status.HTTP_200_OK
assert (
auth.json().get("CONFIG_FILE_PARSE_ERROR")
== 'in "/data/config.yml", line 2'
)
def test_add_platform_binding_payload_shape(client, access_token: str):
with patch.object(cm, "add_platform_binding") as add_platform_binding:
response = client.post(
"/api/config/system/platforms",
headers={"Authorization": f"Bearer {access_token}"},
json={"fs_slug": "n64", "slug": "nintendo-64"},
)
assert response.status_code == status.HTTP_200_OK
add_platform_binding.assert_called_once_with("n64", "nintendo-64")
def test_add_platform_version_payload_shape(client, access_token: str):
with patch.object(cm, "add_platform_version") as add_platform_version:
response = client.post(
"/api/config/system/versions",
headers={"Authorization": f"Bearer {access_token}"},
json={"fs_slug": "n64", "slug": "1.0"},
)
assert response.status_code == status.HTTP_200_OK
add_platform_version.assert_called_once_with("n64", "1.0")
def test_add_exclusion_payload_shape(client, access_token: str):
with patch.object(cm, "add_exclusion") as add_exclusion:
response = client.post(
"/api/config/exclude",
headers={"Authorization": f"Bearer {access_token}"},
json={"exclusion_type": "single_files", "exclusion_value": "README.txt"},
)
assert response.status_code == status.HTTP_200_OK
add_exclusion.assert_called_once_with("single_files", "README.txt")