v0.84.0: catalog-driven initial_credentials — read an app's auto-generated first-login from a file and show it on the app page

This commit is contained in:
2026-06-26 11:00:06 +02:00
parent 49f6dbf847
commit 1705d71dd5
7 changed files with 341 additions and 0 deletions
@@ -0,0 +1,73 @@
package stacks
import "testing"
func TestParseInitialCredsJSON(t *testing.T) {
spec := &InitialCredentials{Format: "json", UsernameKey: "username", PasswordKey: "password"}
c, err := parseInitialCreds(spec, `{"username":"admin","password":"s3cr3t!","info":"ignore me"}`)
if err != nil {
t.Fatal(err)
}
if c.Username != "admin" || c.Password != "s3cr3t!" {
t.Fatalf("json parse wrong: %+v", c)
}
}
func TestParseInitialCredsJSONDefaultPasswordKey(t *testing.T) {
// password_key omitted → defaults to "password"; no username key → empty username.
spec := &InitialCredentials{Format: "json"}
c, err := parseInitialCreds(spec, `{"password":"pw123"}`)
if err != nil {
t.Fatal(err)
}
if c.Username != "" || c.Password != "pw123" {
t.Fatalf("default-key parse wrong: %+v", c)
}
}
func TestParseInitialCredsJSONInvalid(t *testing.T) {
spec := &InitialCredentials{Format: "json", PasswordKey: "password"}
if _, err := parseInitialCreds(spec, `not json`); err == nil {
t.Fatal("expected error on invalid JSON")
}
}
func TestParseInitialCredsRegex(t *testing.T) {
spec := &InitialCredentials{
Format: "regex",
UsernamePattern: `(?m)^user:\s*(\S+)`,
PasswordPattern: `(?m)^pass:\s*(\S+)`,
}
c, err := parseInitialCreds(spec, "user: root\npass: hunter2\n")
if err != nil {
t.Fatal(err)
}
if c.Username != "root" || c.Password != "hunter2" {
t.Fatalf("regex parse wrong: %+v", c)
}
}
func TestParseInitialCredsRegexNoPattern(t *testing.T) {
spec := &InitialCredentials{Format: "regex"}
if _, err := parseInitialCreds(spec, "anything"); err == nil {
t.Fatal("expected error when password_pattern missing")
}
}
func TestParseInitialCredsPlain(t *testing.T) {
spec := &InitialCredentials{Format: "plain"}
c, err := parseInitialCreds(spec, " topsecret\n")
if err != nil {
t.Fatal(err)
}
if c.Password != "topsecret" || c.Username != "" {
t.Fatalf("plain parse wrong: %+v", c)
}
}
func TestParseInitialCredsUnknownFormat(t *testing.T) {
spec := &InitialCredentials{Format: "toml"}
if _, err := parseInitialCreds(spec, "x = 1"); err == nil {
t.Fatal("expected error on unknown format")
}
}