Files
Blizzard bff6e5c7fd test(gateway): 补上「钱路径」与租户隔离的回归测试 —— P0 最高优先级缺口
完成度审计(见记忆 completion-audit)最尖的一条:涉及钱的路径此前 0 测试,
支付一上线 bug=真实错账;租户数据层隔离也只测了角色门禁没测数据层真隔离。
把本会话 live 手验过的断言固化成回归测试。

测试基建:纯 Go sqlite(glebarez,无 CGO)内存库,迁同款模型+建部分唯一索引+
挂租户作用域回调,复用生产 store 方法测真逻辑。CI ubuntu 无 Postgres 也能跑
(此前 store 测试全是纯逻辑,DB 事务逻辑从没进过关卡)。

钱路径不变量(6):
- GrantCredits 记分录+增余额,余额恒等于 SUM(ledger)
- 兑换码核销一次性(CAS)+原子入账,重复核销余额纹丝不动
- 账本(kind,ref)部分唯一索引:重复 grant 被兜底拦下、usage 不受约束
- MarkOrderPaid CAS 幂等:重复回调 changed=false 不重复入账
- SaveUsageEvent task_id 幂等:同任务重投不重复扣费
- ReconcileOrders 抓出 order_without_ledger(钱到了积分没给)

租户隔离(3):创建自动填 tenant_id、查询按 ctx 租户过滤、跨租户改/删命不中、
WithoutTenant 系统视角全可见。

go build/vet + 全 gateway 测试全绿;sqlite 仅测试引用,不进生产二进制。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-18 11:39:36 +08:00

97 lines
3.7 KiB
Go

package store
import (
"context"
"testing"
)
// 租户数据层隔离(P0-3):tenant_scope 插件是多租户的命根,此前只测了角色门禁、
// 没测数据层是否真隔离。一个回归就可能串租户数据——SaaS 里这是致命的。
// 用租户作用域模型 KB 验证:创建自动填 tenant_id + 查询自动按 ctx 租户过滤 + WithoutTenant 跨租户可见。
func countKB(t *testing.T, p *Postgres, ctx context.Context) int64 {
t.Helper()
var n int64
if err := p.db.WithContext(ctx).Model(&KB{}).Count(&n).Error; err != nil {
t.Fatalf("计数失败: %v", err)
}
return n
}
func TestTenantScope_CreateAutoFillAndQueryFilter(t *testing.T) {
p := newTestStore(t)
ctxA := WithTenant(context.Background(), "tenant-A")
ctxB := WithTenant(context.Background(), "tenant-B")
// 在 A 的上下文里建库,不显式写 tenant_id —— 插件应自动填成 tenant-A。
kb := &KB{Name: "A的库", Owner: "u1", Kind: "general"}
if err := p.db.WithContext(ctxA).Create(kb).Error; err != nil {
t.Fatalf("建库失败: %v", err)
}
var got KB
p.db.WithContext(WithoutTenant(context.Background())).First(&got, "id = ?", kb.ID)
if got.TenantID != "tenant-A" {
t.Fatalf("创建应自动填 tenant_id=tenant-A,实得 %q", got.TenantID)
}
// B 的上下文查不到 A 的库(隔离)。
if n := countKB(t, p, ctxB); n != 0 {
t.Fatalf("tenant-B 不该看到 tenant-A 的库,却查到 %d 条", n)
}
// A 的上下文能查到自己的。
if n := countKB(t, p, ctxA); n != 1 {
t.Fatalf("tenant-A 应看到自己 1 条库,实得 %d", n)
}
}
func TestTenantScope_CrossTenantLeakGuard(t *testing.T) {
p := newTestStore(t)
ctxA := WithTenant(context.Background(), "tenant-A")
ctxB := WithTenant(context.Background(), "tenant-B")
p.db.WithContext(ctxA).Create(&KB{Name: "A1", Kind: "general"})
p.db.WithContext(ctxA).Create(&KB{Name: "A2", Kind: "general"})
p.db.WithContext(ctxB).Create(&KB{Name: "B1", Kind: "general"})
if n := countKB(t, p, ctxA); n != 2 {
t.Fatalf("A 应见 2 条,实得 %d", n)
}
if n := countKB(t, p, ctxB); n != 1 {
t.Fatalf("B 应见 1 条,实得 %d", n)
}
// WithoutTenant(系统/admin 聚合口径)应看到全部 3 条。
if n := countKB(t, p, WithoutTenant(context.Background())); n != 3 {
t.Fatalf("WithoutTenant 应见全部 3 条,实得 %d", n)
}
// 无 tenant 上下文(既非 WithTenant 也非 WithoutTenant):插件不过滤,等同系统视角
// —— 这是设计约定(回填/未登录路径),用户面由中间件保证必有 tenant。
if n := countKB(t, p, context.Background()); n != 3 {
t.Fatalf("裸 ctx 不过滤应见全部 3 条,实得 %d", n)
}
}
func TestTenantScope_UpdateAndDeleteScoped(t *testing.T) {
p := newTestStore(t)
ctxA := WithTenant(context.Background(), "tenant-A")
ctxB := WithTenant(context.Background(), "tenant-B")
a := &KB{Name: "A的库", Kind: "general"}
p.db.WithContext(ctxA).Create(a)
// B 的上下文尝试改 A 的库 —— 插件按 tenant-B 过滤,命不中,改不动(防越权写他租)。
res := p.db.WithContext(ctxB).Model(&KB{}).Where("id = ?", a.ID).Update("name", "被B改了")
if res.RowsAffected != 0 {
t.Fatalf("B 不该能改 A 的库,却影响了 %d 行", res.RowsAffected)
}
// B 的上下文删 A 的库 —— 同样命不中。
res = p.db.WithContext(ctxB).Where("id = ?", a.ID).Delete(&KB{})
if res.RowsAffected != 0 {
t.Fatalf("B 不该能删 A 的库,却删了 %d 行", res.RowsAffected)
}
// A 自己能改。
res = p.db.WithContext(ctxA).Model(&KB{}).Where("id = ?", a.ID).Update("name", "A自己改")
if res.RowsAffected != 1 {
t.Fatalf("A 应能改自己的库,实影响 %d 行", res.RowsAffected)
}
}