feat: 整合官方 2.7.0 安全修复与本地文件分享

This commit is contained in:
orion committed 2026-09-18 10:08:42 +08:00
1 parent b10113e5ab
commit 9e9571b031
42 files changed
+2824 -268

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
"""Shared test helpers: settings-override mixin and in-memory Tortoise setup.
Kept unittest-friendly (the legacy suite is unittest-based); pytest fixtures
live in conftest.py and build on top of these helpers.
"""
from tortoise import Tortoise
from core.settings import settings
# Mirrors core.database.get_db_config() but with an in-memory SQLite database
# and no WAL/startup-lock specifics, so tests never touch the real data dir's DB.
MEMORY_DB_CONFIG = {
"connections": {
"default": {
"engine": "tortoise.backends.sqlite",
"credentials": {"file_path": ":memory:"},
}
},
"apps": {
"models": {
"models": ["apps.base.models"],
"default_connection": "default",
}
},
"use_tz": False,
"timezone": "Asia/Shanghai",
}
class SettingsOverrideMixin:
"""Snapshot settings.user_config in setUp and restore it in tearDown."""
def setUp(self):
self._original_user_config = dict(settings.user_config)
def tearDown(self):
settings.user_config = self._original_user_config
async def init_memory_db():
"""Init Tortoise against an in-memory DB and create all schemas."""
# 每个测试都是全新 DB,必须同步失效进程级配置 TTL 缓存,
# 否则上一个测试的缓存会让 middleware 误判初始化状态。
import core.config as config_module
import asyncio
import apps.admin.services as services_module
services_module.keyvalue_write_lock = asyncio.Lock()
config_module._config_cached_until = 0.0
await Tortoise.init(config=MEMORY_DB_CONFIG)
await Tortoise.generate_schemas()
async def close_db():
await Tortoise.close_connections()