| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/ecdh" |
| 6 | "encoding/base64" |
| 7 | "log" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/appconfig" |
| 12 | ) |
| 13 | |
| 14 | // mustTestSecretsKey is the fixed P-256 key every R9 test in this package |
| 15 | // uses: its public half as CARLOS_SECRETS_PUBKEY carries it, and the |
| 16 | // fingerprint a box holding it publishes. |
| 17 | func mustTestSecretsKey() (pubB64, fp string) { |
| 18 | priv, err := ecdh.P256().NewPrivateKey(bytes.Repeat([]byte{1}, 32)) |
| 19 | if err != nil { |
| 20 | panic(err) |
| 21 | } |
| 22 | return base64.StdEncoding.EncodeToString(priv.PublicKey().Bytes()), appconfig.KeyFingerprint(priv.PublicKey()) |
| 23 | } |
| 24 | |
| 25 | func TestAppRegionsOnNeedsANamedHome(t *testing.T) { |
| 26 | fx := newConsoleFixture(t) |
| 27 | for _, c := range []struct { |
| 28 | on bool |
| 29 | home string |
| 30 | want bool |
| 31 | }{{false, "ie", false}, {true, "", false}, {true, "ie", true}} { |
| 32 | fx.srv.cfg.AppRegions, fx.srv.cfg.HomeRegion = c.on, c.home |
| 33 | if got := fx.srv.appRegionsOn(); got != c.want { |
| 34 | t.Errorf("AppRegions=%v HomeRegion=%q: appRegionsOn = %v, want %v", c.on, c.home, got, c.want) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestExpectedSecretsFP(t *testing.T) { |
| 40 | fx := newConsoleFixture(t) |
| 41 | pub, fp := mustTestSecretsKey() |
| 42 | fx.srv.cfg.SecretsPub = pub |
| 43 | if got := fx.srv.expectedSecretsFP(); got != fp { |
| 44 | t.Fatalf("expectedSecretsFP = %q, want %q", got, fp) |
| 45 | } |
| 46 | fx.srv.cfg.SecretsPub = "" |
| 47 | if got := fx.srv.expectedSecretsFP(); got != "" { |
| 48 | t.Fatalf("no CARLOS_SECRETS_PUBKEY = %q, want empty (cannot judge)", got) |
| 49 | } |
| 50 | fx.srv.cfg.SecretsPub = "not a key" |
| 51 | if got := fx.srv.expectedSecretsFP(); got != "" { |
| 52 | t.Fatalf("an unparseable CARLOS_SECRETS_PUBKEY = %q, want empty (cannot judge)", got) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestAppRegionUsesTheSeamWhenSet(t *testing.T) { |
| 57 | fx := newConsoleFixture(t) |
| 58 | if r, src, err := fx.srv.appRegion(OpsAccountID, "nobody"); err != nil || r != "" || src != "" { |
| 59 | t.Fatalf("store read of a missing app = %q %q %v, want empty", r, src, err) |
| 60 | } |
| 61 | fx.srv.appRegionOf = func(id int64, app string) (string, string, error) { |
| 62 | return "uk", "timezone", nil |
| 63 | } |
| 64 | if r, src, err := fx.srv.appRegion(OpsAccountID, "nobody"); err != nil || r != "uk" || src != "timezone" { |
| 65 | t.Fatalf("seam read = %q %q %v, want uk timezone", r, src, err) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestLogRegionConfig(t *testing.T) { |
| 70 | var buf bytes.Buffer |
| 71 | prev := log.Writer() |
| 72 | log.SetOutput(&buf) |
| 73 | defer log.SetOutput(prev) |
| 74 | logRegionConfig(Config{AppRegions: true}) |
| 75 | out := buf.String() |
| 76 | if !strings.Contains(out, "CONSOLE_APP_REGIONS=on but CONSOLE_HOME_REGION is unset, so app regions stay off until the home is named") || |
| 77 | !strings.Contains(out, "CARLOS_SECRETS_PUBKEY is unset, so no region outside the home can be chosen automatically") { |
| 78 | t.Fatalf("missing startup lines: %s", out) |
| 79 | } |
| 80 | // Switch off: the guess endpoint still answers, so a missing secrets |
| 81 | // key is still worth saying (codex plan-R2 8); an unnamed home is not. |
| 82 | buf.Reset() |
| 83 | logRegionConfig(Config{}) |
| 84 | if out := buf.String(); !strings.Contains(out, "CARLOS_SECRETS_PUBKEY is unset") || strings.Contains(out, "CONSOLE_HOME_REGION") { |
| 85 | t.Fatalf("switch off logged %q, want only the secrets-key line", out) |
| 86 | } |
| 87 | buf.Reset() |
| 88 | pub, _ := mustTestSecretsKey() |
| 89 | logRegionConfig(Config{AppRegions: true, HomeRegion: "ie", SecretsPub: pub}) |
| 90 | if buf.Len() != 0 { |
| 91 | t.Fatalf("a complete configuration logged: %s", buf.String()) |
| 92 | } |
| 93 | } |
| 94 | |