| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/appconfig" |
| 12 | ) |
| 13 | |
| 14 | func TestAppSettingsShowsTheSwapCard(t *testing.T) { |
| 15 | fx := newConsoleFixture(t) |
| 16 | if _, err := appconfig.SetSwapPolicy(context.Background(), fx.rel, fx.sqid, "kass", "hold", 3); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | c := signInPaul(t, fx) |
| 20 | code, _, body := get(t, c, fx.u("/apps/kass/settings")) |
| 21 | if code != http.StatusOK { |
| 22 | t.Fatalf("settings = %d", code) |
| 23 | } |
| 24 | for _, want := range []string{swapCopy.Heading, `action="/a/` + fx.sqid + `/apps/kass/swap"`, |
| 25 | `<option value="hold" selected>`, `name="swap_concurrency"`, `value="3"`} { |
| 26 | if !strings.Contains(body, want) { |
| 27 | t.Errorf("settings page lacks %q", want) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestAppSwapFormSaves(t *testing.T) { |
| 33 | fx := newConsoleFixture(t) |
| 34 | c := signInPaul(t, fx) |
| 35 | csrf := sessionToken(t, fx, c) |
| 36 | code, loc, _ := postForm(t, c, fx.u("/apps/kass/swap"), |
| 37 | url.Values{"csrf": {csrf}, "swap": {"hold"}, "swap_concurrency": {"2"}}) |
| 38 | if code != http.StatusSeeOther || loc != fx.path("/apps/kass/settings") { |
| 39 | t.Fatalf("save = %d -> %q", code, loc) |
| 40 | } |
| 41 | got, err := appconfig.GetSwapPolicy(context.Background(), fx.rel, fx.sqid, "kass") |
| 42 | if err != nil || got != (appconfig.SwapPolicy{Swap: "hold", Concurrency: 2}) { |
| 43 | t.Fatalf("stored = %+v, %v", got, err) |
| 44 | } |
| 45 | if n := serial(t, fx); n != 1 { |
| 46 | t.Fatalf("serial = %d, want 1", n) |
| 47 | } |
| 48 | if _, _, body := get(t, c, fx.u("/apps/kass/settings")); !strings.Contains(body, fmt.Sprintf(swapCopy.Saved, "hold", 2)) { |
| 49 | t.Errorf("no saved notice after the redirect") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestAppSwapFormRefusesBadInput(t *testing.T) { |
| 54 | fx := newConsoleFixture(t) |
| 55 | c := signInPaul(t, fx) |
| 56 | csrf := sessionToken(t, fx, c) |
| 57 | for _, form := range []url.Values{ |
| 58 | {"csrf": {csrf}, "swap": {"fast"}, "swap_concurrency": {"1"}}, |
| 59 | {"csrf": {csrf}, "swap": {"hold"}, "swap_concurrency": {"0"}}, |
| 60 | {"csrf": {csrf}, "swap": {"hold"}, "swap_concurrency": {"two"}}, |
| 61 | {"csrf": {csrf}, "swap": {"hold"}}, |
| 62 | } { |
| 63 | if code, _, _ := postForm(t, c, fx.u("/apps/kass/swap"), form); code != http.StatusSeeOther { |
| 64 | t.Errorf("%v = %d, want a redirect with a notice", form, code) |
| 65 | } |
| 66 | } |
| 67 | if got, _ := appconfig.GetSwapPolicy(context.Background(), fx.rel, fx.sqid, "kass"); got != (appconfig.SwapPolicy{Swap: "overlap", Concurrency: 1}) { |
| 68 | t.Fatalf("a refused form stored %+v", got) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestAppSwapFormNeedsCSRFAndMembership(t *testing.T) { |
| 73 | fx := newConsoleFixture(t) |
| 74 | c := signInPaul(t, fx) |
| 75 | if code, _, _ := postForm(t, c, fx.u("/apps/kass/swap"), url.Values{"csrf": {"wrong"}, "swap": {"hold"}, "swap_concurrency": {"1"}}); code != http.StatusForbidden { |
| 76 | t.Fatalf("bad csrf = %d", code) |
| 77 | } |
| 78 | csrf := sessionToken(t, fx, c) |
| 79 | code, _, body := postForm(t, c, fx.u("/apps/no-such-app/swap"), url.Values{"csrf": {csrf}, "swap": {"hold"}, "swap_concurrency": {"1"}}) |
| 80 | if code != http.StatusNotFound || body != "404 page not found\n" { |
| 81 | t.Fatalf("unknown app = %d %q", code, body) |
| 82 | } |
| 83 | } |
| 84 | |