Files
filecodebox/tests/test_issue_476_theme_assets.py
T
orion 0be5a31a59 🔀 合并 FileCodeBox 2.5.4 安全更新
同步官方 2.5.4 的初始化、安全、容量控制与上传校验,并保留 2026 主题、动态资源缓存和管理后台统计能力。

- 使用原子领取和容量预留,补齐文件校验、限流、令牌轮换与路径防护
- 保留三主题构建、2026 静态资源隔离及 TransferStats 生命周期记录
- 已通过容器生产镜像构建、23 项安全与分享回归测试、Python 编译检查和 Dockerfile 检查
2026-08-07 09:09:42 +08:00

56 lines
1.8 KiB
Python

import asyncio
import unittest
from fastapi import HTTPException
from core.settings import BASE_DIR, settings
from main import index, resolve_theme_file
class SettingsOverrideMixin:
def setUp(self):
self._original_user_config = dict(settings.user_config)
def tearDown(self):
settings.user_config = self._original_user_config
class ThemeAssetTests(SettingsOverrideMixin, unittest.TestCase):
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
def test_resolves_assets_from_current_theme(self):
settings.themesSelect = "themes/2023"
theme_2023_asset = resolve_theme_file(
"assets", self.get_theme_index_asset("themes/2023")
)
settings.themesSelect = "themes/2024"
theme_2024_asset = resolve_theme_file(
"assets", self.get_theme_index_asset("themes/2024")
)
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.themesSelect = "themes/2024"
with self.assertRaises(HTTPException) as error:
resolve_theme_file("assets", "..", "..", "core", "settings.py")
self.assertEqual(error.exception.status_code, 404)
def test_index_rewrites_assets_to_theme_specific_urls(self):
settings.themesSelect = "themes/2023"
response = asyncio.run(index())
html = response.body.decode("utf-8")
self.assertIn('src="/theme-assets/', html)
self.assertIn('href="/theme-assets/', html)
self.assertNotIn('src="/assets/', html)
self.assertNotIn('href="/assets/', html)