| new file mode 100644 |
| index 000000000..6f150ee80 |
| --- /dev/null |
| +++ b/internal/console/apiconfig_witness_test.go |
| @@ -0,0 +1,157 @@ |
| +package console |
| + |
| +// Witnesses from the config receipts branch review (#522 plan 1, round 1): |
| +// a supplied precondition never vanishes, and a team is authorised on the |
| +// snapshot it is shown. |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "io" |
| + "net/http" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/appconfig" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// reqWithHeaders is rawReq with any number of values per header. |
| +func reqWithHeaders(t *testing.T, method, u, bearer, body string, hdr http.Header) (int, string) { |
| + t.Helper() |
| + req, err := http.NewRequest(method, u, strings.NewReader(body)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + req.Header.Set("Content-Type", "application/json") |
| + req.Header.Set("Authorization", "Bearer "+bearer) |
| + for k, vs := range hdr { |
| + for _, v := range vs { |
| + req.Header.Add(k, v) |
| + } |
| + } |
| + res, err := http.DefaultClient.Do(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer res.Body.Close() |
| + out, err := io.ReadAll(res.Body) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return res.StatusCode, string(out) |
| +} |
| + |
| +// TestAPIConfigPutRefusesUnusablePrecondition: an If-Match or if_version |
| +// that is present but empty, a wildcard, a weak tag or a list is refused |
| +// with 400 before anything is read. None of them may fall back to an |
| +// unconditional write, and none is a 412: they are not versions at all. |
| +func TestAPIConfigPutRefusesUnusablePrecondition(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-precondition-forms") |
| + seedDefault(t, fx, token, envSet("A", "1")) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + cur := storedVersion(t, fx, key) |
| + stored := rawAtKey(t, fx, key) |
| + gen := genEtag(t, fx, "titogo") |
| + cs := useCfgCountStore(fx, true) |
| + u := configURL(fx, "titogo") |
| + plain := putBody(t, map[string]any{"set": envSet("B", "2")}) |
| + |
| + for _, c := range []struct { |
| + what string |
| + body string |
| + hdr http.Header |
| + }{ |
| + {"an empty quoted If-Match", plain, http.Header{"If-Match": {`""`}}}, |
| + {"an empty If-Match", plain, http.Header{"If-Match": {""}}}, |
| + {"a blank If-Match", plain, http.Header{"If-Match": {" "}}}, |
| + {"a wildcard If-Match", plain, http.Header{"If-Match": {"*"}}}, |
| + {"a weak If-Match of the current version", plain, http.Header{"If-Match": {`W/"` + cur + `"`}}}, |
| + {"an If-Match list", plain, http.Header{"If-Match": {`"` + cur + `", "cv1:00"`}}}, |
| + {"two If-Match headers", plain, http.Header{"If-Match": {`"` + cur + `"`, `"` + cur + `"`}}}, |
| + {"an unbalanced quote", plain, http.Header{"If-Match": {`"` + cur}}}, |
| + {"an empty if_version", `{"if_version":"","set":{"B":{"kind":"env","value":"2"}}}`, nil}, |
| + {"a blank if_version", `{"if_version":" ","set":{"B":{"kind":"env","value":"2"}}}`, nil}, |
| + {"an empty if_version beside a good header", `{"if_version":"","set":{"B":{"kind":"env","value":"2"}}}`, http.Header{"If-Match": {cur}}}, |
| + } { |
| + before := cs.getCount() |
| + status, out := reqWithHeaders(t, http.MethodPut, u, token, c.body, c.hdr) |
| + if status != http.StatusBadRequest { |
| + t.Errorf("%s = %d %s, want 400", c.what, status, out) |
| + } |
| + var e map[string]any |
| + if json.Unmarshal([]byte(out), &e) != nil || e["error"] == "" || strings.Contains(out, cur) { |
| + t.Errorf("%s: body %s, want an error naming no version", c.what, out) |
| + } |
| + if n := cs.getCount() - before; n != 0 { |
| + t.Errorf("%s read the store %d times, want 0", c.what, n) |
| + } |
| + if !bytes.Equal(rawAtKey(t, fx, key), stored) { |
| + t.Fatalf("%s changed the stored bytes", c.what) |
| + } |
| + if genEtag(t, fx, "titogo") != gen { |
| + t.Fatalf("%s bumped the generation", c.what) |
| + } |
| + } |
| + |
| + // An absent precondition is today's unconditional write, and a present |
| + // usable one still works, quoted or not. |
| + if status, out := reqWithHeaders(t, http.MethodPut, u, token, putBody(t, map[string]any{"if_version": cur, "set": envSet("B", "2")}), nil); status != http.StatusOK { |
| + t.Fatalf("a usable if_version = %d %s", status, out) |
| + } |
| + if status, out := reqWithHeaders(t, http.MethodPut, u, token, plain, nil); status != http.StatusOK { |
| + t.Fatalf("no precondition = %d %s", status, out) |
| + } |
| +} |
| + |
| +// TestAPIConfigTeamGetOwnershipRace: a team GET is authorised on the bytes |
| +// it answers with. A bundle that changes hands after the scope step, and |
| +// gains a var and a version on the way, is the canonical 404, carrying |
| +// nothing of the new bytes. |
| +func TestAPIConfigTeamGetOwnershipRace(t *testing.T) { |
| + for _, c := range []struct { |
| + what string |
| + owner string |
| + }{{"released", ""}, {"transferred", "beta"}} { |
| + t.Run(c.what, func(t *testing.T) { |
| + w := newTeamWorld(t, "code-team-get-race-"+c.what) |
| + ctx := context.Background() |
| + key := appconfig.EnvironmentKey(release.OpsAccount, "titogo", "team") |
| + var fired atomic.Bool |
| + var hookErr error |
| + h := func(r *http.Request) { |
| + if r.URL.Path != "/api/cli/apps/bab/titogo/config" || fired.Swap(true) { |
| + return |
| + } |
| + if _, err := appconfig.ApplyEnvironmentOwned(ctx, w.fx.rel, "bab", "titogo", "team", "alta", |
| + map[string]appconfig.Var{"LATER": {Kind: "env", Value: "later-canary"}}, nil); err != nil { |
| + hookErr = err |
| + return |
| + } |
| + if err := appconfig.SetEnvironmentOwner(ctx, w.fx.rel, "bab", "titogo", "team", "", nil); err != nil { |
| + hookErr = err |
| + return |
| + } |
| + if c.owner != "" { |
| + hookErr = appconfig.SetEnvironmentOwner(ctx, w.fx.rel, "bab", "titogo", "team", c.owner, nil) |
| + } |
| + } |
| + teamScopedHook.Store(&h) |
| + t.Cleanup(func() { teamScopedHook.Store(nil) }) |
| + status, hdr, body := rawReq(t, http.MethodGet, w.url("/config?environment=team"), w.team, "") |
| + teamScopedHook.Store(nil) |
| + if !fired.Load() || hookErr != nil { |
| + t.Fatalf("the bundle did not change hands after the scope step (fired %v): %v", fired.Load(), hookErr) |
| + } |
| + assertNotFound(t, "team GET after the bundle changed hands", canonicalNotFound(t), status, hdr, body) |
| + now := storedVersion(t, w.fx, key) |
| + for _, leak := range []string{now, "LATER", "later-canary", "TEAM_ONLY"} { |
| + if strings.Contains(body, leak) { |
| + t.Errorf("the 404 carries %q: %s", leak, body) |
| + } |
| + } |
| + }) |
| + } |
| +} |