Files
ssw-ontology/server/scope_integration_test.go
adminandClaude Code d14440a3ed feat(server): v1.8.0 本体导入 + 分区 + 行级归属 + 认证位 + 结构 diff + 分页 + 进度回报
一次收口 2026-11-06 的九项能力(CHANGELOG 1.5.4 → 1.8.0):

- 自检探针(readiness.go):check_readiness / check_answerable 两个只读探针,把
  「本体里没有」「我没权限看」「模型没抽全」三种空结果原因拆开
- 读路径语义钉死(scope.go):标记与分级的可见范围统一由 Scope 产出,实例分级
  为空时继承类型默认级(fail-closed),列表与详情口径一致
- 行级可改性轴(object_owner.go):归属只收窄不宽于 grants,所有对象写路径统一
  走 canWriteObject,归属查询失败即拒绝
- 公理第 6 类 certification:认证位保护人工策展内容,AI 重摄取不再覆盖已认证条目
- 跨版本结构 diff(schema_diff.go):schema_snapshots 全量快照 + diff 端点,机器
  事实与人工策展分组呈现
- 分区维度 zone:分区绑类型、命中任一即可见,读路径一律带 zone 条件
- 统一分页信封(pagination.go):列表回包统一 count/total/limit/offset/has_more/
  truncated,total 与 items 同源
- MCP 长任务进度(mcp_progress.go):客户端带 progressToken 才走 SSE 进度帧
- 本体 schema 导入(ontology_import.go):Turtle 导入 + 四道 fail-fast 闸门 +
  整事务 + (source, external_id) 幂等 + IRI 往返不变式

MCP 工具清单并入单一注册表(mcp_registry.go),对外声明与实际可调不再各说各话;
工具数 42 → 54(只读 27 / 写入 27),注册表与分发 case 的双向漂移由 AST 单测拦截。

CHANGELOG 编号整理:新「认证位 + 行级归属」条目由 1.6.0 改为 1.5.6(1.6.x 留给
分区与结构 diff),2026-09-20 的 GB/T 规划稿改标「规划稿·未发布」——该能力已并入
v1.5.1,避免与正式发布的版本号重复。

Co-Authored-By: Claude Code <[email protected]>
2026-11-06 13:51:48 +08:00

161 lines
6.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build integration
package main
import (
"context"
"testing"
)
// TestIntegration_ClassificationScope 端到端钉死分级语义(三条,缺一不可):
//
// 1. 实例分级为空 → **继承类型默认级**,不是「无限制」
// (场景:先灌数据、后配分级引擎,存量行 objects.classification 还是空的);
// 2. 主体与该类型的级别完全无交集 → 该类型的实例一律不可见(fail-closed);
// 3. 列表路径与详情路径口径一致:列表看不到 → 详情也必须 404(不泄漏存在性)。
func TestIntegration_ClassificationScope(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
mh := authHeaderFor(t, a, "manager")
key := env.typeKey
// ---- 1. 类型 + 属性(此时还没配分级引擎)----
if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{
"api_name": key, "display_name": "分级作用域探针",
}, mh); code != 200 && code != 201 {
t.Fatalf("建类型失败: %d %v", code, body)
}
if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties", map[string]any{
"api_name": "lvl", "display_name": "级别", "data_type": "string",
"enum_values": []string{"L0", "L1", "L2"},
}, mh); code != 200 && code != 201 {
t.Fatalf("加属性失败: %d %v", code, body)
}
// ---- 2. 引擎配置**之前**造一个实例:objects.classification 保持空 ----
if code, body := env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
"type": key, "pk": "legacy", "properties": map[string]any{"name": "存量实例"},
}, mh); code != 200 && code != 201 {
t.Fatalf("建存量实例失败: %d %v", code, body)
}
// ---- 3. 现在才配分级引擎:levels L0..L2,默认 L1 ----
if code, body := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+key+"/classification", map[string]any{
"classification_property": "lvl",
"classification_levels": []string{"L0", "L1", "L2"},
"classification_default": "L1",
}, mh); code != 200 && code != 201 {
t.Fatalf("配分级引擎失败: %d %v", code, body)
}
// ---- 4. 引擎配好后再造两个实例(分级会被物化进 objects.classification)----
for _, c := range []struct{ pk, lvl string }{{"low", "L0"}, {"high", "L2"}} {
if code, body := env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
"type": key, "pk": c.pk,
"properties": map[string]any{"name": "实例" + c.pk, "lvl": c.lvl},
}, mh); code != 200 && code != 201 {
t.Fatalf("建实例 %s 失败: %d %v", c.pk, code, body)
}
}
// ---- 5. 两个受限主体 ----
lowKey := newScopedPrincipal(t, a, "scope_low", []string{"L0"}) // 只能看 L0
noneKey := newScopedPrincipal(t, a, "scope_none", nil) // 一条 clearance 都没有
lowH := map[string]string{"X-API-Key": lowKey}
noneH := map[string]string{"X-API-Key": noneKey}
visible := func(h map[string]string) map[string]bool {
t.Helper()
code, body := env.httpDo(t, "GET", "/api/v1/connector/objects/"+key, nil, h)
if code != 200 {
t.Fatalf("列实例失败: %d %v", code, body)
}
out := map[string]bool{}
items, _ := body["items"].([]any)
for _, it := range items {
m, _ := it.(map[string]any)
if pk, ok := m["pk"].(string); ok {
out[pk] = true
}
}
return out
}
// manager:三个都看得见
mgr := visible(mh)
if len(mgr) != 3 {
t.Errorf("manager 应看到 3 个实例,实到 %v", mgr)
}
// 只能看 L0 的主体:只看得见 low
// legacy(分级为空 → 继承默认级 L1)不可见 ← 本次改动修复的核心
// high(L2)不可见
low := visible(lowH)
if !low["low"] {
t.Errorf("L0 实例应可见:%v", low)
}
if low["legacy"] {
t.Errorf("分级为空的存量实例应继承默认级 L1 → 对 L0 主体不可见,实则可见:%v", low)
}
if low["high"] {
t.Errorf("L2 实例对 L0 主体不可见:%v", low)
}
// 一条 clearance 都没有的主体:什么都看不到(不是「没有过滤条件所以全放行」)
none := visible(noneH)
if len(none) != 0 {
t.Errorf("无 clearance 的主体应一条都看不到,实到 %v", none)
}
// ---- 6. 详情路径与列表路径口径一致 ----
code, _ := env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/legacy", nil, lowH)
if code != 404 {
t.Errorf("列表看不到的实例,详情也必须 404(不泄漏存在性),got %d", code)
}
code, _ = env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/low", nil, lowH)
if code != 200 {
t.Errorf("可见实例的详情应 200,got %d", code)
}
code, _ = env.httpDo(t, "GET", "/api/v1/connector/object/"+key+"/legacy", nil, noneH)
if code != 404 {
t.Errorf("无 clearance 主体的详情应 404,got %d", code)
}
}
// newScopedPrincipal 造一个只读主体并精确设定其 clearance 集合。
// 返回其 api_key(请求头用)。
func newScopedPrincipal(t *testing.T, a *app, name string, clearances []string) string {
t.Helper()
ctx := context.Background()
key := "test-key-" + name + "-do-not-use-in-prod"
if _, err := a.pool.Exec(ctx,
`INSERT INTO principals(name,description,is_builtin,api_key) VALUES($1,'scope test',false,$2)
ON CONFLICT(name) DO UPDATE SET api_key=EXCLUDED.api_key`, name, key); err != nil {
t.Fatalf("建主体 %s 失败: %v", name, err)
}
if _, err := a.pool.Exec(ctx, `DELETE FROM principal_clearances WHERE principal=$1`, name); err != nil {
t.Fatalf("清 clearance 失败: %v", err)
}
for _, c := range clearances {
if _, err := a.pool.Exec(ctx,
`INSERT INTO principal_clearances(principal,marking) VALUES($1,$2) ON CONFLICT DO NOTHING`,
name, c); err != nil {
t.Fatalf("写 clearance 失败: %v", err)
}
}
// 读路径要求 read 授权(否则整体拒绝,与 clearance 无关)
if _, err := a.pool.Exec(ctx,
`INSERT INTO grants(principal,action,scope_type,scope_pk,effect) VALUES($1,'read','*','*','allow')
ON CONFLICT DO NOTHING`, name); err != nil {
t.Fatalf("写 grant 失败: %v", err)
}
a.InvalidateAuthzCache(name)
t.Cleanup(func() {
c := context.Background()
_, _ = a.pool.Exec(c, `DELETE FROM principal_clearances WHERE principal=$1`, name)
_, _ = a.pool.Exec(c, `DELETE FROM grants WHERE principal=$1`, name)
a.InvalidateAuthzCache(name)
})
return key
}