Files
allmende/cmd/allmende/init_test.go
T
nilsandClaude Sonnet 5.5 7120959cb8 init --no-phrase: an identity without a recovery phrase
For bootstrap nodes and other unattended setups: nothing is shown or logged,
and the data directory is marked so later messages do not point to
'allmende recover', which cannot work. The identity lasts the certificate
lifetime (a year) and then has to be replaced; the node says so.

The Kubernetes StatefulSet now initialises itself on first start with it,
so no manual exec step is left.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
2026-11-18 12:25:54 +01:00

55 lines
1.4 KiB
Go

package main
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"git.stinnesbeck.com/nils/allmende/internal/identity"
)
// captureStdout runs f and returns what it printed.
func captureStdout(t *testing.T, f func()) string {
t.Helper()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
old := os.Stdout
os.Stdout = w
f()
os.Stdout = old
w.Close()
b, _ := io.ReadAll(r)
return string(b)
}
func TestInitWithoutPhraseShowsNoneAndMarksTheDirectory(t *testing.T) {
dir := t.TempDir()
var err error
out := captureStdout(t, func() { err = cmdInit([]string{"--data", dir, "--no-phrase"}) })
if err != nil {
t.Fatal(err)
}
if strings.Contains(out, "recovery phrase\n---") || strings.Contains(out, " 1. ") {
t.Fatalf("no phrase may be shown:\n%s", out)
}
if !strings.Contains(out, "cannot be recovered") {
t.Errorf("the owner must be told it cannot be recovered:\n%s", out)
}
for _, f := range []string{"device.key", tokenFile, noRecoveryFile} {
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
t.Errorf("%s was not written: %v", f, err)
}
}
if _, err := identity.LoadDevice(dir); err != nil {
t.Errorf("the node could not load its identity: %v", err)
}
// A second init must not replace the identity.
if err := cmdInit([]string{"--data", dir, "--no-phrase"}); err == nil {
t.Error("init over an existing identity must fail")
}
}