Files
sam 064eeab748 feat(governance): let owners rename the organisation and workspaces
Adds the organisation routes the single binary's dashboard needs (list, get, workspace list and create) plus owner-only renames of the organisation and its workspaces. A rename is guarded by the version the caller read, writes only the display label, and appends an actor-attributed event in governance.org_events in the same transaction. The Organisation page shows those name changes, and a deep link is no longer replaced by the landing page while the router is still resolving it.

Refs #2077
2026-11-20 21:10:34 +11:00

98 lines
2.8 KiB
Go

package governance
import (
"strings"
"testing"
)
// TestMayRename guards the boundary that decides who may change an
// organisation's names: a person who owns it org-wide, and nobody else.
func TestMayRename(t *testing.T) {
const (
org = OrgID("org-a")
home = OrgID("org-sys")
)
tests := []struct {
name string
p Principal
want bool
}{
{
name: "org owner",
p: Principal{Kind: KindUser, Org: org, Memberships: []Membership{{OrgID: org, Role: RoleOwner}}},
want: true,
},
{
name: "owner of another org",
p: Principal{Kind: KindUser, Org: org, Memberships: []Membership{{OrgID: home, Role: RoleOwner}}},
},
{
name: "admin in the org",
p: Principal{Kind: KindUser, Org: org, Memberships: []Membership{{OrgID: org, Role: RoleAdmin}}},
},
{
name: "owner of one workspace only",
p: Principal{Kind: KindUser, Org: org, Memberships: []Membership{
{OrgID: org, WorkspaceID: "default", Role: RoleOwner},
}},
},
{
name: "shared system token acting as an owner",
p: Principal{Kind: KindSystem, Org: org, Memberships: []Membership{{OrgID: org, Role: RoleOwner}}},
},
{
name: "bot holding the owner role",
p: Principal{Kind: KindBot, Org: org, Memberships: []Membership{{OrgID: org, Role: RoleOwner}}},
},
{
name: "no membership",
p: Principal{Kind: KindUser, Org: home},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.mayRename(org); got != tt.want {
t.Fatalf("mayRename(%q) = %v, want %v", org, got, tt.want)
}
})
}
}
// TestValidateName guards the label grammar: a name is trimmed, bounded, and
// cannot carry a control or bidi character that would render it as another
// name.
func TestValidateName(t *testing.T) {
tests := []struct {
name string
in string
want string
wantErr bool
}{
{name: "ordinary name", in: "Catlow Ltd", want: "Catlow Ltd"},
{name: "trimmed", in: " Acme ", want: "Acme"},
{name: "unicode", in: "Grüne Straße", want: "Grüne Straße"},
{name: "at the limit", in: strings.Repeat("a", MaxNameLength), want: strings.Repeat("a", MaxNameLength)},
{name: "blank", in: " ", wantErr: true},
{name: "past the limit", in: strings.Repeat("a", MaxNameLength+1), wantErr: true},
{name: "newline", in: "Acme\nLtd", wantErr: true},
{name: "bidi override", in: "Acme\u202Egnp.exe", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ValidateName(tt.in)
if tt.wantErr {
if err == nil {
t.Fatalf("ValidateName(%q) = %q, want an error", tt.in, got)
}
return
}
if err != nil {
t.Fatalf("ValidateName(%q): %v", tt.in, err)
}
if got != tt.want {
t.Fatalf("ValidateName(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}