重构:完成架构一致性整改与生命周期收口

This commit is contained in:
Christ committed 2026-08-18 14:05:08 +08:00
1 parent 55904cc409
commit 9eb6be2875
344 files changed
+15961 -13926

No files matched your search

+36 -36
View File
@@ -11,37 +11,37 @@ import (
"xianyu-go/internal/db"
)
// newAuth 负责newAuth相关处理。
// newAuth 封装newAuth业务协调。
func newAuth(t *testing.T) (*Service, func()) {
t.Helper()
// dbPath 保存db路径,供当前处理流程使用
// dbPath 用于本次流程后续判断的db路径
dbPath := filepath.Join(t.TempDir(), "test.db")
// d、err 保存d、err,供当前处理流程使用
// d、err 用于本次流程后续判断的d、err
d, _, err := db.Open(context.Background(), dbPath)
if err != nil {
t.Fatalf("Open: %v", err)
}
// store 保存store,供当前处理流程使用
// store 用于本次流程后续判断的store
store := db.NewStore(d, db.DialectSQLite)
// 初始化 admin。
if ok, _ := store.Users.Create(context.Background(), "admin", "[email protected]", "pw"); !ok {
t.Fatal("create admin")
}
store.Users.SetAdmin(context.Background(), "admin")
// svc 保存svc,供当前处理流程使用
// svc 用于本次流程后续判断的svc
svc := &Service{Store: store, Logger: testLogger()}
return svc, func() { d.Close() }
}
// testLogger 负责testLogger相关处理。
// testLogger 封装testLogger业务协调。
func testLogger() *slog.Logger { return slog.Default() }
// TestLoginAndMiddleware 负责Test登录AndMiddleware相关处理。
// TestLoginAndMiddleware 封装Test登录AndMiddleware业务协调。
func TestLoginAndMiddleware(t *testing.T) {
// svc、cleanup 保存svc、cleanup,供当前处理流程使用
// svc、cleanup 用于本次流程后续判断的svc、cleanup
svc, cleanup := newAuth(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
// ctx 用于本次流程后续判断的ctx
ctx := context.Background()
// 登录。
@@ -57,7 +57,7 @@ func TestLoginAndMiddleware(t *testing.T) {
// 中间件解析会话。
chain := svc.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// sess 保存sess,供当前处理流程使用
// sess 用于本次流程后续判断的sess
sess := SessionFromContext(r.Context())
if sess == nil || !sess.IsAdmin {
t.Errorf("中间件应解析出管理员会话,got=%v", sess)
@@ -65,10 +65,10 @@ func TestLoginAndMiddleware(t *testing.T) {
w.WriteHeader(200)
}))
// req 保存req,供当前处理流程使用
// req 用于本次流程后续判断的req
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.AddCookie(&http.Cookie{Name: CookieName, Value: sid})
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
chain.ServeHTTP(rec, req)
if rec.Code != 200 {
@@ -96,24 +96,24 @@ func TestIdentityFromContextReturnsNilWithoutSession(t *testing.T) {
}
}
// TestRequireAuthAndAdmin 负责TestRequireAuthAndAdmin相关处理。
// TestRequireAuthAndAdmin 封装TestRequireAuthAndAdmin业务协调。
func TestRequireAuthAndAdmin(t *testing.T) {
// svc、cleanup 保存svc、cleanup,供当前处理流程使用
// svc、cleanup 用于本次流程后续判断的svc、cleanup
svc, cleanup := newAuth(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
// ctx 用于本次流程后续判断的ctx
ctx := context.Background()
// sid 保存sid,供当前处理流程使用
// sid 用于本次流程后续判断的sid
sid, _, _ := svc.Login(ctx, "admin", "pw")
// protected 保存protected,供当前处理流程使用
// protected 用于本次流程后续判断的protected
protected := svc.Middleware(RequireAuth(RequireAdmin(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))))
// 无 cookie → 401。
req := httptest.NewRequest(http.MethodGet, "/", nil)
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
protected.ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
@@ -123,7 +123,7 @@ func TestRequireAuthAndAdmin(t *testing.T) {
// 有 admin cookie → 200。
req2 := httptest.NewRequest(http.MethodGet, "/", nil)
req2.AddCookie(&http.Cookie{Name: CookieName, Value: sid})
// rec2 保存rec2,供当前处理流程使用
// rec2 用于本次流程后续判断的rec2
rec2 := httptest.NewRecorder()
protected.ServeHTTP(rec2, req2)
if rec2.Code != 200 {
@@ -131,16 +131,16 @@ func TestRequireAuthAndAdmin(t *testing.T) {
}
}
// TestSetAndClearCookie 负责TestSetAndClear登录凭证相关处理。
// TestSetAndClearCookie 封装TestSetAndClear登录凭证业务协调。
func TestSetAndClearCookie(t *testing.T) {
// svc、cleanup 保存svc、cleanup,供当前处理流程使用
// svc、cleanup 用于本次流程后续判断的svc、cleanup
svc, cleanup := newAuth(t)
defer cleanup()
// rec 保存rec,供当前处理流程使用
// rec 用于本次流程后续判断的rec
rec := httptest.NewRecorder()
svc.SetSessionCookie(rec, "sid123")
// c 保存c,供当前处理流程使用
// c 用于本次流程后续判断的c
c := rec.Result().Cookies()
if len(c) != 1 || c[0].Name != CookieName || c[0].Value != "sid123" || !c[0].HttpOnly {
t.Fatalf("SetSessionCookie 异常: %+v", c)
@@ -149,10 +149,10 @@ func TestSetAndClearCookie(t *testing.T) {
t.Fatalf("SameSite 应为 Lax,got %v", c[0].SameSite)
}
// rec2 保存rec2,供当前处理流程使用
// rec2 用于本次流程后续判断的rec2
rec2 := httptest.NewRecorder()
svc.ClearSessionCookie(rec2)
// cc 保存cc,供当前处理流程使用
// cc 用于本次流程后续判断的cc
cc := rec2.Result().Cookies()
if len(cc) != 1 || cc[0].MaxAge != -1 {
t.Fatalf("ClearSessionCookie 应 MaxAge=-1: %+v", cc)
@@ -162,9 +162,9 @@ func TestSetAndClearCookie(t *testing.T) {
// newEmptyStore 构造未初始化 admin 的测试 store,供 InitAdmin 测试。
func newEmptyStore(t *testing.T) (*db.Store, func()) {
t.Helper()
// dbPath 保存db路径,供当前处理流程使用
// dbPath 用于本次流程后续判断的db路径
dbPath := filepath.Join(t.TempDir(), "init.db")
// d、err 保存d、err,供当前处理流程使用
// d、err 用于本次流程后续判断的d、err
d, _, err := db.Open(context.Background(), dbPath)
if err != nil {
t.Fatalf("Open: %v", err)
@@ -174,10 +174,10 @@ func newEmptyStore(t *testing.T) (*db.Store, func()) {
// TestInitAdmin_CreateThenReset 全新库创建 admin,二次调用走重置路径。
func TestInitAdmin_CreateThenReset(t *testing.T) {
// store、cleanup 保存store、cleanup,供当前处理流程使用
// store、cleanup 用于本次流程后续判断的store、cleanup
store, cleanup := newEmptyStore(t)
defer cleanup()
// ctx 保存ctx,供当前处理流程使用
// ctx 用于本次流程后续判断的ctx
ctx := context.Background()
// 全新库 → 创建。
@@ -185,7 +185,7 @@ func TestInitAdmin_CreateThenReset(t *testing.T) {
if err != nil || !created {
t.Fatalf("首次 InitAdmin 应创建:created=%v err=%v", created, err)
}
// admin 保存admin,供当前处理流程使用
// admin 用于本次流程后续判断的admin
admin, _ := store.Users.GetByUsername(ctx, "admin")
if admin == nil || !admin.IsAdmin {
t.Fatalf("admin 未正确创建/标记: %+v", admin)
@@ -200,11 +200,11 @@ func TestInitAdmin_CreateThenReset(t *testing.T) {
if err != nil || created2 {
t.Fatalf("二次 InitAdmin 应重置:created=%v err=%v", created2, err)
}
if // ok 保存ok,供当前处理流程使用
if // ok 用于本次流程后续判断的ok
_, ok, _ := store.Users.VerifyAndUpgrade(ctx, "admin", "pw1"); ok {
t.Fatal("旧密码 pw1 应已失效")
}
if // ok 保存ok,供当前处理流程使用
if // ok 用于本次流程后续判断的ok
_, ok, _ := store.Users.VerifyAndUpgrade(ctx, "admin", "pw2"); !ok {
t.Fatal("新密码 pw2 应可用")
}
@@ -212,16 +212,16 @@ func TestInitAdmin_CreateThenReset(t *testing.T) {
// TestInitAdmin_DoesNotValidatePassword 密码强度校验由调用方负责(ensureAdmin),
// InitAdmin 本身不拒绝空密码,仅保证不 panic 且能完成创建。
// TestInitAdmin_DoesNotValidatePassword 负责TestInitAdminDoesNotValidate密码相关处理。
// TestInitAdmin_DoesNotValidatePassword 封装TestInitAdminDoesNotValidate密码业务协调。
func TestInitAdmin_DoesNotValidatePassword(t *testing.T) {
// store、cleanup 保存store、cleanup,供当前处理流程使用
// store、cleanup 用于本次流程后续判断的store、cleanup
store, cleanup := newEmptyStore(t)
defer cleanup()
if // err 保存err,供当前处理流程使用
if // err 用于本次流程后续判断的err
_, err := InitAdmin(context.Background(), store, "[email protected]", ""); err != nil {
t.Fatalf("InitAdmin 不应自行拒绝空密码: %v", err)
}
if // admin 保存admin,供当前处理流程使用
if // admin 用于本次流程后续判断的admin
admin, _ := store.Users.GetByUsername(context.Background(), "admin"); admin == nil {
t.Fatal("空密码也应创建 admin")
}