Files
ssw-ontology/server/object_owner_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

291 lines
11 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"
"fmt"
"strings"
"testing"
"time"
)
// 行级可改性轴(object_owner)的端到端验证:
//
// 归属只收窄写权限,不放宽——
// 未设归属 → 行为与从前完全一致;
// 设有归属 → 非归属方即便持有全局写授权也不能改这一行;
// 归属方可转交行;组归属的成员只对该行生效;manager 豁免。
//
// 这些断言走真实路径:MCP 工具调用(进程内复用 REST handler)+ REST 归属接口。
// ownTestPrincipal 造一个带全局读写授权的测试主体(返回主体名)。
func ownTestPrincipal(t *testing.T, a *app, tag string) string {
t.Helper()
ctx := context.Background()
name := fmt.Sprintf("owntest_%s_%d", tag, time.Now().UnixNano())
if _, err := a.pool.Exec(ctx,
`INSERT INTO principals(name,description,api_key) VALUES($1,$2,$3)`,
name, "归属测试主体", "owntest-key-"+name); err != nil {
t.Fatalf("建主体失败: %v", err)
}
// 全局读写授权:先让 MCP 的粗粒度写关卡放行,被测的变量才只剩「归属收窄」。
if _, err := a.pool.Exec(ctx,
`INSERT INTO grants(principal,action,scope_type,scope_pk,effect) VALUES($1,'read','*','*','allow'),
($1,'write','*','*','allow')`, name); err != nil {
t.Fatalf("授权失败: %v", err)
}
a.InvalidateAuthzCache(name)
t.Cleanup(func() {
_, _ = a.pool.Exec(ctx, `DELETE FROM grants WHERE principal=$1`, name)
_, _ = a.pool.Exec(ctx, `DELETE FROM principal_groups WHERE principal=$1`, name)
_, _ = a.pool.Exec(ctx, `DELETE FROM principals WHERE name=$1`, name)
})
return name
}
// ownTestGroup 造一个测试组并写入成员。
func ownTestGroup(t *testing.T, a *app, tag string, members ...string) string {
t.Helper()
ctx := context.Background()
g := fmt.Sprintf("owntest_grp_%s_%d", tag, time.Now().UnixNano())
for _, m := range members {
if _, err := a.pool.Exec(ctx,
`INSERT INTO principal_groups(principal,group_name) VALUES($1,$2) ON CONFLICT DO NOTHING`, m, g); err != nil {
t.Fatalf("建组失败: %v", err)
}
}
t.Cleanup(func() {
_, _ = a.pool.Exec(ctx, `DELETE FROM principal_groups WHERE group_name=$1`, g)
})
return g
}
// ownTestObject 建一个类型 + 一个实例,返回实例 pk。
func ownTestObject(t *testing.T, e *testEnv, mh map[string]string) string {
t.Helper()
key := e.typeKey
if code, body := e.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 := e.httpDo(t, "POST", "/api/v1/objects", map[string]any{
"type": key, "pk": "own1", "properties": map[string]any{"name": "归属探针实例"},
}, mh); code != 200 && code != 201 {
t.Fatalf("建实例失败: %d %v", code, body)
}
return "own1"
}
// ownWrite 试写一次实例(走 MCP create_object 的 merge 语义)。
// 返回 (是否被拒, 响应文本)。
func ownWrite(t *testing.T, e *testEnv, h map[string]string, typ, pk, val string) (bool, string) {
t.Helper()
text, isErr, rpcErr := mcpCallRaw(t, e, h, "create_object", map[string]any{
"type": typ, "pk": pk, "properties": map[string]any{"name": val},
})
if rpcErr != "" {
t.Fatalf("MCP 分发失败: %s", rpcErr)
}
return isErr, text
}
func TestIntegration_ObjectOwnerNarrowsWrite(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
mh := authHeaderFor(t, a, "manager")
key := env.typeKey
pk := ownTestObject(t, env, mh)
alice := ownTestPrincipal(t, a, "alice")
bob := ownTestPrincipal(t, a, "bob")
ah := authHeaderFor(t, a, alice)
bh := authHeaderFor(t, a, bob)
// ---- 1. 未设归属:持写授权者照常可写(向后兼容)----
if isErr, txt := ownWrite(t, env, ah, key, pk, "alice 写的"); isErr {
t.Fatalf("未设归属时 alice 应可写,实际被拒: %s", txt)
}
// ---- 2. 设归属给 bob → alice 被拒(尽管她仍持有全局写授权)----
code, body := env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", map[string]any{
"owners": []any{map[string]any{"kind": "principal", "name": bob}},
}, mh)
if code != 200 {
t.Fatalf("设归属失败: %d %v", code, body)
}
if isErr, txt := ownWrite(t, env, ah, key, pk, "alice 再写"); !isErr {
t.Fatalf("设归属后 alice 应被拒,实际写入成功: %s", txt)
} else if !strings.Contains(txt, "归属") {
t.Errorf("拒绝原因应提到归属,实际: %s", txt)
}
// bob 是归属方 → 可写
if isErr, txt := ownWrite(t, env, bh, key, pk, "bob 写的"); isErr {
t.Fatalf("归属方 bob 应可写,实际被拒: %s", txt)
}
// ---- 3. 非归属方不能改归属 ----
code, body = env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", map[string]any{
"owners": []any{map[string]any{"kind": "principal", "name": alice}},
}, ah)
if code != 403 {
t.Fatalf("非归属方设归属应 403,实际 %d: %v", code, body)
}
// ---- 4. 归属方可转交 / 加协作者 ----
code, body = env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", map[string]any{
"owners": []any{
map[string]any{"kind": "principal", "name": bob},
map[string]any{"kind": "principal", "name": alice},
},
}, bh)
if code != 200 {
t.Fatalf("归属方加协作者应 200,实际 %d: %v", code, body)
}
if isErr, txt := ownWrite(t, env, ah, key, pk, "alice 协作者写的"); isErr {
t.Fatalf("成为协作者后 alice 应可写,实际被拒: %s", txt)
}
// ---- 5. 归属可查 ----
code, body = env.httpDo(t, "GET", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", nil, mh)
if code != 200 {
t.Fatalf("查归属失败: %d %v", code, body)
}
if body["ownership_enforced"] != true {
t.Errorf("归属应已生效: %v", body)
}
owners, _ := body["owners"].([]any)
if len(owners) != 2 {
t.Errorf("应有 2 个归属方,实际 %d: %v", len(owners), body["owners"])
}
// ---- 6. manager 可解除归属(解除比设置更强,仅 manager)----
if code, body := env.httpDo(t, "DELETE", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", nil, bh); code != 403 {
t.Errorf("非 manager 解除归属应 403,实际 %d: %v", code, body)
}
code, body = env.httpDo(t, "DELETE", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", nil, mh)
if code != 200 {
t.Fatalf("manager 解除归属失败: %d %v", code, body)
}
// 解除后:持写授权即可写(alice 本就有写权)
if isErr, txt := ownWrite(t, env, ah, key, pk, "alice 解除后写的"); isErr {
t.Fatalf("解除归属后 alice 应可写,实际被拒: %s", txt)
}
}
func TestIntegration_ObjectOwnerGroup(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
mh := authHeaderFor(t, a, "manager")
key := env.typeKey
pk := ownTestObject(t, env, mh)
alice := ownTestPrincipal(t, a, "alice")
carol := ownTestPrincipal(t, a, "carol")
ah := authHeaderFor(t, a, alice)
ch := authHeaderFor(t, a, carol)
// 组内成员 = alice
grp := ownTestGroup(t, a, "ops", alice)
// manager 设组归属
code, body := env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", map[string]any{
"owners": []any{map[string]any{"kind": "group", "name": grp}},
}, mh)
if code != 200 {
t.Fatalf("设组归属失败: %d %v", code, body)
}
// 组内成员可写
if isErr, txt := ownWrite(t, env, ah, key, pk, "组内成员写的"); isErr {
t.Fatalf("组内成员 alice 应可写,实际被拒: %s", txt)
}
// 组外成员不可写
if isErr, txt := ownWrite(t, env, ch, key, pk, "组外成员写的"); !isErr {
t.Fatalf("组外成员 carol 应被拒,实际写入成功: %s", txt)
}
// 组成员身份不扩大全局权限:carol 仍是全局可写(只对该行受限)
// —— 换一个未设归属的行,carol 照常可写
code, body = env.httpDo(t, "POST", "/api/v1/objects", map[string]any{
"type": key, "pk": "own2", "properties": map[string]any{"name": "无归属实例"},
}, mh)
if code != 200 && code != 201 {
t.Fatalf("建第二实例失败: %d %v", code, body)
}
if isErr, txt := ownWrite(t, env, ch, key, "own2", "carol 写无归属行"); isErr {
t.Fatalf("无归属的行不该被组限制外溢: %s", txt)
}
// 删组 → 引用该组的归属行一并解除(否则那行会变成谁都改不了的死锁)
code, body = env.httpDo(t, "DELETE", "/api/v1/access/groups/"+grp, nil, mh)
if code != 200 {
t.Fatalf("删组失败: %d %v", code, body)
}
if body["ownership_rows_released"] == nil {
t.Errorf("删组应回报解除的归属行数: %v", body)
}
if isErr, txt := ownWrite(t, env, ch, key, pk, "carol 删组后写的"); isErr {
t.Fatalf("删组后该行应恢复可写,实际被拒: %s", txt)
}
}
func TestIntegration_ObjectOwnerRejectsUnknownAssignee(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
mh := authHeaderFor(t, a, "manager")
key := env.typeKey
pk := ownTestObject(t, env, mh)
// 归属方必须真实存在(NVIDIA:赋值要落到确实存在的主体上)
code, body := env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", map[string]any{
"owners": []any{map[string]any{"kind": "principal", "name": "owntest_不存在的主体"}},
}, mh)
if code != 400 {
t.Fatalf("归属主体不存在应 400,实际 %d: %v", code, body)
}
// 组不存在同样拒绝
code, body = env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", map[string]any{
"owners": []any{map[string]any{"kind": "group", "name": "owntest_不存在的组"}},
}, mh)
if code != 400 {
t.Fatalf("归属组不存在应 400,实际 %d: %v", code, body)
}
// 不存在的对象不能设归属(避免预埋将来生效的锁)
code, body = env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/不存在/owners", map[string]any{
"owners": []any{map[string]any{"kind": "principal", "name": "manager"}},
}, mh)
if code != 404 {
t.Fatalf("对象不存在应 404,实际 %d: %v", code, body)
}
}
func TestIntegration_ObjectOwnerRemovedWithObject(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
mh := authHeaderFor(t, a, "manager")
key := env.typeKey
pk := ownTestObject(t, env, mh)
bob := ownTestPrincipal(t, a, "bob")
if code, body := env.httpDo(t, "PUT", "/api/v1/ownership/objects/"+key+"/"+pk+"/owners", map[string]any{
"owners": []any{map[string]any{"kind": "principal", "name": bob}},
}, mh); code != 200 {
t.Fatalf("设归属失败: %d %v", code, body)
}
// 删实例 → 归属行随实例清理(行没了,归属声明没有存在意义)
if code, body := env.httpDo(t, "DELETE", "/api/v1/objects/"+key+"/"+pk, nil, mh); code != 200 {
t.Fatalf("删实例失败: %d %v", code, body)
}
var n int
if err := a.pool.QueryRow(context.Background(),
`SELECT count(*) FROM object_owners WHERE object_type=$1 AND object_pk=$2`, key, pk).Scan(&n); err != nil {
t.Fatalf("查归属行失败: %v", err)
}
if n != 0 {
t.Errorf("实例删除后应清掉归属行,实际残留 %d 行", n)
}
}