- wristband: TestAuthorizeConsentPath, TestAuthorizeDefaultScopes - pier: TestBackendOnlyGrant, TestLinkedGrantNoScopeLimit (Docker) - cabana: TestTokenScopeNames, TestMCPAdminConsent, TestAccountContributionItems, TestBackendAccessRevokedEvent (Docker); export_test.go exposes the event helper for the de-duplication case
125 lines
5.3 KiB
Go
125 lines
5.3 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)
|
|
}
|
|
})
|
|
}
|