Answers "can roles reset as well as add?" — yes, both now.
Role membership UNIONS across the cascade:
- A deeper .zddc that defines an inherited role again with one
extra member ADDS that member (was: deepest definition shadowed
the ancestor's entirely).
- New `reset: true` on a role definition breaks the union — that
level's members are authoritative, ancestor definitions above
are excluded; descendants below still union on top. Use it to
give a project its own team independent of a deployment-wide
default.
- lookupRoleMembers / RoleMembers reworked: walk deep→shallow,
union members, stop at the first reset:true; finally fold in
chain.Embedded.Roles as the baseline so a role declared only in
defaults.zddc.yaml is "defined" (and a deployment's on-disk
redefinition unions on top).
Admin checks are now role-aware:
- IsSubtreeAdmin / CanEditZddc's strict-ancestor scan use
MatchesPrincipal instead of MatchesPattern, so `admins:
[document_controller]` resolves to the role's members. The
strict-ancestor scan resolves roles only up to level i, so a
role defined at the deepest level (= dirPath) never confers
self-edit rights.
Two standard roles ship in defaults.zddc.yaml (empty members — a
fresh deployment grants nothing until they're populated):
document_controller — files into the WORM zones. Gets:
- rw at the project level (read + overwrite-existing; NOT c, so
it can't make arbitrary folders)
- rwc at archive/ (can create party subfolders)
- subtree-admin at working/ and staging/ (full create + manage,
including taking over a fenced per-user home) — scoped HERE,
not at the project root, so the WORM constraint still binds
it in archive/<party>/received|issued
- listed in worm: on received/ and issued/ → write-once-create
survives the WORM mask
project_team — read-only across the project. The per-user
working home's fenced auto-own .zddc (rwcda for the creator)
wins via deepest-match, so "read-only except what I own" falls
out of the cascade with no special rule. Inside received/issued
their r is preserved (worm: doesn't strip read).
archive/<party>/ gains `auto_own: true` (UNFENCED) so whoever
creates a party subtree (normally the doc controller) owns it and
can set up that counterparty's .zddc afterward — without fencing,
project_team:r still cascades through to received/issued.
Tests: roles_test (union + reset), standardroles_test (the
doc-controller scoped-create matrix + project-team read-only-except-
owned), ensure_test updated for the new party-folder auto-own.
fileapi_test's WORM doc-controller test already uses worm: [role].
All Go + 248 Playwright tests green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
165 lines
4.5 KiB
Go
165 lines
4.5 KiB
Go
package zddc
|
|
|
|
import "testing"
|
|
|
|
func TestParseVerbSetRoundTrip(t *testing.T) {
|
|
cases := []struct {
|
|
in string
|
|
out string
|
|
}{
|
|
{"", ""},
|
|
{"r", "r"},
|
|
{"rw", "rw"},
|
|
{"wr", "rw"}, // canonical reorder
|
|
{"rwcd", "rwcd"},
|
|
{"adcwr", "rwcda"}, // canonical reorder
|
|
{"RWCDA", "rwcda"}, // case-insensitive
|
|
{" r w c ", "rwc"}, // whitespace tolerated
|
|
}
|
|
for _, tc := range cases {
|
|
v, ok := ParseVerbSet(tc.in)
|
|
if !ok {
|
|
t.Errorf("ParseVerbSet(%q) ok=false", tc.in)
|
|
continue
|
|
}
|
|
if got := v.String(); got != tc.out {
|
|
t.Errorf("ParseVerbSet(%q).String() = %q, want %q", tc.in, got, tc.out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseVerbSetUnknownLetter(t *testing.T) {
|
|
if _, ok := ParseVerbSet("rwx"); ok {
|
|
t.Errorf("ParseVerbSet(\"rwx\") = ok=true, want false")
|
|
}
|
|
}
|
|
|
|
func TestVerbSetHasAndUnion(t *testing.T) {
|
|
rw, _ := ParseVerbSet("rw")
|
|
cd, _ := ParseVerbSet("cd")
|
|
if !rw.Has(VerbR) {
|
|
t.Errorf("rw should have R")
|
|
}
|
|
if rw.Has(VerbC) {
|
|
t.Errorf("rw should not have C")
|
|
}
|
|
if got := rw.Union(cd).String(); got != "rwcd" {
|
|
t.Errorf("rw|cd = %q, want rwcd", got)
|
|
}
|
|
}
|
|
|
|
func TestIsPrincipalRole(t *testing.T) {
|
|
cases := map[string]bool{
|
|
"alice@example.com": false,
|
|
"*@example.com": false,
|
|
"alice@*": false,
|
|
"_doc_controller": true,
|
|
"vendor_acme": true,
|
|
"*": true, // legacy bare wildcard — treated as role-or-pattern
|
|
}
|
|
for in, want := range cases {
|
|
if got := IsPrincipalRole(in); got != want {
|
|
t.Errorf("IsPrincipalRole(%q) = %v, want %v", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRoleMembersUnionAcrossCascade(t *testing.T) {
|
|
chain := PolicyChain{
|
|
Levels: []ZddcFile{
|
|
// root: role defined with one member
|
|
{Roles: map[string]Role{
|
|
"editors": {Members: []string{"alice@example.com"}},
|
|
}},
|
|
// child: ADDS a member (union, not shadow)
|
|
{Roles: map[string]Role{
|
|
"editors": {Members: []string{"bob@example.com"}},
|
|
}},
|
|
},
|
|
HasAnyFile: true,
|
|
}
|
|
got := RoleMembers(chain, 1, "editors", ModeDelegated)
|
|
if len(got) != 2 {
|
|
t.Fatalf("union: got %v, want both alice + bob", got)
|
|
}
|
|
has := func(s string) bool {
|
|
for _, g := range got {
|
|
if g == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
if !has("alice@example.com") || !has("bob@example.com") {
|
|
t.Errorf("union: got %v, want alice + bob", got)
|
|
}
|
|
// At root level, only the root definition is in the visible chain.
|
|
got = RoleMembers(chain, 0, "editors", ModeDelegated)
|
|
if len(got) != 1 || got[0] != "alice@example.com" {
|
|
t.Errorf("root visibility: got %v, want [alice]", got)
|
|
}
|
|
}
|
|
|
|
func TestRoleMembersResetBreaksUnion(t *testing.T) {
|
|
chain := PolicyChain{
|
|
Levels: []ZddcFile{
|
|
// root
|
|
{Roles: map[string]Role{
|
|
"editors": {Members: []string{"alice@example.com"}},
|
|
}},
|
|
// mid: reset — ancestors above this are excluded for editors
|
|
{Roles: map[string]Role{
|
|
"editors": {Members: []string{"carol@example.com"}, Reset: true},
|
|
}},
|
|
// leaf: still unions on top of the reset level
|
|
{Roles: map[string]Role{
|
|
"editors": {Members: []string{"dave@example.com"}},
|
|
}},
|
|
},
|
|
HasAnyFile: true,
|
|
}
|
|
got := RoleMembers(chain, 2, "editors", ModeDelegated)
|
|
// Expect carol (reset level) + dave (leaf), NOT alice (excluded by reset).
|
|
if len(got) != 2 {
|
|
t.Fatalf("reset: got %v, want carol + dave only", got)
|
|
}
|
|
for _, g := range got {
|
|
if g == "alice@example.com" {
|
|
t.Errorf("reset should have excluded alice; got %v", got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMatchesPrincipalLegacyPatternFallback(t *testing.T) {
|
|
// No roles defined; bare "*" and "*example.com" must still match
|
|
// via legacy email-pattern semantics.
|
|
chain := PolicyChain{
|
|
Levels: []ZddcFile{{}},
|
|
HasAnyFile: true,
|
|
}
|
|
if !MatchesPrincipal("*", "alice@example.com", chain, 0, ModeDelegated) {
|
|
t.Errorf("bare * should match any email via legacy fallback")
|
|
}
|
|
if !MatchesPrincipal("*example.com", "alice@example.com", chain, 0, ModeDelegated) {
|
|
t.Errorf("*example.com should match alice@example.com via legacy fallback")
|
|
}
|
|
}
|
|
|
|
func TestMatchesPrincipalRoleNamePrefersRole(t *testing.T) {
|
|
// When a role is defined, the role match wins; legacy fallback is
|
|
// not consulted.
|
|
chain := PolicyChain{
|
|
Levels: []ZddcFile{{
|
|
Roles: map[string]Role{
|
|
"vendor_acme": {Members: []string{"*@acme.com"}},
|
|
},
|
|
}},
|
|
HasAnyFile: true,
|
|
}
|
|
if !MatchesPrincipal("vendor_acme", "rep@acme.com", chain, 0, ModeDelegated) {
|
|
t.Errorf("rep@acme.com should match role vendor_acme")
|
|
}
|
|
if MatchesPrincipal("vendor_acme", "rep@other.com", chain, 0, ModeDelegated) {
|
|
t.Errorf("rep@other.com should NOT match role vendor_acme — fallback to pattern would wrongly succeed")
|
|
}
|
|
}
|