| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // /new-temporary takes /new's optional region fields, resolved the same way |
| 9 | // and stored in the same insert as the expiry. |
| 10 | func newTemporaryURL(fx *consoleFixture) string { |
| 11 | return fx.ts.URL + "/api/cli/apps/" + fx.sqid + "/new-temporary" |
| 12 | } |
| 13 | |
| 14 | func TestTemporaryCreateResolvesRegion(t *testing.T) { |
| 15 | fx, token := regionFixture(t) |
| 16 | fx.srv.cfg.TemporaryApps = true |
| 17 | status, res := postJSON(t, newTemporaryURL(fx), map[string]any{"name": "tmp-tz", "timezone": "Europe/London"}, token) |
| 18 | if status != http.StatusOK || res["region"] != "uk" || res["region_source"] != "timezone" || |
| 19 | res["region_name"] != ukPlace || res["regions_available"] != float64(2) { |
| 20 | t.Fatalf("timezone create = %d %v", status, res) |
| 21 | } |
| 22 | if r, src, _ := fx.st.AppRegion(OpsAccountID, "tmp-tz"); r != "uk" || src != "timezone" { |
| 23 | t.Fatalf("stored %q %q", r, src) |
| 24 | } |
| 25 | if l := lifeOf(t, fx, "tmp-tz"); l == nil || !l.Temporary() { |
| 26 | t.Fatalf("row = %+v, want a temporary row", l) |
| 27 | } |
| 28 | status, res = postJSON(t, newTemporaryURL(fx), map[string]any{"name": "tmp-chosen", "region": "uk"}, token) |
| 29 | if status != http.StatusOK || res["region"] != "uk" || res["region_source"] != "chosen" { |
| 30 | t.Fatalf("--region create = %d %v", status, res) |
| 31 | } |
| 32 | if r, src, _ := fx.st.AppRegion(OpsAccountID, "tmp-chosen"); r != "uk" || src != "chosen" { |
| 33 | t.Fatalf("stored %q %q", r, src) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestTemporaryCreateWithoutRegionGetsTheDefault(t *testing.T) { |
| 38 | fx, token := regionFixture(t) |
| 39 | fx.srv.cfg.TemporaryApps = true |
| 40 | status, res := postJSON(t, newTemporaryURL(fx), map[string]any{"name": "tmp-plain"}, token) |
| 41 | if status != http.StatusOK || res["region"] != "ie" || res["region_source"] != "default" { |
| 42 | t.Fatalf("create = %d %v", status, res) |
| 43 | } |
| 44 | if r, src, _ := fx.st.AppRegion(OpsAccountID, "tmp-plain"); r != "" || src != "default" { |
| 45 | t.Fatalf("stored %q %q", r, src) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestTemporaryCreateRefusesABadRegionAndUnknownFields(t *testing.T) { |
| 50 | fx, token := regionFixture(t) |
| 51 | fx.srv.cfg.TemporaryApps = true |
| 52 | status, res := postJSON(t, newTemporaryURL(fx), map[string]any{"name": "tmp-bad", "region": "Bad/Code"}, token) |
| 53 | if status != http.StatusBadRequest || errOf(res) != msgRegionUnknown("Bad/Code", []string{"ie", "uk"}) { |
| 54 | t.Fatalf("bad region = %d %v, want /new's refusal", status, res) |
| 55 | } |
| 56 | for _, extra := range []string{"place", "control_plane", "build_probe", "regions"} { |
| 57 | name := "tmp-x" + extra[:2] |
| 58 | status, _ := postJSON(t, newTemporaryURL(fx), map[string]any{"name": name, extra: "fleet/pi"}, token) |
| 59 | if status != http.StatusBadRequest { |
| 60 | t.Errorf("%s = %d, want 400", extra, status) |
| 61 | } |
| 62 | } |
| 63 | for _, name := range []string{"tmp-bad", "tmp-xpl", "tmp-xco", "tmp-xbu", "tmp-xre"} { |
| 64 | if has, _ := fx.st.HasAppIn(OpsAccountID, name); has { |
| 65 | t.Errorf("a refused create still claimed %s", name) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |