Files

54 lines
2.1 KiB
Python
Raw Permalink Normal View History

2026-06-05 11:37:44 +08:00
import asyncio
import unittest
from fastapi import HTTPException
2026-06-16 00:49:43 +08:00
from core.settings import BASE_DIR, settings
from apps.base.pages import index, resolve_theme_file
from tests.helpers import SettingsOverrideMixin
2026-06-05 11:37:44 +08:00
# themes/ is produced by the Dockerfile frontend build (see .gitignore); the test
# switches between both bundled themes, so it needs both to exist locally.
THEMES_BUILT = all(
BASE_DIR.joinpath(f"themes/{year}/assets").is_dir() for year in ("2023", "2024")
)
2026-06-05 11:37:44 +08:00
class ThemeAssetTests(SettingsOverrideMixin, unittest.TestCase):
2026-06-16 00:49:43 +08:00
def get_theme_index_asset(self, theme: str) -> str:
assets = sorted((BASE_DIR / theme / "assets").glob("index-*.js"))
self.assertTrue(assets, f"{theme} 缺少 index JS 资源")
return assets[0].name
@unittest.skipUnless(THEMES_BUILT, "themes/ exists only after the Docker frontend build; skip in bare checkouts")
2026-06-05 11:37:44 +08:00
def test_resolves_assets_from_current_theme(self):
settings.themes_select = "themes/2023"
2026-06-16 00:49:43 +08:00
theme_2023_asset = resolve_theme_file(
"assets", self.get_theme_index_asset("themes/2023")
)
2026-06-05 11:37:44 +08:00
settings.themes_select = "themes/2024"
2026-06-16 00:49:43 +08:00
theme_2024_asset = resolve_theme_file(
"assets", self.get_theme_index_asset("themes/2024")
)
2026-06-05 11:37:44 +08:00
self.assertIn("themes/2023/assets", str(theme_2023_asset))
self.assertIn("themes/2024/assets", str(theme_2024_asset))
def test_rejects_theme_asset_path_traversal(self):
settings.themes_select = "themes/2024"
2026-06-05 11:37:44 +08:00
with self.assertRaises(HTTPException) as error:
resolve_theme_file("assets", "..", "..", "core", "settings.py")
self.assertEqual(error.exception.status_code, 404)
@unittest.skipUnless(THEMES_BUILT, "themes/ exists only after the Docker frontend build; skip in bare checkouts")
2026-06-05 11:37:44 +08:00
def test_index_keeps_absolute_asset_urls(self):
settings.themes_select = "themes/2023"
2026-06-05 11:37:44 +08:00
response = asyncio.run(index())
html = response.body.decode("utf-8")
self.assertIn('src="/assets/', html)
self.assertIn('href="/assets/', html)