package console import ( "bytes" "context" "crypto/ecdh" "encoding/base64" "encoding/json" "errors" "os" "testing" "github.com/carlosframework/platform/internal/appconfig" "github.com/carlosframework/platform/internal/moves" "github.com/carlosframework/platform/internal/release" ) // D13: the console publishes control/moves-console.json only with the // moves switch on, and a second start replaces the first start's record. func TestConsoleRecordNeedsTheSwitch(t *testing.T) { ctx := context.Background() priv, err := ecdh.P256().NewPrivateKey(bytes.Repeat([]byte{1}, 32)) if err != nil { t.Fatal(err) } pubB64 := base64.StdEncoding.EncodeToString(priv.PublicKey().Bytes()) store := release.NewDirStore(t.TempDir()) off := &Server{cfg: Config{Releases: store, SecretsPub: pubB64}} if err := off.PublishMovesConsole(ctx); err != nil { t.Fatal(err) } if _, _, err := store.Get(ctx, moves.ConsoleKey); !errors.Is(err, os.ErrNotExist) { t.Fatalf("switch off, yet %s: %v", moves.ConsoleKey, err) } on := &Server{cfg: Config{Releases: store, SecretsPub: pubB64, MovesEnabled: true}} for start := 0; start < 2; start++ { if err := on.PublishMovesConsole(ctx); err != nil { t.Fatalf("start %d: %v", start, err) } } body, _, err := store.Get(ctx, moves.ConsoleKey) if err != nil { t.Fatal(err) } var rec moves.ConsoleRecord if err := json.Unmarshal(body, &rec); err != nil { t.Fatal(err) } if rec.V != 1 || rec.Protocol != moves.ProtocolVersion || rec.SecretsKeyFP != appconfig.KeyFingerprint(priv.PublicKey()) || rec.At.IsZero() { t.Fatalf("record = %+v", rec) } // No secrets key: the record says so by carrying no fingerprint. bare := &Server{cfg: Config{Releases: store, MovesEnabled: true}} if err := bare.PublishMovesConsole(ctx); err != nil { t.Fatal(err) } body, _, _ = store.Get(ctx, moves.ConsoleKey) if bytes.Contains(body, []byte("secrets_key_fp")) { t.Fatalf("no key, yet %s", body) } // A bucketless console has nowhere to publish and does not fail. if err := (&Server{cfg: Config{MovesEnabled: true}}).PublishMovesConsole(ctx); err != nil { t.Fatal(err) } }