| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // A console older than per-environment stores does not read ?environment= at |
| 9 | // all: it answers about the app's OWN store, with a 200 and an ordinary body. |
| 10 | // Every console that knows about them echoes the environment on every reply, |
| 11 | // so a reply that does not name what we asked about is the tell. |
| 12 | // |
| 13 | // This matters because the verbs behind it are not all reversible: create |
| 14 | // fixes the env prefix once granted, grant mints a bucket and an IAM user, |
| 15 | // and rotate cycles a live credential. Acting on the wrong store and |
| 16 | // reporting success is the failure mode being closed here. |
| 17 | func TestStoreEnvMismatch(t *testing.T) { |
| 18 | cases := []struct { |
| 19 | name string |
| 20 | reply apiStoreReply |
| 21 | asked string |
| 22 | wantErr bool |
| 23 | }{ |
| 24 | {"app store, nothing asked", apiStoreReply{Environment: ""}, "", false}, |
| 25 | {"environment echoed back", apiStoreReply{Environment: "beta"}, "beta", false}, |
| 26 | {"old console omits it", apiStoreReply{Environment: ""}, "beta", true}, |
| 27 | {"answered about another environment", apiStoreReply{Environment: "alpha"}, "beta", true}, |
| 28 | // An old console answering an app-store request is the ordinary case |
| 29 | // and must stay silent: this guard exists for the environment ask. |
| 30 | {"old console, app store asked", apiStoreReply{}, "", false}, |
| 31 | } |
| 32 | for _, tc := range cases { |
| 33 | t.Run(tc.name, func(t *testing.T) { |
| 34 | err := storeEnvMismatch(tc.reply, tc.asked) |
| 35 | if tc.wantErr != (err != nil) { |
| 36 | t.Fatalf("storeEnvMismatch(%+v, %q) = %v, wantErr %v", tc.reply, tc.asked, err, tc.wantErr) |
| 37 | } |
| 38 | if err == nil { |
| 39 | return |
| 40 | } |
| 41 | // The sentence has to name the environment asked for and the fix, |
| 42 | // because the caller cannot inspect what happened without the |
| 43 | // very verb that just answered about the wrong store. |
| 44 | for _, want := range []string{tc.asked, "older than per-environment", "Upgrade the console"} { |
| 45 | if !strings.Contains(err.Error(), want) { |
| 46 | t.Errorf("refusal %q does not contain %q", err, want) |
| 47 | } |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | |