feat(16.3-01): admin:token CLI, party.Activated, READMEs and docs
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package cabana_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
@@ -12,6 +14,7 @@ import (
|
||||
"testing/fstest"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||
"git.golem15.com/golem15/summercms/modules/compass"
|
||||
"git.golem15.com/golem15/summercms/modules/lagoon"
|
||||
@@ -249,6 +252,79 @@ func TestAccountTokensSmoke(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenCommandsSmoke(t *testing.T) {
|
||||
gdb := adminGorm(t)
|
||||
if err := gdb.AutoMigrate(&tokenDemoWidget{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app := commandApp(t, gdb)
|
||||
demo := tokenDemoPlugin{fsys: tokenDemoFS()}
|
||||
if err := app.Publish(party.Activated{demo}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := insertAdmin(t, gdb, "tok-cli", "tok-cli@example.test", "secret-pass", true, false)
|
||||
|
||||
create := commandByName(t, cabana.RuntimeCommands(app), "admin:token:create")
|
||||
var createOut bytes.Buffer
|
||||
if err := create.Run(context.Background(), flagInput{
|
||||
args: []string{user.Login},
|
||||
flags: map[string]string{
|
||||
"name": "cli-agent",
|
||||
"expires": "90d",
|
||||
},
|
||||
multiFlags: map[string][]string{"scope": {"demo"}},
|
||||
}, bonfire.NewOutput(nil, &createOut, &bytes.Buffer{})); err != nil {
|
||||
t.Fatalf("admin:token:create: %v", err)
|
||||
}
|
||||
plain := strings.TrimSpace(createOut.String())
|
||||
if !wellFormedPlain(plain) {
|
||||
t.Fatalf("create stdout %q", createOut.String())
|
||||
}
|
||||
if strings.Count(createOut.String(), "\n") != 1 || !strings.HasSuffix(createOut.String(), "\n") {
|
||||
t.Fatalf("create must print exactly one token line, got %q", createOut.String())
|
||||
}
|
||||
|
||||
list := commandByName(t, cabana.RuntimeCommands(app), "admin:token:list")
|
||||
var listOut bytes.Buffer
|
||||
if err := list.Run(context.Background(), flagInput{
|
||||
args: []string{user.Login},
|
||||
}, bonfire.NewOutput(nil, &listOut, &bytes.Buffer{})); err != nil {
|
||||
t.Fatalf("admin:token:list: %v", err)
|
||||
}
|
||||
listText := listOut.String()
|
||||
if strings.Contains(listText, plain) {
|
||||
t.Fatal("list leaked plaintext token")
|
||||
}
|
||||
hint := plain[len(plain)-4:]
|
||||
if !strings.Contains(listText, "g15_…"+hint) {
|
||||
t.Fatalf("list missing hint, got %q", listText)
|
||||
}
|
||||
|
||||
var row cabana.BackendAPIToken
|
||||
if err := gdb.Where("backend_user_id = ? AND revoked_at IS NULL", user.ID).First(&row).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
revoke := commandByName(t, cabana.RuntimeCommands(app), "admin:token:revoke")
|
||||
var revokeOut bytes.Buffer
|
||||
if err := revoke.Run(context.Background(), flagInput{
|
||||
args: []string{strconv.FormatUint(uint64(row.ID), 10)},
|
||||
}, bonfire.NewOutput(nil, &revokeOut, &bytes.Buffer{})); err != nil {
|
||||
t.Fatalf("admin:token:revoke: %v", err)
|
||||
}
|
||||
|
||||
scopes := commandByName(t, cabana.RuntimeCommands(app), "admin:token:scopes")
|
||||
var scopesOut bytes.Buffer
|
||||
if err := scopes.Run(context.Background(), flagInput{}, bonfire.NewOutput(nil, &scopesOut, &bytes.Buffer{})); err != nil {
|
||||
t.Fatalf("admin:token:scopes: %v", err)
|
||||
}
|
||||
scopesText := scopesOut.String()
|
||||
for _, name := range []string{"*", "admin:read", "demo"} {
|
||||
if !strings.Contains(scopesText, name) {
|
||||
t.Fatalf("scopes missing %q in %q", name, scopesText)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustJSON(t *testing.T, v any) []byte {
|
||||
t.Helper()
|
||||
b, err := json.Marshal(v)
|
||||
|
||||
Reference in New Issue
Block a user