package console import ( "context" "net/http" "strings" "testing" "time" "github.com/carlosframework/platform/internal/appconfig" "github.com/carlosframework/platform/internal/objstore" "github.com/carlosframework/platform/internal/release" ) // The Environments section is the first console surface for the config // bundles themselves. Until it existed the ladder showed which environment a // route was wired to, and nothing showed what the environments WERE. func TestSettingsPageShowsEnvironments(t *testing.T) { ctx := context.Background() fx := newConsoleFixture(t) freshApp(t, fx, "sprout") // A bundle with one plain var and one sealed one. The envelope is opaque // to appconfig at set time, so it needs no real key here. if _, err := appconfig.ApplyEnvironment(ctx, fx.rel, release.OpsAccount, "sprout", "acme", map[string]appconfig.Var{ "APP_ORIGIN": {Kind: "env", Value: "https://acme.example.com"}, "ACME_API_KEY": {Kind: "secret", Envelope: "sealed-envelope-bytes"}, }, nil); err != nil { t.Fatal(err) } // ...and a store of its own, provisioned, which nothing reads. rec := &objstore.Record{ Declared: &objstore.Declared{EnvPrefix: "CARLOS_STORE", At: time.Now(), By: "member@example.com"}, Grant: &objstore.GrantBlock{Backend: "mem", Bucket: "carlos-bab-sprout-acme", Source: objstore.GrantSourceOperator, At: time.Now(), By: "op"}, Provisioned: &objstore.Provisioned{Bucket: "carlos-bab-sprout-acme", Region: "eu-west-1", IAMUser: "u", KeyID: "K", At: time.Now()}, } if _, err := objstore.PutIn(ctx, fx.rel, release.OpsAccount, "sprout", "acme", rec, ""); err != nil { t.Fatal(err) } c := signInPaul(t, fx) status, _, body := get(t, c, fx.u("/apps/sprout/settings/environments")) if status != http.StatusOK { t.Fatalf("environments page = %d:\n%s", status, body) } for _, want := range []string{ "acme", // the bundle "APP_ORIGIN", // a name it sets "ACME_API_KEY", // including the sealed one, by name "carlos-bab-sprout-acme", // the store that belongs to it "no instances", // nothing is bound to it "no instance reads it", // ...so the bucket is billing for nobody } { if !strings.Contains(body, want) { t.Fatalf("the environments section is missing %q:\n%s", want, body) } } // NAMES, never values. This page has never shown a config value, and an // environment bundle is exactly where a per-tenant origin or credential // sits — so this is the assertion that keeps it that way. if strings.Contains(body, "https://acme.example.com") { t.Fatalf("the environments section printed a config VALUE:\n%s", body) } if strings.Contains(body, "sealed-envelope-bytes") { t.Fatalf("the environments section printed a sealed envelope:\n%s", body) } } // An app with no named environments says so, and says how to make one — // rather than rendering an empty shell that reads like a failure. func TestSettingsPageEnvironmentsEmptyState(t *testing.T) { fx := newConsoleFixture(t) freshApp(t, fx, "bare") c := signInPaul(t, fx) status, _, body := get(t, c, fx.u("/apps/bare/settings/environments")) if status != http.StatusOK { t.Fatalf("environments page = %d:\n%s", status, body) } if !strings.Contains(body, "No named environments") { t.Fatalf("no empty-state line:\n%s", body) } if !strings.Contains(body, "carlos env set --app bare") { t.Fatalf("the empty state does not say how to make one:\n%s", body) } }