| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/appconfig" |
| 11 | "github.com/carlosframework/platform/internal/objstore" |
| 12 | "github.com/carlosframework/platform/internal/release" |
| 13 | ) |
| 14 | |
| 15 | // The Environments section is the first console surface for the config |
| 16 | // bundles themselves. Until it existed the ladder showed which environment a |
| 17 | // route was wired to, and nothing showed what the environments WERE. |
| 18 | |
| 19 | func TestSettingsPageShowsEnvironments(t *testing.T) { |
| 20 | ctx := context.Background() |
| 21 | fx := newConsoleFixture(t) |
| 22 | freshApp(t, fx, "sprout") |
| 23 | |
| 24 | // A bundle with one plain var and one sealed one. The envelope is opaque |
| 25 | // to appconfig at set time, so it needs no real key here. |
| 26 | if _, err := appconfig.ApplyEnvironment(ctx, fx.rel, release.OpsAccount, "sprout", "acme", |
| 27 | map[string]appconfig.Var{ |
| 28 | "APP_ORIGIN": {Kind: "env", Value: "https://acme.example.com"}, |
| 29 | "ACME_API_KEY": {Kind: "secret", Envelope: "sealed-envelope-bytes"}, |
| 30 | }, nil); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | // ...and a store of its own, provisioned, which nothing reads. |
| 34 | rec := &objstore.Record{ |
| 35 | Declared: &objstore.Declared{EnvPrefix: "CARLOS_STORE", At: time.Now(), By: "member@example.com"}, |
| 36 | Grant: &objstore.GrantBlock{Backend: "mem", Bucket: "carlos-bab-sprout-acme", Source: objstore.GrantSourceOperator, At: time.Now(), By: "op"}, |
| 37 | Provisioned: &objstore.Provisioned{Bucket: "carlos-bab-sprout-acme", Region: "eu-west-1", IAMUser: "u", KeyID: "K", At: time.Now()}, |
| 38 | } |
| 39 | if _, err := objstore.PutIn(ctx, fx.rel, release.OpsAccount, "sprout", "acme", rec, ""); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | |
| 43 | c := signInPaul(t, fx) |
| 44 | status, _, body := get(t, c, fx.u("/apps/sprout/settings/environments")) |
| 45 | if status != http.StatusOK { |
| 46 | t.Fatalf("environments page = %d:\n%s", status, body) |
| 47 | } |
| 48 | for _, want := range []string{ |
| 49 | "acme", // the bundle |
| 50 | "APP_ORIGIN", // a name it sets |
| 51 | "ACME_API_KEY", // including the sealed one, by name |
| 52 | "carlos-bab-sprout-acme", // the store that belongs to it |
| 53 | "no instances", // nothing is bound to it |
| 54 | "no instance reads it", // ...so the bucket is billing for nobody |
| 55 | } { |
| 56 | if !strings.Contains(body, want) { |
| 57 | t.Fatalf("the environments section is missing %q:\n%s", want, body) |
| 58 | } |
| 59 | } |
| 60 | // NAMES, never values. This page has never shown a config value, and an |
| 61 | // environment bundle is exactly where a per-tenant origin or credential |
| 62 | // sits — so this is the assertion that keeps it that way. |
| 63 | if strings.Contains(body, "https://acme.example.com") { |
| 64 | t.Fatalf("the environments section printed a config VALUE:\n%s", body) |
| 65 | } |
| 66 | if strings.Contains(body, "sealed-envelope-bytes") { |
| 67 | t.Fatalf("the environments section printed a sealed envelope:\n%s", body) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // An app with no named environments says so, and says how to make one — |
| 72 | // rather than rendering an empty shell that reads like a failure. |
| 73 | func TestSettingsPageEnvironmentsEmptyState(t *testing.T) { |
| 74 | fx := newConsoleFixture(t) |
| 75 | freshApp(t, fx, "bare") |
| 76 | c := signInPaul(t, fx) |
| 77 | |
| 78 | status, _, body := get(t, c, fx.u("/apps/bare/settings/environments")) |
| 79 | if status != http.StatusOK { |
| 80 | t.Fatalf("environments page = %d:\n%s", status, body) |
| 81 | } |
| 82 | if !strings.Contains(body, "No named environments") { |
| 83 | t.Fatalf("no empty-state line:\n%s", body) |
| 84 | } |
| 85 | if !strings.Contains(body, "carlos env set --app bare") { |
| 86 | t.Fatalf("the empty state does not say how to make one:\n%s", body) |
| 87 | } |
| 88 | } |
| 89 | |