69 lines
2.3 KiB
Go
69 lines
2.3 KiB
Go
package controlplanestore
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
func TestInstanceOnlyKindsRefuseOtherOrgs(t *testing.T) {
|
|
tests := []struct {
|
|
name, org, kind string
|
|
refused bool
|
|
}{
|
|
{"another org writes an instance setting", "acme", ToolSettingsKind, true},
|
|
{"the system org writes an instance setting", storecontract.DefaultOrgID, ToolSettingsKind, false},
|
|
{"another org writes an org-scoped kind", "acme", SoulKind, false},
|
|
{"another org writes a forge connection", "acme", ForgeConnectionsKind, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := (&Server{}).checkOrgWrite(t.Context(), tt.org, tt.kind, []byte(`{}`))
|
|
if refused := errors.Is(err, ErrValidation); refused != tt.refused {
|
|
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCatalogHidesInstanceOnlyKinds pins what the catalog advertises: the
|
|
// system org sees an instance setting, another org does not (its write would
|
|
// be refused), and an org-scoped kind stays visible to both.
|
|
func TestCatalogHidesInstanceOnlyKinds(t *testing.T) {
|
|
server := &Server{ordered: []Definition{
|
|
{Kind: AuditRetentionKind, Title: "Audit retention"},
|
|
{Kind: SoulKind, Title: "Soul"},
|
|
}}
|
|
tests := []struct {
|
|
name string
|
|
org org.OrgID
|
|
wantRetention bool
|
|
wantOrgResource bool
|
|
}{
|
|
{"system org sees the instance setting", org.DefaultOrgID, true, true},
|
|
{"another org does not", "acme", false, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx := org.WithOrg(t.Context(), tt.org)
|
|
catalog, err := server.Catalog(ctx, &pb.CatalogRequest{})
|
|
if err != nil {
|
|
t.Fatalf("Catalog() error = %v", err)
|
|
}
|
|
visible := make(map[string]bool, len(catalog.GetResources()))
|
|
for _, descriptor := range catalog.GetResources() {
|
|
visible[descriptor.GetKind()] = true
|
|
}
|
|
if got := visible[AuditRetentionKind]; got != tt.wantRetention {
|
|
t.Fatalf("%s visible = %v, want %v", AuditRetentionKind, got, tt.wantRetention)
|
|
}
|
|
if got := visible[SoulKind]; got != tt.wantOrgResource {
|
|
t.Fatalf("%s visible = %v, want %v", SoulKind, got, tt.wantOrgResource)
|
|
}
|
|
})
|
|
}
|
|
}
|