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") } }