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

143 lines
4.0 KiB
Go

package main
import (
"context"
"testing"
)
// 行级可改性轴(object_owners)的纯逻辑单测。
func TestOwnerDecision(t *testing.T) {
cases := []struct {
name string
owners []ObjectOwner
principal string
groups map[string]bool
wantOwner bool
wantEnforced bool
}{
{
name: "无归属行不限制任何人",
owners: nil,
principal: "alice",
groups: map[string]bool{},
wantOwner: true,
wantEnforced: false,
},
{
name: "本人持有归属",
owners: []ObjectOwner{{Kind: OwnerKindPrincipal, Name: "alice"}},
principal: "alice",
groups: map[string]bool{},
wantOwner: true,
wantEnforced: true,
},
{
name: "他人持有归属",
owners: []ObjectOwner{{Kind: OwnerKindPrincipal, Name: "bob"}},
principal: "alice",
groups: map[string]bool{},
wantOwner: false,
wantEnforced: true,
},
{
name: "组归属命中成员",
owners: []ObjectOwner{{Kind: OwnerKindGroup, Name: "ops"}},
principal: "alice",
groups: map[string]bool{"ops": true},
wantOwner: true,
wantEnforced: true,
},
{
name: "组归属不命中非成员",
owners: []ObjectOwner{{Kind: OwnerKindGroup, Name: "ops"}},
principal: "alice",
groups: map[string]bool{"other": true},
wantOwner: false,
wantEnforced: true,
},
{
name: "多归属任一命中即可",
owners: []ObjectOwner{
{Kind: OwnerKindPrincipal, Name: "bob"},
{Kind: OwnerKindGroup, Name: "ops"},
},
principal: "alice",
groups: map[string]bool{"ops": true},
wantOwner: true,
wantEnforced: true,
},
{
name: "账号与组并存时账号命中",
owners: []ObjectOwner{
{Kind: OwnerKindPrincipal, Name: "alice"},
{Kind: OwnerKindGroup, Name: "ops"},
},
principal: "alice",
groups: map[string]bool{},
wantOwner: true,
wantEnforced: true,
},
}
for _, c := range cases {
isOwner, hasOwners := ownerDecision(c.owners, c.principal, c.groups)
if isOwner != c.wantOwner || hasOwners != c.wantEnforced {
t.Errorf("%s: got (%v,%v) want (%v,%v)", c.name, isOwner, hasOwners, c.wantOwner, c.wantEnforced)
}
}
}
func TestNormalizeOwners(t *testing.T) {
// kind 缺省 = principal
got, err := normalizeOwners([]ObjectOwner{{Name: "alice"}})
if err != nil {
t.Fatalf("应接受缺省 kind: %v", err)
}
if len(got) != 1 || got[0].Kind != OwnerKindPrincipal || got[0].Name != "alice" {
t.Errorf("归一化结果不对: %+v", got)
}
// 去重(kind+name 相同)
got, err = normalizeOwners([]ObjectOwner{
{Kind: "principal", Name: "alice"},
{Kind: "principal", Name: " alice "},
{Kind: "group", Name: "alice"},
})
if err != nil {
t.Fatalf("不应报错: %v", err)
}
if len(got) != 2 {
t.Errorf("应去重为 2 条,实际 %d: %+v", len(got), got)
}
// 非法 kind
if _, err := normalizeOwners([]ObjectOwner{{Kind: "robot", Name: "alice"}}); err == nil {
t.Error("非法 kind 应被拒绝")
}
// 空 name
if _, err := normalizeOwners([]ObjectOwner{{Kind: "principal", Name: " "}}); err == nil {
t.Error("空 name 应被拒绝")
}
// 空名单合法(= 解除归属)
got, err = normalizeOwners(nil)
if err != nil || len(got) != 0 {
t.Errorf("空名单应合法且为空: %v %+v", err, got)
}
}
func TestCanWriteObjectManagerBypass(t *testing.T) {
a := &app{}
// manager 不受行级归属限制(与 CanRead/CanWrite 的 manager 特例一致)
if !a.canWriteObject(context.Background(), &Authz{Principal: "manager"}, "write", "T", "pk") {
t.Error("manager 应无条件放行")
}
// 无授权的主体:连 grant 都不通过,直接拒(不触库)
if a.canWriteObject(context.Background(), &Authz{Principal: "alice"}, "write", "T", "pk") {
t.Error("无写授权的主体应被拒")
}
// nil authz 不放行
if a.canWriteObject(context.Background(), nil, "write", "T", "pk") {
t.Error("nil authz 应拒绝")
}
}