Files
Havline/internal/auth/prune_test.go
T
mrleeandDevin 9d75fb1d3e
CI / verify (push) Canceled after 0s
fix: 收敛正确性缺口并补齐质量门禁
备份、隧道生命周期、日志、会话与设置项存在一批「静默失效」问题,本次一并收敛。

正确性:
- 备份归档补上 session_secret 与 frp 配置。缺密钥时换机恢复后库里所有凭据都解不开,
  界面表现为「配置都在、一执行就报解密失败」;同时支持口令加密(scrypt + AES-256-GCM)。
- 修复「立即备份」必失败:tar 头用的是遍历时的文件尺寸,而 logs、frpc 配置在打包期间
  还在增长,读出的字节多于声明值导致 archive/tar 报错、整份备份作废。改为打开后重新
  取尺寸并用 LimitReader 兜住,被截短时补零保持条目结构完整。
- 数据库改用 VACUUM INTO 生成一致性快照。直接打包正在写入的 SQLite 文件可能得到
  结构完整但内容自相矛盾的副本,恢复后才暴露。
- Cloudflare 启动改用 StartAuto,Shutdown 显式 StopAll,子进程绑定应用 ctx。此前
  重启会把用户手动停掉的隧道重新拉起,退出也可能留下孤儿进程。
- Nginx 访问/错误日志加轮转(200MB 阈值、gzip 归档、USR1 重开),读取改为从文件尾
  分块倒读,并跨当日归档统计。此前日志只增不减,读接口每次全量扫描。
- 过期会话改由日任务清理;删除反代规则时同步清掉流量累计,避免同名规则重建继承旧读数。
- 启动加就绪门:Nginx bootstrap 完成后再放行 FRP 对账、Agent 隧道与 Cloudflare 自启。

设置项:
- 新增设置项注册表(internal/settings/registry.go),白名单与默认值从它派生,
  并记录每个键的消费者,防止再出现「界面能改、后端没人读」的黑洞设置。
- 接上三个此前不生效的设置项:timezone(启动与保存时应用到 time.Local)、
  theme(后端为权威源,挂载时覆盖本地偏好、顶栏切换回写)、log_retention_days
  (控制日志归档保留天数)。

工程质量:
- gofmt 全量格式化 23 个历史文件,CI 增加格式化门禁。
- 前端引入 ESLint(flat config)与 lint 脚本,CI 增加 lint 步骤;顺带修掉
  logDisplay 的 let→const 与 LogsView 模板内直接改 prop 两处问题。
- 删除无引用的 pnpm-lock.yaml,prototype/ 加入忽略列表。
- 打包版本对齐:Docker manifest 同步到 2.0.0,CI 校验 VERSION 与 package.json 一致。

验证:gofmt 0 文件、go vet、go test ./...、npm run lint/typecheck/test/build 全绿。

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-24 16:30:00 +08:00

88 lines
2.5 KiB
Go

package auth
import (
"context"
"database/sql"
"path/filepath"
"testing"
"time"
"github.com/havline/havline/internal/db"
)
// TestPruneExpiredSessions 过期会话应被日任务清掉,未过期的必须留着。
func TestPruneExpiredSessions(t *testing.T) {
conn, err := db.Open(filepath.Join(t.TempDir(), "havline.db"))
if err != nil {
t.Fatalf("open db: %v", err)
}
defer conn.Close()
ctx := context.Background()
if err := db.Migrate(ctx, conn, filepath.Join("..", "..", "migrations")); err != nil {
t.Fatalf("migrate: %v", err)
}
svc := New(conn)
if err := svc.Setup(ctx, "admin", "admin-password-123"); err != nil {
t.Fatalf("setup: %v", err)
}
adminID, err := svc.AdminIDForSession(ctx, mustLogin(t, svc))
if err != nil {
t.Fatalf("admin id: %v", err)
}
now := time.Now().UTC()
expired := []string{"expired-1", "expired-2"}
for _, id := range expired {
if _, err := conn.ExecContext(ctx,
`INSERT INTO sessions(id, admin_id, expires_at) VALUES (?, ?, ?)`,
id, adminID, now.Add(-time.Hour).Format(time.RFC3339)); err != nil {
t.Fatalf("insert expired session: %v", err)
}
}
future := "still-valid"
if _, err := conn.ExecContext(ctx,
`INSERT INTO sessions(id, admin_id, expires_at) VALUES (?, ?, ?)`,
future, adminID, now.Add(time.Hour).Format(time.RFC3339)); err != nil {
t.Fatalf("insert valid session: %v", err)
}
before := countSessions(t, conn)
removed, err := svc.PruneExpiredSessions(ctx)
if err != nil {
t.Fatalf("prune: %v", err)
}
if removed != int64(len(expired)) {
t.Fatalf("应删除 %d 条过期会话,实际 %d", len(expired), removed)
}
if after := countSessions(t, conn); after != before-int64(len(expired)) {
t.Fatalf("会话数不符:before=%d after=%d", before, after)
}
if err := svc.ValidateSession(ctx, future); err != nil {
t.Fatalf("未过期会话不应被删:%v", err)
}
// 再跑一次应当没有可删的(幂等)
if removed, err = svc.PruneExpiredSessions(ctx); err != nil || removed != 0 {
t.Fatalf("重复清理应为空操作:removed=%d err=%v", removed, err)
}
}
func mustLogin(t *testing.T, svc *Service) string {
t.Helper()
sessionID, err := svc.Login(context.Background(), "admin", "admin-password-123")
if err != nil {
t.Fatalf("login: %v", err)
}
return sessionID
}
func countSessions(t *testing.T, conn *sql.DB) int64 {
t.Helper()
var count int64
if err := conn.QueryRowContext(context.Background(), `SELECT COUNT(1) FROM sessions`).Scan(&count); err != nil {
t.Fatalf("count sessions: %v", err)
}
return count
}