| 1 | package platform |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | type seen struct { |
| 14 | method, path, auth, message string |
| 15 | body map[string]any |
| 16 | } |
| 17 | |
| 18 | func server(t *testing.T, status int, body string) (*httptest.Server, *seen) { |
| 19 | t.Helper() |
| 20 | s := &seen{} |
| 21 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | s.method, s.path = r.Method, r.URL.Path |
| 23 | s.auth, s.message = r.Header.Get("Authorization"), r.Header.Get("X-Carlos-Message") |
| 24 | if b, _ := io.ReadAll(r.Body); len(b) > 0 { |
| 25 | if err := json.Unmarshal(b, &s.body); err != nil { |
| 26 | t.Errorf("body not JSON: %v", err) |
| 27 | } |
| 28 | } |
| 29 | w.WriteHeader(status) |
| 30 | io.WriteString(w, body) |
| 31 | })) |
| 32 | t.Cleanup(ts.Close) |
| 33 | return ts, s |
| 34 | } |
| 35 | |
| 36 | func TestSetEnvShape(t *testing.T) { |
| 37 | ts, s := server(t, 200, `{}`) |
| 38 | c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| 39 | if err := c.SetEnv(context.Background(), "scratchpad", "base-scratchpad", map[string]string{"K": "V"}); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if s.method != "PUT" || s.path != "/api/cli/apps/acct1/scratchpad/config" { |
| 43 | t.Fatalf("got %s %s", s.method, s.path) |
| 44 | } |
| 45 | if s.auth != "Bearer op-tok" { |
| 46 | t.Fatalf("auth %q", s.auth) |
| 47 | } |
| 48 | got, _ := json.Marshal(s.body) |
| 49 | want := `{"environment":"base-scratchpad","message":"Base: install","set":{"K":{"kind":"env","value":"V"}}}` |
| 50 | if string(got) != want { |
| 51 | t.Fatalf("body %s", got) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestCreateInstanceShape(t *testing.T) { |
| 56 | for _, status := range []int{200, 201} { |
| 57 | ts, s := server(t, status, `{}`) |
| 58 | c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| 59 | if err := c.CreateInstance(context.Background(), "scratchpad", "b-scratchpad.example.com", "b-scratchpad"); err != nil { |
| 60 | t.Fatalf("status %d: %v", status, err) |
| 61 | } |
| 62 | if s.method != "POST" || s.path != "/api/cli/apps/acct1/scratchpad/instances" { |
| 63 | t.Fatalf("got %s %s", s.method, s.path) |
| 64 | } |
| 65 | if s.auth != "Bearer inst-tok" { |
| 66 | t.Fatalf("auth %q", s.auth) |
| 67 | } |
| 68 | got, _ := json.Marshal(s.body) |
| 69 | want := `{"channel":"edge","environment":"b-scratchpad","host":"b-scratchpad.example.com","message":"Base: install"}` |
| 70 | if string(got) != want { |
| 71 | t.Fatalf("body %s", got) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestDeleteInstanceShape(t *testing.T) { |
| 77 | ts, s := server(t, 200, `{"result":"deleted"}`) |
| 78 | c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| 79 | if err := c.DeleteInstance(context.Background(), "scratchpad", "b-scratchpad.example.com"); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | if s.method != "DELETE" || s.path != "/api/cli/apps/acct1/scratchpad/instances/b-scratchpad.example.com" { |
| 83 | t.Fatalf("got %s %s", s.method, s.path) |
| 84 | } |
| 85 | if s.auth != "Bearer inst-tok" || s.message != "Base: uninstall" { |
| 86 | t.Fatalf("auth %q message %q", s.auth, s.message) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestErrorsCarryStatusNotBody(t *testing.T) { |
| 91 | ts, _ := server(t, 403, "SECRET-BODY") |
| 92 | c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| 93 | ctx := context.Background() |
| 94 | errs := map[string]error{ |
| 95 | "CreateInstance": c.CreateInstance(ctx, "a", "h", "e"), |
| 96 | "DeleteInstance": c.DeleteInstance(ctx, "a", "h"), |
| 97 | "SetEnv": c.SetEnv(ctx, "a", "e", map[string]string{"K": "V"}), |
| 98 | } |
| 99 | for name, err := range errs { |
| 100 | if err == nil { |
| 101 | t.Fatalf("%s: want error", name) |
| 102 | } |
| 103 | msg := err.Error() |
| 104 | if !strings.Contains(msg, name) || !strings.Contains(msg, "403") || strings.Contains(msg, "SECRET") { |
| 105 | t.Errorf("%s: error %q", name, msg) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestFakeRecordsAndFails(t *testing.T) { |
| 111 | f := NewFake() |
| 112 | ctx := context.Background() |
| 113 | f.CreateInstance(ctx, "a", "h", "e") |
| 114 | f.Fail("DeleteInstance", io.ErrUnexpectedEOF) |
| 115 | if err := f.DeleteInstance(ctx, "a", "h"); err != io.ErrUnexpectedEOF { |
| 116 | t.Fatalf("got %v", err) |
| 117 | } |
| 118 | f.SetEnv(ctx, "a", "e", map[string]string{"K": "V"}) |
| 119 | calls := f.Calls() |
| 120 | if len(calls) != 3 || calls[0].Method != "CreateInstance" || calls[1].Method != "DeleteInstance" || calls[2].Method != "SetEnv" { |
| 121 | t.Fatalf("calls %+v", calls) |
| 122 | } |
| 123 | if calls[2].Vars["K"] != "V" || calls[0].Host != "h" || calls[0].Environment != "e" { |
| 124 | t.Fatalf("args %+v", calls) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // conflictServer answers the create with 409 and the record read with |
| 129 | // status and body, and remembers the read's path and credential. |
| 130 | func conflictServer(t *testing.T, status int, body string) (*httptest.Server, *seen) { |
| 131 | t.Helper() |
| 132 | read := &seen{} |
| 133 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 134 | if r.Method == http.MethodPost { |
| 135 | w.WriteHeader(http.StatusConflict) |
| 136 | io.WriteString(w, `{"error":"instance already exists"}`) |
| 137 | return |
| 138 | } |
| 139 | read.method, read.path, read.auth = r.Method, r.URL.Path, r.Header.Get("Authorization") |
| 140 | w.WriteHeader(status) |
| 141 | io.WriteString(w, body) |
| 142 | })) |
| 143 | t.Cleanup(ts.Close) |
| 144 | return ts, read |
| 145 | } |
| 146 | |
| 147 | // Finding 3 (R27): a 409 is success only when the existing record is this |
| 148 | // app's instance at this host, bound to this environment. |
| 149 | func TestCreateInstanceConflictMatchingRecord(t *testing.T) { |
| 150 | ts, read := conflictServer(t, 200, `{"schema":1,"host":"b-scratchpad.example.com","app":"scratchpad","account":"acct1","environment":"b-scratchpad","gated":false}`) |
| 151 | c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| 152 | if err := c.CreateInstance(context.Background(), "scratchpad", "b-scratchpad.example.com", "b-scratchpad"); err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | if read.method != "GET" || read.path != "/api/cli/apps/acct1/scratchpad/instances/b-scratchpad.example.com" || read.auth != "Bearer inst-tok" { |
| 156 | t.Fatalf("record read %+v", read) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestCreateInstanceConflictOtherRecord(t *testing.T) { |
| 161 | for name, tc := range map[string]struct { |
| 162 | status int |
| 163 | body string |
| 164 | }{ |
| 165 | "no record for this app": {404, `404 page not found`}, |
| 166 | "another app's record": {200, `{"host":"b-scratchpad.example.com","app":"welcome","environment":"b-scratchpad"}`}, |
| 167 | "another environment": {200, `{"host":"b-scratchpad.example.com","app":"scratchpad","environment":"other"}`}, |
| 168 | "no environment": {200, `{"host":"b-scratchpad.example.com","app":"scratchpad"}`}, |
| 169 | "another host": {200, `{"host":"x.example.com","app":"scratchpad","environment":"b-scratchpad"}`}, |
| 170 | "unreadable record": {200, `not json`}, |
| 171 | "record read fails": {502, `{"error":"SECRET"}`}, |
| 172 | } { |
| 173 | t.Run(name, func(t *testing.T) { |
| 174 | ts, _ := conflictServer(t, tc.status, tc.body) |
| 175 | c := NewHTTP(ts.URL, "acct1", "inst-tok", "op-tok", ts.Client()) |
| 176 | err := c.CreateInstance(context.Background(), "scratchpad", "b-scratchpad.example.com", "b-scratchpad") |
| 177 | if err == nil { |
| 178 | t.Fatal("a conflict with another record counted as success") |
| 179 | } |
| 180 | if !strings.Contains(err.Error(), "CreateInstance") || strings.Contains(err.Error(), "SECRET") { |
| 181 | t.Errorf("error %q", err) |
| 182 | } |
| 183 | }) |
| 184 | } |
| 185 | } |
| 186 | |