Files
summercms/modules/cabana/token_scope_names_test.go

272 lines
11 KiB
Go

package cabana
import (
"context"
"slices"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/party"
"git.golem15.com/golem15/summercms/modules/phrasebook"
"git.golem15.com/golem15/summercms/modules/towel"
)
// namedScopesPlugin declares the token scopes it is given, behind one
// permission of its own.
type namedScopesPlugin struct {
id string
scopes []pact.TokenScope
}
func (p namedScopesPlugin) ID() string { return p.id }
func (namedScopesPlugin) Requires() []string { return nil }
func (namedScopesPlugin) Register(*backpack.App) error { return nil }
func (namedScopesPlugin) Boot(*backpack.App) error { return nil }
func (p namedScopesPlugin) TokenScopes() []pact.TokenScope { return p.scopes }
func (p namedScopesPlugin) Permissions() []pact.Permission {
return []pact.Permission{{Code: p.id + ".use", Tab: "Tab", Label: "Use"}}
}
// TestTokenScopeNames locks the exported token-scope catalog (SC-1, D-09):
// TokenScopeNames answers *, admin:read, then every plugin's scopes in
// plugin order and each plugin's declaration order, from party.Activated
// alone (before cabana.Activate, as an OAuth server reads it at Boot);
// TokenScopeLabels translates phrase keys in the context's locale, keeps
// literal labels and passes unknown names through.
func TestTokenScopeNames(t *testing.T) {
scope := func(id, name, label string) pact.TokenScope {
return pact.TokenScope{Name: name, Label: label, Description: "Description of " + name, Permissions: []string{id + ".use"}, Methods: []string{"GET"}}
}
blog := namedScopesPlugin{id: "acme.blog", scopes: []pact.TokenScope{
scope("acme.blog", "blog:write", "Blog write"),
scope("acme.blog", "blog", "Blog"),
}}
shop := namedScopesPlugin{id: "acme.shop", scopes: []pact.TokenScope{
scope("acme.shop", "shop:read", "Shop read"),
}}
none := namedScopesPlugin{id: "acme.none"}
appWith := func(t *testing.T, plugins ...party.Plugin) *backpack.App {
t.Helper()
app := backpack.New(nil)
if err := app.Publish(party.Activated(plugins)); err != nil {
t.Fatal(err)
}
return app
}
t.Run("catalog order before activation", func(t *testing.T) {
names, err := TokenScopeNames(appWith(t, shop, none, blog))
if err != nil {
t.Fatal(err)
}
if want := []string{"*", "admin:read", "shop:read", "blog:write", "blog"}; !slices.Equal(names, want) {
t.Fatalf("names = %v, want %v", names, want)
}
names, err = TokenScopeNames(appWith(t, blog, shop))
if err != nil || !slices.Equal(names, []string{"*", "admin:read", "blog:write", "blog", "shop:read"}) {
t.Fatalf("names in the other plugin order = %v, %v", names, err)
}
})
t.Run("framework scopes only", func(t *testing.T) {
for _, app := range []*backpack.App{backpack.New(nil), appWith(t), appWith(t, none), nil} {
names, err := TokenScopeNames(app)
if err != nil || !slices.Equal(names, []string{"*", "admin:read"}) {
t.Fatalf("names = %v, %v, want [* admin:read]", names, err)
}
}
})
t.Run("a catalog compile error is answered", func(t *testing.T) {
dup := namedScopesPlugin{id: "acme.dup", scopes: []pact.TokenScope{scope("acme.dup", "blog", "Again")}}
if names, err := TokenScopeNames(appWith(t, blog, dup)); err == nil || !strings.Contains(err.Error(), "duplicate token scope") {
t.Fatalf("duplicate scope: %v, %v", names, err)
}
reserved := namedScopesPlugin{id: "acme.bad", scopes: []pact.TokenScope{scope("acme.bad", "admin:write", "Bad")}}
if _, err := TokenScopeNames(appWith(t, reserved)); err == nil {
t.Fatal("a reserved scope name compiled")
}
})
t.Run("labels translate and pass unknown names through", func(t *testing.T) {
app := appWith(t, blog, shop)
if err := phrasebook.Activate(app, []party.Plugin{blog, shop}); err != nil {
t.Fatal(err)
}
names := []string{"*", "admin:read", "blog", "ghost", "shop:read", ""}
en := TokenScopeLabels(towel.WithLocale(context.Background(), "en"), app, names)
if want := []string{"Full access", "Read-only, whole admin", "Blog", "ghost", "Shop read", ""}; !slices.Equal(en, want) {
t.Fatalf("en labels = %q, want %q", en, want)
}
pl := TokenScopeLabels(towel.WithLocale(context.Background(), "pl"), app, names[:2])
if want := []string{"Pełny dostęp", "Tylko odczyt, cały panel"}; !slices.Equal(pl, want) {
t.Fatalf("pl labels = %q, want %q", pl, want)
}
})
t.Run("labels without a translator or catalog", func(t *testing.T) {
// No translator: a phrase-key label falls back to the key itself.
labels := TokenScopeLabels(nil, appWith(t, blog), []string{"blog", "admin:read", "ghost"})
if !slices.Equal(labels, []string{"Blog", "backend::lang.api_tokens.scope_admin_read", "ghost"}) {
t.Fatalf("labels without translator = %q", labels)
}
// A catalog that fails to compile: every name is its own label.
dup := namedScopesPlugin{id: "acme.dup", scopes: []pact.TokenScope{scope("acme.dup", "blog", "Again")}}
if labels := TokenScopeLabels(context.Background(), appWith(t, blog, dup), []string{"blog", "*"}); !slices.Equal(labels, []string{"blog", "*"}) {
t.Fatalf("labels of a broken catalog = %q", labels)
}
if labels := TokenScopeLabels(context.Background(), nil, nil); len(labels) != 0 {
t.Fatalf("labels of nothing = %q", labels)
}
})
}
type multiPermPlugin struct {
id string
perms []pact.Permission
scopes []pact.TokenScope
}
func (p multiPermPlugin) ID() string { return p.id }
func (multiPermPlugin) Requires() []string { return nil }
func (multiPermPlugin) Register(*backpack.App) error { return nil }
func (multiPermPlugin) Boot(*backpack.App) error { return nil }
func (p multiPermPlugin) Permissions() []pact.Permission { return p.perms }
func (p multiPermPlugin) TokenScopes() []pact.TokenScope { return p.scopes }
func TestTokenScopeCovers(t *testing.T) {
all := []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}
readOnly := []string{"GET", "HEAD"}
noScopes := multiPermPlugin{
id: "acme.bare",
perms: []pact.Permission{{Code: "acme.bare.write", Tab: "Bare", Label: "Write"}},
}
blog := multiPermPlugin{
id: "acme.blog",
perms: []pact.Permission{
{Code: "acme.blog.posts", Tab: "Blog", Label: "Posts"},
{Code: "acme.blog.tags", Tab: "Blog", Label: "Tags"},
},
scopes: []pact.TokenScope{
{
Name: "blog:read", Label: "Blog read", Description: "Read blog",
Permissions: []string{"acme.blog.*"}, Methods: readOnly,
},
{
Name: "blog:posts", Label: "Blog posts", Description: "Write posts",
Permissions: []string{"acme.blog.posts"}, Methods: all,
},
{
Name: "blog:tags", Label: "Blog tags", Description: "Write tags",
Permissions: []string{"acme.blog.tags"}, Methods: all,
},
},
}
blogAll := multiPermPlugin{
id: "acme.blog",
perms: blog.perms,
scopes: []pact.TokenScope{
{
Name: "blog", Label: "Blog", Description: "Full blog",
Permissions: []string{"acme.blog.*"}, Methods: all,
},
},
}
companion := multiPermPlugin{
id: "acme.companion",
scopes: []pact.TokenScope{
{
Name: "bare:companion", Label: "Bare companion", Description: "Covers acme.bare",
Permissions: []string{"acme.bare.*"}, Methods: all,
},
},
}
t.Run("wildcard * alone does not cover writes; GET and HEAD covered via admin:read", func(t *testing.T) {
plugins := []party.Plugin{noScopes}
for _, method := range []string{"POST", "PUT", "PATCH", "DELETE"} {
if TokenScopeCovers(nil, plugins, method, []string{"acme.bare.write"}) {
t.Fatalf("method %s should not be covered by * alone", method)
}
}
for _, method := range []string{"GET", "HEAD", " get "} {
if !TokenScopeCovers(nil, plugins, method, []string{"acme.bare.write"}) {
t.Fatalf("method %q should be covered via admin:read", method)
}
if !TokenScopeCovers(nil, plugins, method, nil) {
t.Fatalf("method %q with empty permissions should be covered via admin:read", method)
}
}
if TokenScopeCovers(nil, plugins, "", []string{"acme.bare.write"}) {
t.Fatal("empty method must return false")
}
})
t.Run("read-only scope fails POST; split scopes fail multi-perm; single scope covering all passes", func(t *testing.T) {
plugins := []party.Plugin{blog}
if !TokenScopeCovers(nil, plugins, "POST", []string{"acme.blog.posts"}) {
t.Fatal("blog:posts should cover POST acme.blog.posts")
}
if TokenScopeCovers(nil, plugins, "POST", []string{"acme.blog.posts", "acme.blog.tags"}) {
t.Fatal("multi-permission requirement split across two scopes must return false")
}
// Only read-only scope plugin.
roOnly := multiPermPlugin{
id: "acme.blog",
perms: blog.perms,
scopes: blog.scopes[:1],
}
if TokenScopeCovers(nil, []party.Plugin{roOnly}, "POST", []string{"acme.blog.posts"}) {
t.Fatal("read-only plugin scope must not cover POST")
}
// Single scope covering both permissions.
if !TokenScopeCovers(nil, []party.Plugin{blogAll}, "POST", []string{"acme.blog.posts", "acme.blog.tags"}) {
t.Fatal("blog wildcard scope should cover both permissions on POST")
}
// Empty permissions on POST must return false (matching runtime allowsUnderScope).
if TokenScopeCovers(nil, []party.Plugin{blogAll}, "POST", nil) {
t.Fatal("empty permissions on POST must return false")
}
})
t.Run("companion plugin scope covers first plugin write tool", func(t *testing.T) {
plugins := []party.Plugin{noScopes, companion}
if !TokenScopeCovers(nil, plugins, "DELETE", []string{"acme.bare.write"}) {
t.Fatal("companion plugin scope should cover acme.bare.write")
}
})
t.Run("activated cabana guard path and party.Activated fallback", func(t *testing.T) {
// Activated cabana guard path takes precedence even when plugins slice is nil.
reg, err := compileTokenCatalog([]party.Plugin{blogAll})
if err != nil {
t.Fatal(err)
}
activatedApp := backpack.New(nil)
if err := activatedApp.Publish(&backendResolver{guard: &compositeGuard{reg: reg}}); err != nil {
t.Fatal(err)
}
if !TokenScopeCovers(activatedApp, nil, "POST", []string{"acme.blog.posts", "acme.blog.tags"}) {
t.Fatal("activated guard path should cover blog write permissions")
}
if TokenScopeCovers(activatedApp, nil, "POST", []string{"acme.bare.write"}) {
t.Fatal("activated guard path should not cover unrelated permission")
}
// party.Activated fallback when cabana is not activated and plugins slice is nil.
bootApp := backpack.New(nil)
if err := bootApp.Publish(party.Activated([]party.Plugin{blogAll})); err != nil {
t.Fatal(err)
}
if !TokenScopeCovers(bootApp, nil, "PUT", []string{"acme.blog.posts"}) {
t.Fatal("party.Activated fallback should cover acme.blog.posts")
}
if TokenScopeCovers(bootApp, nil, "PUT", []string{"acme.bare.write"}) {
t.Fatal("party.Activated fallback should not cover unrelated permission")
}
})
}