644 lines
23 KiB
Go
644 lines
23 KiB
Go
//go:build integration
|
||
|
||
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"os"
|
||
"strconv"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// testEnv 是 integration_test 共享的进程内测试环境。
|
||
// 只在 -tags=integration 时编译,避免污染默认 `go test`。
|
||
type testEnv struct {
|
||
srv *httptest.Server
|
||
typeKey string // 本次测试用的对象类型 api_name,全库唯一
|
||
}
|
||
|
||
// 用一个长随机后缀保证类型不与历史/并发测试冲突
|
||
func uniqTypeKey(prefix string) string {
|
||
return fmt.Sprintf("TestRgo_%s_%d", prefix, time.Now().UnixNano())
|
||
}
|
||
|
||
func testDSN() string {
|
||
if v := os.Getenv("TEST_DATABASE_URL"); v != "" {
|
||
return v
|
||
}
|
||
return "postgres://dacli:dacli123@localhost:15432/shangshu?sslmode=disable"
|
||
}
|
||
|
||
// setupTestEnv 启一个 httptest 服务,跑到真实 PG,并在 t.Cleanup 里清理测试类型/实例/边/clearance。
|
||
func setupTestEnv(t *testing.T) (*testEnv, *app) {
|
||
t.Helper()
|
||
if testing.Short() {
|
||
t.Skip("integration 测试需要真实 PG;跳过 -short 模式")
|
||
}
|
||
ctx := context.Background()
|
||
|
||
pool, err := initDB(ctx, testDSN())
|
||
if err != nil {
|
||
t.Skipf("无法连接测试 PG(%s): %v", testDSN(), err)
|
||
}
|
||
|
||
cfg := &Config{
|
||
HTTPPort: 0,
|
||
DatabaseURL: testDSN(),
|
||
AIEnabled: false,
|
||
AllowCORS: false,
|
||
BaseIRI: baseIRI(), // 与 loadConfig 默认值保持一致(IRI 命名空间)
|
||
}
|
||
a := &app{cfg: cfg, pool: pool}
|
||
|
||
gin.SetMode(gin.TestMode)
|
||
r := gin.New()
|
||
registerRoutes(r, a)
|
||
srv := httptest.NewServer(r)
|
||
|
||
key := uniqTypeKey(t.Name())
|
||
env := &testEnv{srv: srv, typeKey: key}
|
||
|
||
t.Cleanup(func() {
|
||
// 清理:直接 SQL 清理本次测试创建的类型 + 残留 clearance
|
||
_, _ = pool.Exec(ctx,
|
||
`DELETE FROM objects WHERE type_api_name=$1`, key)
|
||
_, _ = pool.Exec(ctx, `DELETE FROM object_types WHERE api_name=$1`, key)
|
||
_, _ = pool.Exec(ctx,
|
||
`DELETE FROM links WHERE source_type=$1 OR target_type=$1`, key)
|
||
// 清理备份(按 name 前缀 TestRgo_)—— 跨测试共享一个 schema,物理删最干净
|
||
_, _ = pool.Exec(ctx,
|
||
`DELETE FROM backups.snapshot_runs WHERE name LIKE 'TestRgo_%'`)
|
||
// 注:analyst 共享主体不复删,由后续运行累积,最坏情况仅扩大可见集不影响正确性
|
||
invalidateClassCache()
|
||
authzCache.Range(func(k, _ any) bool { authzCache.Delete(k); return true })
|
||
srv.Close()
|
||
pool.Close()
|
||
})
|
||
return env, a
|
||
}
|
||
|
||
// ---------- 备份(v1.3.0)----------
|
||
|
||
// uniqSnapshotName 在并行 / 重跑场景下用 nanos 命名备份,便于 cleanup 物理删除。
|
||
func uniqSnapshotName(t *testing.T) string {
|
||
return fmt.Sprintf("TestRgo_snap_%d", time.Now().UnixNano())
|
||
}
|
||
|
||
// httpDo 简易 JSON POST/GET 帮手。body 为 nil 时是 GET。
|
||
// headerExtras 可选:用于「正常 headers 之外再叠加快照头/特殊头」的场景(如「写
|
||
// 路径要带 X-Snapshot-Id」组合)。
|
||
func (e *testEnv) httpDo(t *testing.T, method, path string, body any, headers map[string]string, headerExtras ...map[string]string) (int, map[string]any) {
|
||
t.Helper()
|
||
var rdr io.Reader
|
||
if body != nil {
|
||
b, err := json.Marshal(body)
|
||
if err != nil {
|
||
t.Fatalf("marshal: %v", err)
|
||
}
|
||
rdr = bytes.NewReader(b)
|
||
}
|
||
req, err := http.NewRequest(method, e.srv.URL+path, rdr)
|
||
if err != nil {
|
||
t.Fatalf("req: %v", err)
|
||
}
|
||
if body != nil {
|
||
req.Header.Set("Content-Type", "application/json")
|
||
}
|
||
for k, v := range headers {
|
||
req.Header.Set(k, v)
|
||
}
|
||
// headerExtras 可叠加在 headers 之上(用于「写路径要带快照头」等组合场景)
|
||
for _, extras := range headerExtras {
|
||
for k, v := range extras {
|
||
req.Header.Set(k, v)
|
||
}
|
||
}
|
||
resp, err := e.srv.Client().Do(req)
|
||
if err != nil {
|
||
t.Fatalf("do: %v", err)
|
||
}
|
||
defer resp.Body.Close()
|
||
out := map[string]any{}
|
||
raw, _ := io.ReadAll(resp.Body)
|
||
if len(raw) > 0 {
|
||
_ = json.Unmarshal(raw, &out)
|
||
}
|
||
return resp.StatusCode, out
|
||
}
|
||
|
||
// ---------- 1. 枚举写入校验(issue #5-①,含修复后的变量遮蔽 bug)----------
|
||
|
||
func TestIntegration_EnumValidation(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
key := env.typeKey
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
// 建类型 + 枚举属性
|
||
if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": key, "display_name": "回归-枚举校验",
|
||
}, h); code != 200 && code != 201 {
|
||
t.Fatalf("create type: %d %v", code, body)
|
||
}
|
||
if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties", map[string]any{
|
||
"api_name": "level", "display_name": "级别", "data_type": "string",
|
||
"enum_values": []string{"L0", "L1", "L2", "L3"},
|
||
}, h); code != 200 && code != 201 {
|
||
t.Fatalf("add property: %d %v", code, body)
|
||
}
|
||
|
||
// 合法值 → 200
|
||
if code, _ := env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": "ok1", "properties": map[string]any{"level": "L2"}, "via": "test",
|
||
}, h); code != 200 && code != 201 {
|
||
t.Errorf("合法枚举值 L2 应成功, got %d", code)
|
||
}
|
||
|
||
// 越界值 → 400 + 友好错误(这是修复过的变量遮蔽 bug 的回归点)
|
||
code, body := env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": "bad1", "properties": map[string]any{"level": "L9"}, "via": "test",
|
||
}, h)
|
||
if code != 400 {
|
||
t.Fatalf("越界枚举值 L9 应 400, got %d, body=%v", code, body)
|
||
}
|
||
if msg, _ := body["error"].(string); !strings.Contains(msg, "enum_violation") {
|
||
t.Errorf("错误信息应包含 enum_violation, got %v", body)
|
||
}
|
||
|
||
// 越界实例不应被写入
|
||
code, _ = env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/bad1", nil, h)
|
||
if code == 200 {
|
||
t.Errorf("越界实例 bad1 不应被持久化")
|
||
}
|
||
|
||
// 空字符串 → 不校验(视作「未填」,不阻断 AI 抽取)
|
||
if code, _ := env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": "empty1", "properties": map[string]any{"level": ""}, "via": "test",
|
||
}, h); code != 200 && code != 201 {
|
||
t.Errorf("空枚举值应放行, got %d", code)
|
||
}
|
||
}
|
||
|
||
// ---------- 2. 分级过滤(issue #5-③④)----------
|
||
|
||
func TestIntegration_ClassificationFilter(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
key := env.typeKey
|
||
|
||
// 建类型(带分级)+ 枚举属性 + 插入 analyst clearance(L0,L1)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": key, "display_name": "回归-分级过滤",
|
||
"classification_property": "level",
|
||
"classification_levels": []string{"L0", "L1", "L2", "L3"},
|
||
"classification_default": "L0",
|
||
}, h); code != 200 && code != 201 {
|
||
t.Fatal("create type failed")
|
||
}
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties", map[string]any{
|
||
"api_name": "level", "display_name": "级别", "data_type": "string",
|
||
"enum_values": []string{"L0", "L1", "L2", "L3"},
|
||
}, h)
|
||
|
||
// 注入 analyst clearance:L0+L1(principal 名与 GET 时 X-Principal 一致)
|
||
pool, _ := initDB(context.Background(), testDSN())
|
||
defer pool.Close()
|
||
_, _ = pool.Exec(context.Background(),
|
||
`INSERT INTO principal_clearances(principal,marking) VALUES($1,'L0'),($1,'L1') ON CONFLICT DO NOTHING`,
|
||
"analyst")
|
||
invalidateClassCache()
|
||
// 清掉可能残留的 authz 缓存(sync.Map 进程级,跨测试可能命中过期空集)
|
||
authzCache.Range(func(k, _ any) bool { authzCache.Delete(k); return true })
|
||
|
||
// 建 3 个实例:L0/L1/L2
|
||
for _, c := range []struct{ pk, lvl string }{
|
||
{"a_l0", "L0"}, {"a_l1", "L1"}, {"a_l2", "L2"},
|
||
} {
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": c.pk, "properties": map[string]any{"level": c.lvl}, "via": "test",
|
||
}, h)
|
||
}
|
||
|
||
// manager → 见 3 个
|
||
_, body := env.httpDo(t, "GET", "/api/v1/connector/objects/"+key+"?limit=50", nil, h)
|
||
if total, _ := body["total"].(float64); int(total) != 3 {
|
||
t.Errorf("manager 应见 3 个, got total=%v items=%v", total, body["items"])
|
||
}
|
||
|
||
// analyst(L0,L1) → 只见到 L0/L1
|
||
_, body = env.httpDo(t, "GET", "/api/v1/connector/objects/"+key+"?limit=50", nil,
|
||
map[string]string{"X-Principal": "analyst"})
|
||
total := int(body["total"].(float64))
|
||
if total != 2 {
|
||
t.Errorf("analyst 应见 2 个, got %d, items=%v", total, body["items"])
|
||
}
|
||
pks := map[string]bool{}
|
||
for _, it := range body["items"].([]any) {
|
||
pks[it.(map[string]any)["pk"].(string)] = true
|
||
}
|
||
if pks["a_l2"] {
|
||
t.Errorf("analyst 不应见到 L2 实例")
|
||
}
|
||
|
||
// analyst 直取 L2 实例 → 404(fail-closed:不泄漏存在性)
|
||
code, _ := env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/a_l2", nil,
|
||
map[string]string{"X-Principal": "analyst"})
|
||
if code != 404 {
|
||
t.Errorf("analyst 直取 L2 应 404, got %d", code)
|
||
}
|
||
}
|
||
|
||
// ---------- 3. 字段级分级脱敏(issue #5-②)----------
|
||
|
||
func TestIntegration_FieldSensitivity(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
key := env.typeKey
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": key, "display_name": "回归-字段脱敏",
|
||
"classification_property": "level",
|
||
"classification_levels": []string{"L0", "L1", "L2", "L3"},
|
||
"classification_default": "L0",
|
||
}, h)
|
||
|
||
// 一个公开属性 + 一个 secret 属性(sensitivity=L2)
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties", map[string]any{
|
||
"api_name": "public_name", "display_name": "名称", "data_type": "string",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties", map[string]any{
|
||
"api_name": "secret_id", "display_name": "身份证", "data_type": "string",
|
||
"sensitivity": "L2",
|
||
}, h)
|
||
|
||
// 创建实例
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": "u1",
|
||
"properties": map[string]any{"level": "L1", "public_name": "张三", "secret_id": "110101199001011234"},
|
||
"via": "test",
|
||
}, h)
|
||
|
||
// 给 analyst 注入 L0+L1 clearance,并清掉 authz 缓存(sync.Map 跨测试可能命中过期空集)
|
||
pool, _ := initDB(context.Background(), testDSN())
|
||
defer pool.Close()
|
||
_, _ = pool.Exec(context.Background(),
|
||
`INSERT INTO principal_clearances(principal,marking) VALUES($1,'L0'),($1,'L1') ON CONFLICT DO NOTHING`,
|
||
"analyst")
|
||
invalidateClassCache()
|
||
authzCache.Range(func(k, _ any) bool { authzCache.Delete(k); return true })
|
||
|
||
// manager → 两个字段都见
|
||
_, body := env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/u1", nil, h)
|
||
props := body["properties"].(map[string]any)
|
||
if props["secret_id"] != "110101199001011234" {
|
||
t.Errorf("manager 应见 secret_id 原值, got %v", props["secret_id"])
|
||
}
|
||
|
||
// analyst(L0,L1) → secret_id 应被 redact 为 ***
|
||
_, body = env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/u1", nil,
|
||
map[string]string{"X-Principal": "analyst"})
|
||
props = body["properties"].(map[string]any)
|
||
if props["secret_id"] != "***" {
|
||
t.Errorf("analyst 应见 secret_id='***', got %v", props["secret_id"])
|
||
}
|
||
if props["public_name"] != "张三" {
|
||
t.Errorf("公开字段不应被 redact, got %v", props["public_name"])
|
||
}
|
||
}
|
||
|
||
// ---------- 4. 删除 → 恢复关系边(issue #4)----------
|
||
|
||
func TestIntegration_RestoreEdges(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
key := env.typeKey
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
pool, _ := initDB(context.Background(), testDSN())
|
||
defer pool.Close()
|
||
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": key, "display_name": "回归-恢复边",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/ontology/link-types", map[string]any{
|
||
"api_name": key + "_rel", "display_name": "关联",
|
||
"source_type": key, "target_type": key,
|
||
}, h)
|
||
|
||
// 建两个实例 + 一条边
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": "n1", "properties": map[string]any{"name": "一"}, "via": "test",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": "n2", "properties": map[string]any{"name": "二"}, "via": "test",
|
||
}, h)
|
||
if code, body := env.httpDo(t, "POST", "/api/v1/links", map[string]any{
|
||
"type": key + "_rel", "source_type": key, "source_pk": "n1",
|
||
"target_type": key, "target_pk": "n2",
|
||
}, h); code != 200 && code != 201 {
|
||
t.Fatalf("建边失败: %d %v", code, body)
|
||
}
|
||
|
||
// 删除 n1(级联应同时删边)
|
||
if code, body := env.httpDo(t, "DELETE", "/api/v1/objects/"+key+"/n1", nil, h); code != 200 {
|
||
t.Fatalf("delete n1: %d %v", code, body)
|
||
} else if v, _ := body["links_deleted"].(float64); v != 1 {
|
||
t.Errorf("links_deleted 应为 1, got %v", body)
|
||
} else if v, _ := body["links_snapshot_saved"].(float64); v != 1 {
|
||
t.Errorf("links_snapshot_saved 应为 1, got %v", body)
|
||
}
|
||
|
||
// 验证:删除后 n1 应 404
|
||
if code, _ := env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/n1", nil, h); code != 404 {
|
||
t.Errorf("删除后 n1 应 404, got %d", code)
|
||
}
|
||
|
||
// 恢复 n1(默认 restore_edges=true)
|
||
code, body := env.httpDo(t, "POST", "/api/v1/objects/"+key+"/n1/restore", nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("restore n1: %d %v", code, body)
|
||
}
|
||
if v, _ := body["edges_created"].(float64); v != 1 {
|
||
t.Errorf("edges_created 应为 1, got %v", body)
|
||
}
|
||
|
||
// 验证:边在 DB 中真重建(不靠 connObject 的边字段:单对象接口不返回 links_out)
|
||
var linkCount int
|
||
err := pool.QueryRow(context.Background(),
|
||
`SELECT count(*) FROM links WHERE link_type=$1 AND source_type=$2 AND source_pk=$3 AND target_pk=$4`,
|
||
key+"_rel", key, "n1", "n2").Scan(&linkCount)
|
||
if err != nil {
|
||
t.Fatalf("查 links: %v", err)
|
||
}
|
||
if linkCount != 1 {
|
||
t.Errorf("恢复后 n1->n2 边应存在, got %d", linkCount)
|
||
}
|
||
}
|
||
|
||
// ---------- 5. recompute 落库(issue #5-⑩)----------
|
||
|
||
func TestIntegration_RecomputeApply(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
key := env.typeKey
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": key, "display_name": "回归-recompute",
|
||
"classification_property": "level",
|
||
"classification_levels": []string{"L0", "L1", "L2", "L3"},
|
||
"classification_default": "L0",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties", map[string]any{
|
||
"api_name": "level", "display_name": "级别", "data_type": "string",
|
||
"enum_values": []string{"L0", "L1", "L2", "L3"},
|
||
}, h)
|
||
|
||
// 先建一个分类正确的实例(用于验证「不会改对它」)
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": key, "pk": "r1", "properties": map[string]any{"level": "L3"}, "via": "test",
|
||
}, h)
|
||
|
||
// 手工把数据库里的 classification 改成错误值,模拟历史数据
|
||
pool, _ := initDB(context.Background(), testDSN())
|
||
defer pool.Close()
|
||
_, _ = pool.Exec(context.Background(),
|
||
`UPDATE objects SET classification='WRONG' WHERE type_api_name=$1`, key)
|
||
invalidateClassCache()
|
||
|
||
// dry_run=true 应返回 would_change ≥ 1(r1 应被纠正回 L3)
|
||
_, body := env.httpDo(t, "POST", "/api/v1/ontology/classification/recompute", map[string]any{
|
||
"type": key, "dry_run": true,
|
||
}, h)
|
||
would, _ := body["would_change"].(float64)
|
||
if would < 1 {
|
||
t.Errorf("dry_run 应报告至少 1 条需改, got %v", body)
|
||
}
|
||
// 此时 DB 中仍是 WRONG
|
||
var cls string
|
||
_ = pool.QueryRow(context.Background(),
|
||
`SELECT classification FROM objects WHERE type_api_name=$1 AND pk='r1'`, key).Scan(&cls)
|
||
if cls != "WRONG" {
|
||
t.Errorf("dry_run 不应落库, got cls=%q", cls)
|
||
}
|
||
|
||
// apply=true 应落库纠正
|
||
_, body = env.httpDo(t, "POST", "/api/v1/ontology/classification/recompute", map[string]any{
|
||
"type": key, "dry_run": false, "apply": true,
|
||
}, h)
|
||
if applied, _ := body["applied"].(float64); applied < 1 {
|
||
t.Errorf("apply=true 应至少修正 1 条, got %v", body)
|
||
}
|
||
_ = pool.QueryRow(context.Background(),
|
||
`SELECT classification FROM objects WHERE type_api_name=$1 AND pk='r1'`, key).Scan(&cls)
|
||
if cls != "L3" {
|
||
t.Errorf("apply 后 classification 应为 L3, got %q", cls)
|
||
}
|
||
}
|
||
|
||
// ---------- 6. 数据备份 (v1.3.0) 端到端:create → list → inspect → diff → download → delete ----------
|
||
|
||
func TestIntegration_BackupLifecycle(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
// 当前 public 的 objects 表应有内容(>0 行),diff 才有意义
|
||
pool, _ := initDB(context.Background(), testDSN())
|
||
defer pool.Close()
|
||
var preCount int
|
||
_ = pool.QueryRow(context.Background(), `SELECT count(*) FROM objects`).Scan(&preCount)
|
||
if preCount == 0 {
|
||
t.Skip("public.objects 为空,跳过备份 diff 验证")
|
||
}
|
||
|
||
// 1) create
|
||
name := uniqSnapshotName(t)
|
||
code, body := env.httpDo(t, "POST", "/api/v1/backups", map[string]any{
|
||
"name": name, "notes": "test backup",
|
||
}, h)
|
||
if code != 201 {
|
||
t.Fatalf("创建备份失败: %d %v", code, body)
|
||
}
|
||
idF, ok := body["id"].(float64)
|
||
if !ok {
|
||
t.Fatalf("响应缺 id: %v", body)
|
||
}
|
||
id := int64(idF)
|
||
if body["principal"] != "manager" {
|
||
t.Errorf("principal 应为 manager, got %v", body["principal"])
|
||
}
|
||
tablesF, _ := body["table_count"].(float64)
|
||
if tablesF < 20 {
|
||
t.Errorf("备份应至少覆盖 20 张表, got %v", tablesF)
|
||
}
|
||
|
||
// 2) list 应能看见
|
||
code, body = env.httpDo(t, "GET", "/api/v1/backups", nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("list 失败: %d", code)
|
||
}
|
||
items, _ := body["items"].([]any)
|
||
found := false
|
||
for _, it := range items {
|
||
if idF, _ := it.(map[string]any)["id"].(float64); int64(idF) == id {
|
||
found = true
|
||
break
|
||
}
|
||
}
|
||
if !found {
|
||
t.Errorf("list 应包含新备份 #%d", id)
|
||
}
|
||
|
||
// 3) inspect 应返回元信息 + schema_name(按 schema 镜像方案不再列每张表)
|
||
code, body = env.httpDo(t, "GET", "/api/v1/backups/"+strconv.FormatInt(id, 10), nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("inspect 失败: %d", code)
|
||
}
|
||
schemaName, _ := body["schema_name"].(string)
|
||
if !strings.HasPrefix(schemaName, "backups_") {
|
||
t.Errorf("inspect 应返回 schema_name=backups_*, got %q", schemaName)
|
||
}
|
||
|
||
// 4) switch 应返回「切到这个快照需要的信息」
|
||
code, body = env.httpDo(t, "GET", "/api/v1/backups/"+strconv.FormatInt(id, 10)+"/switch", nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("switch 失败: %d", code)
|
||
}
|
||
if body["switch_via"] != "X-Snapshot-Id" {
|
||
t.Errorf("switch_via 应为 X-Snapshot-Id, got %v", body["switch_via"])
|
||
}
|
||
|
||
// 4.5) 立即用快照头读 connector/object-types 验证「切到快照 → 见 schema」
|
||
// (不需要专门的 diff 接口:snapshot_router 中间件已是 diff 替代品)
|
||
code, body = env.httpDo(t, "GET", "/api/v1/connector/object-types",
|
||
nil, h,
|
||
map[string]string{"X-Snapshot-Id": strconv.FormatInt(id, 10)})
|
||
if code != 200 {
|
||
t.Fatalf("快照头读 connector/object-types 失败: %d", code)
|
||
}
|
||
if types, _ := body["object_types"].([]any); len(types) == 0 {
|
||
t.Errorf("快照应非空")
|
||
}
|
||
|
||
// 5) 制造改动:建测试类型 + 新实例 → 再用快照头读应见不到新增
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": env.typeKey, "display_name": "backup-diff-probe",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": env.typeKey, "pk": "diff_probe",
|
||
"properties": map[string]any{"name": "diff probe"}, "via": "test",
|
||
}, h)
|
||
code, body = env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/diff_probe",
|
||
nil, h,
|
||
map[string]string{"X-Snapshot-Id": strconv.FormatInt(id, 10)})
|
||
if code == 200 {
|
||
t.Errorf("快照应不见改后新增的 diff_probe, got 200 %v", body)
|
||
}
|
||
|
||
// 6) list 能看到刚建的快照
|
||
code, body = env.httpDo(t, "GET", "/api/v1/backups?limit=10", nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("list 失败: %d", code)
|
||
}
|
||
items2, _ := body["items"].([]any)
|
||
foundSchema := false
|
||
for _, it := range items2 {
|
||
m := it.(map[string]any)
|
||
if idF, _ := m["id"].(float64); int64(idF) == id {
|
||
if s, _ := m["schema_name"].(string); strings.HasPrefix(s, "backups_") {
|
||
foundSchema = true
|
||
}
|
||
}
|
||
}
|
||
if !foundSchema {
|
||
t.Errorf("list 应包含 schema_name=backups_* 的快照 #%d", id)
|
||
}
|
||
|
||
// 7) connector 主体应被拒(managerAuth 中间件 → 401)
|
||
code, body = env.httpDo(t, "GET", "/api/v1/backups", nil,
|
||
map[string]string{"X-Principal": "connector"})
|
||
if code != 401 {
|
||
t.Errorf("非 manager 应被拒 401, got %d, body=%v", code, body)
|
||
}
|
||
|
||
// 8) delete
|
||
code, body = env.httpDo(t, "DELETE", "/api/v1/backups/"+strconv.FormatInt(id, 10), nil, h)
|
||
if code != 200 {
|
||
t.Fatalf("delete 失败: %d %v", code, body)
|
||
}
|
||
code, body = env.httpDo(t, "GET", "/api/v1/backups/"+strconv.FormatInt(id, 10), nil, h)
|
||
if code != 404 {
|
||
t.Errorf("delete 后 inspect 应 404, got %d", code)
|
||
}
|
||
}
|
||
|
||
// TestIntegration_BackupDiffAfterMutation 验证「改动前/后各打一份 → diff 可见」
|
||
// 这正是「敢操作」的工作流核心。
|
||
func TestIntegration_BackupDiffAfterMutation(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
// 创建测试类型 + 一个实例
|
||
env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
|
||
"api_name": env.typeKey, "display_name": "diff-test",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": env.typeKey, "pk": "before", "properties": map[string]any{"v": "1"}, "via": "test",
|
||
}, h)
|
||
|
||
// 1) before
|
||
_, bodyBefore := env.httpDo(t, "POST", "/api/v1/backups",
|
||
map[string]any{"name": uniqSnapshotName(t), "notes": "before mutation"}, h)
|
||
idBefore := int64(bodyBefore["id"].(float64))
|
||
|
||
// 2) 改:再写两个新实例 + 改一个原值
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": env.typeKey, "pk": "after1", "properties": map[string]any{"v": "1"}, "via": "test",
|
||
}, h)
|
||
env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
|
||
"type": env.typeKey, "pk": "after2", "properties": map[string]any{"v": "1"}, "via": "test",
|
||
}, h)
|
||
putCode, _ := env.httpDo(t, "PUT", "/api/v1/objects/"+env.typeKey+"/before", map[string]any{
|
||
"properties": map[string]any{"v": "2"}, "via": "test",
|
||
}, h)
|
||
if putCode != 200 {
|
||
t.Logf("[warn] PUT before 返回 %d(updateObject 路径可能不同),继续验证 added", putCode)
|
||
}
|
||
|
||
// 3) 用快照头读快照:应见 before=v=1(不改前)、不见 after1/after2(改后才有)
|
||
code, snapBody := env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/before",
|
||
nil, h,
|
||
map[string]string{"X-Snapshot-Id": strconv.FormatInt(idBefore, 10)})
|
||
if code != 200 {
|
||
t.Fatalf("快照读 before 失败: %d", code)
|
||
}
|
||
snapProps, _ := snapBody["properties"].(map[string]any)
|
||
if snapProps["v"] != "1" {
|
||
t.Errorf("快照中 before 应为 v=1, got %v", snapProps["v"])
|
||
}
|
||
// after1 在快照中不存在
|
||
code, _ = env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/after1",
|
||
nil, h,
|
||
map[string]string{"X-Snapshot-Id": strconv.FormatInt(idBefore, 10)})
|
||
if code == 200 {
|
||
t.Errorf("快照不应见 after1, got 200")
|
||
}
|
||
|
||
// 4) 不带头读活库:before=v=2、after1/after2 都存在
|
||
_, liveBody := env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/before", nil, h)
|
||
liveProps, _ := liveBody["properties"].(map[string]any)
|
||
if liveProps["v"] != "2" {
|
||
t.Errorf("活库 before 应为 v=2, got %v", liveProps["v"])
|
||
}
|
||
code, _ = env.httpDo(t, "GET", "/api/v1/connector/object/"+env.typeKey+"/after1", nil, h)
|
||
if code != 200 {
|
||
t.Errorf("活库 after1 应 200, got %d", code)
|
||
}
|
||
}
|