348 lines
17 KiB
Go
348 lines
17 KiB
Go
package dobotaiops
|
||
|
||
import (
|
||
"context"
|
||
"crypto/rand"
|
||
"encoding/base64"
|
||
"errors"
|
||
"fmt"
|
||
"net/url"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/jackc/pgx/v5/pgxpool"
|
||
)
|
||
|
||
// 这些用例只在 TEST_DATABASE_URL 指向**一次性/隔离**数据库时运行(CI 默认跳过)。
|
||
// 它们**绝不**指向生产:fixture 表、隔离角色、临时 DSN 文件都在这个库内建/删。
|
||
// 生产侧的真实验证另有入口(纯目录/SELECT,见工单与 runbook)。
|
||
func fixturePool(t *testing.T) *pgxpool.Pool {
|
||
t.Helper()
|
||
// 先过隔离环境护栏(会建表/建角色):未声明就跳过,声明了但不像一次性库就直接失败。
|
||
fixtureEnvironment(t)
|
||
dsn := strings.TrimSpace(os.Getenv("TEST_DATABASE_URL"))
|
||
pool, err := pgxpool.New(context.Background(), dsn)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
t.Cleanup(pool.Close)
|
||
return pool
|
||
}
|
||
|
||
// dropRoleIfExists 先解掉授权依赖再删角色(临时库里可重复运行)。
|
||
func dropRoleIfExists(t *testing.T, pool *pgxpool.Pool, role string) {
|
||
t.Helper()
|
||
var exists bool
|
||
if err := pool.QueryRow(context.Background(), `SELECT EXISTS(SELECT 1 FROM pg_roles WHERE rolname = $1)`, role).Scan(&exists); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if !exists {
|
||
return
|
||
}
|
||
execFixture(t, pool, fmt.Sprintf(`DROP OWNED BY %s`, quoteIdent(role)), fmt.Sprintf(`DROP ROLE %s`, quoteIdent(role)))
|
||
}
|
||
|
||
func execFixture(t *testing.T, pool *pgxpool.Pool, statements ...string) {
|
||
t.Helper()
|
||
for _, statement := range statements {
|
||
if _, err := pool.Exec(context.Background(), statement); err != nil {
|
||
t.Fatalf("fixture 语句失败(%s):%v", strings.SplitN(statement, "\n", 2)[0], err)
|
||
}
|
||
}
|
||
}
|
||
|
||
func randomSecret(t *testing.T) string {
|
||
t.Helper()
|
||
buf := make([]byte, 32)
|
||
if _, err := rand.Read(buf); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return strings.NewReplacer("+", "A", "/", "B", "=", "C").Replace(base64.URLEncoding.EncodeToString(buf))
|
||
}
|
||
|
||
func writeSecretFile(t *testing.T, content string) string {
|
||
t.Helper()
|
||
path := filepath.Join(t.TempDir(), "dobotai_ops_ro.env")
|
||
if err := os.WriteFile(path, []byte(content+"\n"), 0o600); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return path
|
||
}
|
||
|
||
// buildFixture 建表 + 灌数据 + 建一个镜像生产授权面的最小只读角色,返回只读 DSN。
|
||
func buildFixture(t *testing.T, pool *pgxpool.Pool) string {
|
||
t.Helper()
|
||
ctx := context.Background()
|
||
execFixture(t, pool,
|
||
`DROP TABLE IF EXISTS messages, coverage_decisions, channel_actors, turns CASCADE`,
|
||
`CREATE TABLE turns (id text PRIMARY KEY, tenant_key text NOT NULL, channel text NOT NULL,
|
||
status text NOT NULL, agent_id text NOT NULL DEFAULT '', knowledge_searched boolean NOT NULL DEFAULT false,
|
||
references_json jsonb NOT NULL DEFAULT '[]'::jsonb, request_message_id text, answer_message_id text,
|
||
question text NOT NULL DEFAULT '', answer text NOT NULL DEFAULT '', error_message text NOT NULL DEFAULT '',
|
||
created_at timestamptz NOT NULL)`,
|
||
`CREATE TABLE messages (id text PRIMARY KEY, tenant_key text NOT NULL, channel text NOT NULL, actor_id text,
|
||
department_snapshot text NOT NULL DEFAULT '', region_snapshot text NOT NULL DEFAULT '', actor_union_id_snapshot text NOT NULL DEFAULT '',
|
||
direction text NOT NULL DEFAULT '', role text NOT NULL DEFAULT '', message_type text NOT NULL DEFAULT '',
|
||
status text NOT NULL DEFAULT '', text_content text NOT NULL DEFAULT '', created_at timestamptz NOT NULL)`,
|
||
`CREATE TABLE coverage_decisions (id text PRIMARY KEY, tenant_key text NOT NULL, channel text NOT NULL,
|
||
turn_id text NOT NULL, evaluator_version text NOT NULL, verdict text NOT NULL, reason_code text NOT NULL DEFAULT '',
|
||
reference_count integer NOT NULL DEFAULT 0, created_at timestamptz NOT NULL,
|
||
UNIQUE (tenant_key, channel, turn_id))`,
|
||
`CREATE TABLE channel_actors (id text PRIMARY KEY, tenant_key text NOT NULL, channel text NOT NULL,
|
||
external_actor_id text NOT NULL, actor_type text NOT NULL, display_name text NOT NULL DEFAULT '',
|
||
created_at timestamptz NOT NULL)`,
|
||
`INSERT INTO turns (id, tenant_key, channel, status, agent_id, knowledge_searched, references_json,
|
||
request_message_id, answer_message_id, question, answer, created_at) VALUES
|
||
('turn-4', 'default', 'dingtalk', 'succeeded', 'agent-a', false, '[]',
|
||
'msg-req-4', 'msg-ans-4', '第四问(无规则决策)', '第四答', '2026-10-14T04:00:00Z'),
|
||
('turn-3', 'default', 'dingtalk', 'succeeded', 'agent-a', true, '[{"doc":"x"},{"doc":"y"}]',
|
||
'msg-req-3', 'msg-ans-3', '第三问(正文)', '第三答(正文)', '2026-10-14T03:00:00Z'),
|
||
('turn-2', 'default', 'dingtalk', 'succeeded', 'agent-a', false, '[]',
|
||
'msg-req-2', 'msg-ans-2', '第二问', '第二答', '2026-10-14T02:00:00Z'),
|
||
('turn-1', 'default', 'dingtalk', 'failed', 'agent-b', true, '[]',
|
||
NULL, NULL, '第一问', '', '2026-10-14T01:00:00Z')`,
|
||
`INSERT INTO messages (id, tenant_key, channel, actor_id, department_snapshot, region_snapshot,
|
||
direction, role, message_type, status, text_content, created_at) VALUES
|
||
('msg-req-3', 'default', 'dingtalk', 'actor-row-1', '', '', 'inbound', 'user', 'text', 'ok', '第三问原文', '2026-10-14T03:00:00Z'),
|
||
('msg-ans-3', 'default', 'dingtalk', 'actor-row-1', '研发部', '华南', 'outbound', 'assistant', 'text', 'ok', '第三答原文', '2026-10-14T03:00:01Z'),
|
||
('msg-req-2', 'default', 'dingtalk', 'actor-row-1', '', '', 'inbound', 'user', 'text', 'ok', '第二问原文', '2026-10-14T02:00:00Z'),
|
||
('msg-req-4', 'default', 'dingtalk', 'actor-row-1', '', '', 'inbound', 'user', 'text', 'ok', '第四问原文', '2026-10-14T04:00:00Z'),
|
||
('msg-ans-4', 'default', 'dingtalk', 'actor-row-1', '', '', 'outbound', 'assistant', 'text', 'ok', '第四答原文', '2026-10-14T04:00:01Z')`,
|
||
// 真实关系:messages.actor_id 引用 channel_actors(id);external_actor_id 是另一个
|
||
// 唯一键(tenant_key, channel, external_actor_id)。夹具必须照这个来 —— 用
|
||
// external_actor_id 关联就会命中 0 行(生产对拍实测:错 0 / 对 2234)。
|
||
`INSERT INTO channel_actors (id, tenant_key, channel, external_actor_id, actor_type, display_name, created_at)
|
||
VALUES ('actor-row-1', 'default', 'dingtalk', 'ext-actor-1', 'employee', '不应外泄的姓名', '2026-10-14T00:00:00Z')`,
|
||
// 另一个租户的数据:所有查询都必须把它挡在外面(跨租户隔离)。
|
||
`INSERT INTO turns (id, tenant_key, channel, status, agent_id, created_at)
|
||
VALUES ('turn-other-tenant', 'another-tenant', 'dingtalk', 'succeeded', 'agent-x', '2026-10-14T03:30:00Z')`,
|
||
`INSERT INTO coverage_decisions (id, tenant_key, channel, turn_id, evaluator_version, verdict, reason_code, reference_count, created_at)
|
||
VALUES ('cov-other-tenant', 'another-tenant', 'dingtalk', 'turn-other-tenant', 'v5', 'covered', '', 0, '2026-10-14T03:30:02Z')`,
|
||
// 生产上 (tenant_key, channel, turn_id) 是唯一的:一个 turn 只会有一条决策,
|
||
// 不同 turn 才可能来自不同 evaluator_version —— fixture 照生产形态来。
|
||
`INSERT INTO coverage_decisions (id, tenant_key, channel, turn_id, evaluator_version, verdict, reason_code, reference_count, created_at)
|
||
VALUES ('cov-1', 'default', 'dingtalk', 'turn-3', 'v5', 'covered', '', 2, '2026-10-14T03:00:02Z'),
|
||
('cov-2', 'default', 'dingtalk', 'turn-1', 'v5', 'uncovered', 'no_source', 0, '2026-10-14T01:00:02Z'),
|
||
('cov-3', 'default', 'dingtalk', 'turn-2', 'v4', 'covered', '', 0, '2026-10-14T02:00:02Z')`,
|
||
)
|
||
password := randomSecret(t)
|
||
// 角色在同一个临时库里可能已经存在(多个用例复用):先删再建,保持用例可重复运行。
|
||
dropRoleIfExists(t, pool, "kg_ro_fixture")
|
||
execFixture(t, pool,
|
||
fmt.Sprintf(`CREATE ROLE kg_ro_fixture LOGIN PASSWORD '%s' CONNECTION LIMIT 4`, password),
|
||
`ALTER ROLE kg_ro_fixture SET default_transaction_read_only = on`,
|
||
)
|
||
// 当前库名(避免写死)
|
||
var databaseName string
|
||
if err := pool.QueryRow(ctx, `SELECT current_database()`).Scan(&databaseName); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
execFixture(t, pool,
|
||
fmt.Sprintf(`GRANT CONNECT ON DATABASE %s TO kg_ro_fixture`, quoteIdent(databaseName)),
|
||
`GRANT USAGE ON SCHEMA public TO kg_ro_fixture`,
|
||
`GRANT SELECT ON TABLE public.turns, public.messages, public.coverage_decisions, public.channel_actors TO kg_ro_fixture`,
|
||
)
|
||
// 用与生产一致的写法拼只读 DSN:主机/端口/库名从 TEST_DATABASE_URL 派生
|
||
//(容器内 inet_server_addr 从宿主机不可路由),口令在测试内不打印。
|
||
return dsnWithCredentials(t, "kg_ro_fixture", password)
|
||
}
|
||
|
||
// dsnWithCredentials 把 TEST_DATABASE_URL 里的用户/口令换掉,其余(主机、端口、库、查询串)保持。
|
||
func dsnWithCredentials(t *testing.T, user, password string) string {
|
||
t.Helper()
|
||
parsed, err := url.Parse(strings.TrimSpace(os.Getenv("TEST_DATABASE_URL")))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
parsed.User = url.UserPassword(user, password)
|
||
return parsed.String()
|
||
}
|
||
|
||
func quoteIdent(value string) string {
|
||
return `"` + strings.ReplaceAll(value, `"`, `""`) + `"`
|
||
}
|
||
|
||
func buildReadOnlyClient(t *testing.T, roDSN string) *Client {
|
||
t.Helper()
|
||
client, err := Build(context.Background(), Config{
|
||
DSNFile: writeSecretFile(t, "DOBOTAI_OPS_READ_DSN="+roDSN),
|
||
GovernanceDatabaseURL: "postgres://governance@elsewhere:5432/governance",
|
||
DialMode: "direct",
|
||
ConnectTimeout: 5 * time.Second,
|
||
QueryTimeout: 5 * time.Second,
|
||
MaxConns: 2,
|
||
// 固定"当前时间":仓储入口会拒未来窗口,夹具的时间窗必须相对一个确定时刻来判。
|
||
Now: func() time.Time { return time.Date(2026, 10, 15, 0, 0, 0, 0, time.UTC) },
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("Build() 失败:%v", err)
|
||
}
|
||
t.Cleanup(client.Close)
|
||
return client
|
||
}
|
||
|
||
func TestReadOnlyRepositoryAgainstFixtureDatabase(t *testing.T) {
|
||
pool := fixturePool(t)
|
||
roDSN := buildFixture(t, pool)
|
||
client := buildReadOnlyClient(t, roDSN)
|
||
ctx := context.Background()
|
||
window, err := NewWindow("default", "2026-10-14T00:00:00Z", "2026-10-15T00:00:00Z", 90*24*time.Hour, time.Date(2026, 10, 15, 0, 0, 0, 0, time.UTC))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
// ACL 自检报告必须干净(这是"账号确实最小只读"的机器证据)。
|
||
report := client.ACL()
|
||
if report.Role != "kg_ro_fixture" || report.Super || report.SchemaCreate || len(report.WritePrivileges) != 0 || len(report.MissingSelects) != 0 {
|
||
t.Fatalf("ACL 报告不干净:%+v", report)
|
||
}
|
||
|
||
counts, err := client.QueryCounts(ctx, window, Filter{})
|
||
if err != nil {
|
||
t.Fatalf("QueryCounts() error = %v", err)
|
||
}
|
||
if counts.Total != 4 || counts.ByStatus["succeeded"] != 3 || counts.ByStatus["failed"] != 1 || counts.ByChannel["dingtalk"] != 4 {
|
||
t.Fatalf("counts = %+v", counts)
|
||
}
|
||
filtered, err := client.QueryCounts(ctx, window, Filter{Channel: "dingtalk", Status: "succeeded"})
|
||
if err != nil || filtered.Total != 3 {
|
||
t.Fatalf("过滤计数 = %+v (%v)", filtered, err)
|
||
}
|
||
if _, err := client.QueryCounts(ctx, window, Filter{Status: "nope"}); !errors.Is(err, ErrInvalidQuery) {
|
||
t.Fatalf("未知状态应被拒:%v", err)
|
||
}
|
||
|
||
coverage, err := client.QueryRuleCoverage(ctx, window)
|
||
if err != nil {
|
||
t.Fatalf("QueryRuleCoverage() error = %v", err)
|
||
}
|
||
if len(coverage.Groups) != 3 {
|
||
t.Fatalf("覆盖分组 = %+v", coverage.Groups)
|
||
}
|
||
if coverage.SucceededTurns != 3 || coverage.SucceededWithoutDecision != 1 {
|
||
t.Fatalf("成功 turn 统计 = %+v", coverage)
|
||
}
|
||
for _, group := range coverage.Groups {
|
||
if group.EvaluatorVersion == "v5" && group.Verdict == "uncovered" && group.ReasonCode != "no_source" {
|
||
t.Fatalf("reason_code 未保留:%+v", group)
|
||
}
|
||
}
|
||
|
||
first, err := client.ListTurnMetadata(ctx, window, "", 2)
|
||
if err != nil {
|
||
t.Fatalf("ListTurnMetadata() error = %v", err)
|
||
}
|
||
if len(first.Items) != 2 || !first.HasMore || first.NextCursor == "" {
|
||
t.Fatalf("第一页 = %+v", first)
|
||
}
|
||
if first.Items[0].ID != "turn-4" || first.Items[1].ID != "turn-3" {
|
||
t.Fatalf("倒序不对:%+v", first.Items)
|
||
}
|
||
if first.Items[1].ReferenceCount != 2 || first.Items[1].LinkedMessages != 2 {
|
||
t.Fatalf("引用/关联消息计数不对:%+v", first.Items[1])
|
||
}
|
||
// turn-3:一条关联消息部门/区域为空、一条有值 ⇒ 不该标 missing;turn-4:两条都为空 ⇒ 如实标 missing。
|
||
if first.Items[1].DepartmentMissing || first.Items[1].RegionMissing {
|
||
t.Fatalf("有值的关联消息不该标 missing:%+v", first.Items[1])
|
||
}
|
||
if !first.Items[0].DepartmentMissing || !first.Items[0].RegionMissing {
|
||
t.Fatalf("快照为空的关联消息必须如实标 missing:%+v", first.Items[0])
|
||
}
|
||
second, err := client.ListTurnMetadata(ctx, window, first.NextCursor, 2)
|
||
if err != nil {
|
||
t.Fatalf("第二页 error = %v", err)
|
||
}
|
||
if len(second.Items) != 2 || second.Items[0].ID != "turn-2" || second.Items[1].ID != "turn-1" || second.HasMore {
|
||
t.Fatalf("第二页 = %+v", second)
|
||
}
|
||
if _, err := client.ListTurnMetadata(ctx, window, "不是游标", 10); !errors.Is(err, ErrInvalidQuery) {
|
||
t.Fatalf("非法游标应被拒:%v", err)
|
||
}
|
||
if _, err := client.ListTurnMetadata(ctx, window, "", 101); !errors.Is(err, ErrInvalidQuery) {
|
||
t.Fatalf("超过分页上限应被拒:%v", err)
|
||
}
|
||
// 跨租户隔离:另一个租户的 turn 不能出现在任何结果里(计数、覆盖、列表都一样)。
|
||
otherTenant, err := NewWindow("another-tenant", "2026-10-14T00:00:00Z", "2026-10-15T00:00:00Z", 90*24*time.Hour, time.Date(2026, 10, 15, 0, 0, 0, 0, time.UTC))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
otherCounts, err := client.QueryCounts(ctx, otherTenant, Filter{})
|
||
if err != nil || otherCounts.Total != 1 {
|
||
t.Fatalf("另一个租户应只看到自己的 1 条:%+v (%v)", otherCounts, err)
|
||
}
|
||
for _, item := range append(first.Items, second.Items...) {
|
||
if item.ID == "turn-other-tenant" {
|
||
t.Fatalf("跨租户泄漏:%+v", item)
|
||
}
|
||
}
|
||
if _, err := client.GetTurnForAuthorizedRead(ctx, "default", "turn-other-tenant", func(string) error { return nil }); !errors.Is(err, ErrUnavailable) {
|
||
// default 租户下没有这个 turn ⇒ 读不到(不是"读到了别人的")
|
||
t.Fatalf("跨租户 turn 不该在 default 下可读:%v", err)
|
||
}
|
||
|
||
// 授权读取:没有授权回调一律拒绝(不提供"默认放行")。
|
||
if _, err := client.GetTurnForAuthorizedRead(ctx, "default", "turn-3", nil); !errors.Is(err, ErrInvalidQuery) {
|
||
t.Fatalf("缺授权回调必须拒绝:%v", err)
|
||
}
|
||
if _, err := client.GetTurnForAuthorizedRead(ctx, "default", "turn-3", func(string) error { return errors.New("no") }); !errors.Is(err, ErrInvalidQuery) {
|
||
t.Fatalf("授权回调拒绝时必须拒绝:%v", err)
|
||
}
|
||
detail, err := client.GetTurnForAuthorizedRead(ctx, "default", "turn-3", func(string) error { return nil })
|
||
if err != nil {
|
||
t.Fatalf("授权读取 error = %v", err)
|
||
}
|
||
if detail.Question != "第三问(正文)" || detail.Answer != "第三答(正文)" || len(detail.Messages) != 2 {
|
||
t.Fatalf("授权读取内容不对:%+v", detail)
|
||
}
|
||
// 关联回归:即使夹具里 id 与 external_actor_id 不同,也必须靠正确的字段命中 actor_type。
|
||
for _, message := range detail.Messages {
|
||
if message.ActorType != "employee" {
|
||
t.Fatalf("人员关联没命中(关联字段必须是 channel_actors.id):%+v", message)
|
||
}
|
||
}
|
||
if strings.Contains(detail.Messages[0].ActorType, "不应外泄") {
|
||
t.Fatalf("只应带 actor_type,不应带员工姓名:%+v", detail.Messages[0])
|
||
}
|
||
_ = client.Available(ctx)
|
||
}
|
||
|
||
func TestBuildRefusesUnsafeAccountsAndTargets(t *testing.T) {
|
||
pool := fixturePool(t)
|
||
roDSN := buildFixture(t, pool)
|
||
ctx := context.Background()
|
||
password := randomSecret(t)
|
||
dropRoleIfExists(t, pool, "kg_writer_fixture")
|
||
execFixture(t, pool,
|
||
fmt.Sprintf(`CREATE ROLE kg_writer_fixture LOGIN PASSWORD '%s'`, password),
|
||
`GRANT USAGE ON SCHEMA public TO kg_writer_fixture`,
|
||
`GRANT SELECT, INSERT ON TABLE public.turns TO kg_writer_fixture`,
|
||
`GRANT SELECT ON TABLE public.messages, public.coverage_decisions, public.channel_actors TO kg_writer_fixture`,
|
||
)
|
||
writerDSN := dsnWithCredentials(t, "kg_writer_fixture", password)
|
||
|
||
// 有写权限的账号:fail closed(不是"能读就算过")。
|
||
_, err := Build(ctx, Config{
|
||
DSNFile: writeSecretFile(t, writerDSN), DialMode: "direct",
|
||
GovernanceDatabaseURL: "postgres://governance@elsewhere:5432/governance",
|
||
ConnectTimeout: 5 * time.Second, QueryTimeout: 5 * time.Second})
|
||
if !errors.Is(err, ErrNotReadOnly) {
|
||
t.Fatalf("可写账号必须被拒为 ErrNotReadOnly:%v", err)
|
||
}
|
||
// 指向治理主库的 DSN:拒绝(防"误配后读写治理库")。
|
||
_, err = Build(ctx, Config{
|
||
DSNFile: writeSecretFile(t, roDSN), DialMode: "direct", GovernanceDatabaseURL: roDSN,
|
||
ConnectTimeout: 5 * time.Second, QueryTimeout: 5 * time.Second})
|
||
if !errors.Is(err, ErrNotReadOnly) {
|
||
t.Fatalf("指向治理库必须被拒:%v", err)
|
||
}
|
||
// secret 文件缺失:ErrUnavailable(按"未启用"处理,不拖垮治理)。
|
||
_, err = Build(ctx, Config{DSNFile: filepath.Join(t.TempDir(), "missing.env"), DialMode: "direct",
|
||
ConnectTimeout: time.Second, QueryTimeout: time.Second})
|
||
if !errors.Is(err, ErrUnavailable) {
|
||
t.Fatalf("缺 secret 应为 ErrUnavailable:%v", err)
|
||
}
|
||
}
|