Files
azzz 5c4b8d84ed WorkLog 全量合并:密码登录体系、每日自动备份、发信日志、活动邮件通知一键参加
- 认证:注册设密码(双次确认,>=10 位)、密码/验证码双模式登录、邮箱验证码找回重置密码
- 备份:服务内置每天 23:00 自动备份到 data/backups/(默认保留 14 份),backup_db.py 支持手动/crontab
- 发信日志:logs/mail-YYYYMM.log 记录每次发信成败与失败原因(535 认证失败/断连细分),按月滚动保留 6 个月
- 通知:发起活动群发邮件(参与不发),邮件内一键参加(一次性令牌、7 天有效、私密活动不群发)
- 前端:报错细分(字段/类型/状态码全中文)、验证码按钮 60 秒防连点
- 部署:Dockerfile + entrypoint(seed 机制)、限流/唯一索引/令牌摘要等安全加固
- 工具:repair_db/clear_db 的 SQL 全部常量化,无动态拼接
- 测试:43 项全覆盖(注册/登录/活动/通知/备份/日志/安全)
2026-10-29 19:00:48 +08:00

98 lines
3.9 KiB
Python

"""定时备份功能测试:备份内容一致性、保留策略、调度时间计算。
Run with: python -m unittest discover -s tests -p "test_backup.py" -v
"""
import shutil
import sqlite3
import tempfile
import time
import unittest
from contextlib import closing
from datetime import datetime
from pathlib import Path
from uuid import uuid4
from server.backup import _backup_once_sync, parse_time, seconds_until
def make_db(path: Path) -> None:
with closing(sqlite3.connect(str(path))) as conn:
conn.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)")
conn.executemany("INSERT INTO t (name) VALUES (?)", [(f"row{i}",) for i in range(50)])
conn.commit()
class BackupTests(unittest.TestCase):
def setUp(self):
self.directory = Path(tempfile.gettempdir()) / f"worklog-backup-{uuid4().hex}"
self.directory.mkdir()
self.db = self.directory / "worklog.db"
make_db(self.db)
def tearDown(self):
shutil.rmtree(self.directory)
def test_backup_creates_consistent_copy(self):
dest = _backup_once_sync(self.db, self.directory / "backups", keep=14)
self.assertTrue(dest.exists())
self.assertEqual(dest.parent.name, "backups")
with closing(sqlite3.connect(str(self.db))) as src:
src_count = src.execute("SELECT COUNT(*) FROM t").fetchone()[0]
with closing(sqlite3.connect(str(dest))) as copy:
copy_count = copy.execute("SELECT COUNT(*) FROM t").fetchone()[0]
self.assertEqual(copy_count, 50)
self.assertEqual(src_count, copy_count)
def test_backup_runs_while_db_in_wal_mode_with_uncommitted_wal(self):
# 模拟服务运行中:开一个 WAL 连接写入但不 checkpoint
with closing(sqlite3.connect(str(self.db))) as conn:
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("INSERT INTO t (name) VALUES ('wal-only-row')")
conn.commit()
try:
dest = _backup_once_sync(self.db, self.directory / "backups", keep=14)
finally:
pass
with closing(sqlite3.connect(str(dest))) as copy:
copy_count = copy.execute(
"SELECT COUNT(*) FROM t WHERE name='wal-only-row'"
).fetchone()[0]
self.assertEqual(copy_count, 1, "备份应包含 WAL 里未合并的数据")
def test_retention_keeps_only_latest(self):
bdir = self.directory / "backups"
for _ in range(5):
_backup_once_sync(self.db, bdir, keep=3)
time.sleep(0.05) # 同秒会加 -2/-3 序号,不会被覆盖
files = sorted(bdir.glob("worklog-*.db"))
self.assertEqual(len(files), 3, f"应只保留 3 份,实际 {len(files)}")
def test_prune_does_not_touch_foreign_files(self):
bdir = self.directory / "backups"
bdir.mkdir()
stranger = bdir / "other-20260101-000000.db"
stranger.write_bytes(b"x")
_backup_once_sync(self.db, bdir, keep=1)
self.assertTrue(stranger.exists(), "不匹配命名规则的文件不应被清理")
def test_seconds_until(self):
base = datetime(2026, 10, 27, 22, 0, 0)
self.assertEqual(seconds_until(23, 0, base), 3600.0)
# 已过目标点:算明天
late = datetime(2026, 10, 27, 23, 30, 0)
self.assertEqual(seconds_until(23, 0, late), 23.5 * 3600)
# 恰好等于目标点:算明天(避免 0 延迟重复触发)
exact = datetime(2026, 10, 27, 23, 0, 0)
self.assertEqual(seconds_until(23, 0, exact), 24 * 3600)
def test_parse_time(self):
self.assertEqual(parse_time("23:00"), (23, 0))
self.assertEqual(parse_time("0:05"), (0, 5))
self.assertEqual(parse_time("25:00"), (23, 0), "非法时间回退 23:00")
self.assertEqual(parse_time("abc"), (23, 0))
self.assertEqual(parse_time(""), (23, 0))
if __name__ == "__main__":
unittest.main()