feat(build): make summer new apps admin-ready

- NewOptions.AdminSecret: summer new writes SUMMER_ADMIN__JWT__SECRET
  to .env (still 0600), generated from 32 crypto/rand bytes when empty
  and validated as at least 32 .env-safe characters
- .env.example gets an empty SUMMER_ADMIN__JWT__SECRET= placeholder
  with a comment; the secret never reaches the printed next steps
- The next steps and the generated CLAUDE.md explain the admin at
  /backend: make:admin-controller, build, migrate and
  summer admin:create --email you@example.com --superuser
- Goldens use a fixed, non-secret admin secret fixture
- Scaffolding, installation and users-and-permissions docs updated
This commit is contained in:
Jakub Zych
2026-10-08 17:20:19 +02:00
parent da21670870
commit f70d80ec7e
13 changed files with 264 additions and 36 deletions

View File

@@ -2,6 +2,7 @@ package build
import (
"bytes"
"encoding/base64"
"go/token"
"io/fs"
"os"
@@ -642,3 +643,88 @@ func TestNewNoGit(t *testing.T) {
t.Fatalf("output lacks the created line:\n%s", out.String())
}
}
// envValue returns the value of key in an env file body, or "" and false.
func envValue(body []byte, key string) (string, bool) {
for _, line := range strings.Split(string(body), "\n") {
if v, ok := strings.CutPrefix(line, key+"="); ok {
return v, true
}
}
return "", false
}
// TestNewAdminSecret writes SUMMER_ADMIN__JWT__SECRET to .env only, generates
// a random one when none is given and rejects values unsafe for .env.
func TestNewAdminSecret(t *testing.T) {
const key = "SUMMER_ADMIN__JWT__SECRET"
t.Run("explicit", func(t *testing.T) {
const secret = "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWY="
parent := parentWithFramework(t)
var out bytes.Buffer
dir, err := NewApp(t.Context(), NewOptions{Name: "demo", Parent: parent, AppKey: testAppKey, AdminSecret: secret, NoGit: true, NoPlugin: true}, &out)
if err != nil {
t.Fatal(err)
}
env := readAppFile(t, dir, ".env")
if !strings.Contains(string(env), "\n"+key+"="+secret+"\n") {
t.Fatalf(".env does not hold %s=%s:\n%s", key, secret, env)
}
st, err := os.Stat(filepath.Join(dir, ".env"))
if err != nil {
t.Fatal(err)
}
if st.Mode().Perm() != 0o600 {
t.Fatalf(".env mode = %o, want 600", st.Mode().Perm())
}
example := readAppFile(t, dir, ".env.example")
if v, ok := envValue(example, key); !ok || v != "" {
t.Fatalf(".env.example %s = (%q, %v), want an empty placeholder", key, v, ok)
}
if strings.Contains(string(example), secret) {
t.Fatal(".env.example contains the admin secret")
}
if strings.Contains(out.String(), secret) {
t.Fatal("the next steps print the admin secret")
}
})
t.Run("generated", func(t *testing.T) {
var seen []string
for range 2 {
parent := parentWithFramework(t)
dir, err := NewApp(t.Context(), NewOptions{Name: "demo", Parent: parent, AppKey: testAppKey, NoGit: true, NoPlugin: true}, nil)
if err != nil {
t.Fatal(err)
}
v, ok := envValue(readAppFile(t, dir, ".env"), key)
if !ok {
t.Fatalf(".env has no %s", key)
}
raw, err := base64.StdEncoding.DecodeString(v)
if err != nil || len(raw) != 32 {
t.Fatalf("generated secret %q is not the base64 of 32 bytes (%v)", v, err)
}
seen = append(seen, v)
}
if seen[0] == seen[1] {
t.Fatal("two apps got the same admin secret")
}
})
t.Run("invalid", func(t *testing.T) {
for _, secret := range []string{
"short",
"abcdefghijklmnopqrst uvwxyzABCDEFGHIJKLMN",
"abcdefghijklmnopqrst\nuvwxyzABCDEFGHIJKLMN",
} {
parent := parentWithFramework(t)
_, err := NewApp(t.Context(), NewOptions{Name: "demo", Parent: parent, AppKey: testAppKey, AdminSecret: secret, NoGit: true, NoPlugin: true}, nil)
if err == nil || !strings.Contains(err.Error(), "admin secret") {
t.Fatalf("NewApp(AdminSecret %q) = %v, want an admin secret error", secret, err)
}
assertDirEntries(t, parent, []string{"summercms"})
}
})
}