Files
outlookEmailPlus/tests/_import_app.py
T
胡绍康andClaude Opus 4.6 0eb36f5175 style: 运行 Black 格式化所有 Python 代码
- 格式化 66 个 Python 文件以符合 Black 代码风格
- 修复 GitHub Actions Code Quality 工作流失败问题

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 18:23:57 +08:00

47 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import importlib
import os
import sys
import tempfile
from pathlib import Path
_TEMP_DIR = tempfile.TemporaryDirectory(prefix="outlookEmail-tests-")
_DB_PATH = Path(_TEMP_DIR.name) / "test.db"
def import_web_app_module():
"""
以"可测试"的方式导入 web_outlook_app:
- 注入必要环境变量(SECRET_KEY / DATABASE_PATH 等)
- 禁用调度器自启动,避免测试期间启动后台线程
- 将 DB 指向临时文件,避免污染本地 data/
"""
os.environ["SECRET_KEY"] = "test-secret-key-32bytes-minimum-0000000000000000"
os.environ["LOGIN_PASSWORD"] = "testpass123" # >= 8
os.environ["SCHEDULER_AUTOSTART"] = "false"
os.environ["DATABASE_PATH"] = str(_DB_PATH)
if "web_outlook_app" in sys.modules:
return sys.modules["web_outlook_app"]
module = importlib.import_module("web_outlook_app")
# 测试配置:禁用 CSRF(避免额外 token 依赖),开启 TESTING
module.app.config.update(
TESTING=True,
WTF_CSRF_ENABLED=False,
WTF_CSRF_CHECK_DEFAULT=False,
)
return module
def clear_login_attempts():
"""清理登录限制记录,避免测试间互相影响"""
from outlook_web.db import get_db
try:
db = get_db()
db.execute("DELETE FROM login_attempts")
db.commit()
except Exception:
pass