| index ff3493e75..0ce1edf11 100644 |
| --- a/internal/console/newapp_ha_test.go |
| +++ b/internal/console/newapp_ha_test.go |
| @@ -2,9 +2,13 @@ package console |
| |
| import ( |
| "context" |
| + "errors" |
| "fmt" |
| + "html" |
| "net/http" |
| "net/url" |
| + "os" |
| + "regexp" |
| "slices" |
| "strings" |
| "testing" |
| @@ -34,6 +38,23 @@ func haPickerAbsentWorld(t *testing.T, accounts []string) *haRegionWorld { |
| // usHAOption is the picker's us option with its HA mark. |
| const usHAOption = `<option value="us" data-ha>` |
| |
| +// hiddenInput matches a rendered hidden input's name and value. |
| +var hiddenInput = regexp.MustCompile(`<input type="hidden" name="([^"]+)" value="([^"]*)">`) |
| + |
| +// renderedNewAppFields is what the create form as rendered for w posts |
| +// with nothing ticked or typed: its hidden inputs (the CSRF value is |
| +// replaced by postNewApp). Tests that must post what the page posts use |
| +// it rather than a hand-written field set. |
| +func renderedNewAppFields(t *testing.T, w *haRegionWorld, c *http.Client) url.Values { |
| + t.Helper() |
| + _, _, body := get(t, c, w.u("/apps/new")) |
| + form := url.Values{} |
| + for _, m := range hiddenInput.FindAllStringSubmatch(newAppForm(t, body), -1) { |
| + form.Add(html.UnescapeString(m[1]), html.UnescapeString(m[2])) |
| + } |
| + return form |
| +} |
| + |
| // postNewApp posts the browser create form, refused or not, and reports |
| // the status and the redirect. |
| func postNewApp(t *testing.T, w *haRegionWorld, c *http.Client, csrf string, form url.Values) (int, string) { |
| @@ -97,8 +118,8 @@ func TestNewAppHACheckboxOnlyForListedAccounts(t *testing.T) { |
| t.Errorf("listed: a non-HA option is marked: %s", o) |
| } |
| } |
| - if strings.Contains(form, `type="hidden" name="region"`) { |
| - t.Errorf("listed with a picker: a hidden region input in\n%s", form) |
| + if strings.Contains(form, `name="ha_region"`) { |
| + t.Errorf("listed with a picker: a hidden HA region input in\n%s", form) |
| } |
| }) |
| |
| @@ -143,8 +164,13 @@ func TestNewAppHACheckboxOnlyForListedAccounts(t *testing.T) { |
| if !strings.Contains(form, `name="ha"`) || !has(form, haCopyLabel) { |
| t.Errorf("picker absent: no HA checkbox in\n%s", form) |
| } |
| - if !strings.Contains(form, `<input type="hidden" name="region" value="ie">`) { |
| - t.Errorf("picker absent: no hidden region ie in\n%s", form) |
| + if !strings.Contains(form, `<input type="hidden" name="ha_region" value="ie">`) { |
| + t.Errorf("picker absent: no hidden HA region ie in\n%s", form) |
| + } |
| + // The fallback is HA's own field: the form posts no region, as |
| + // before Task 6 (TestNewAppHAUncheckedKeepsRegionSource). |
| + if strings.Contains(form, `name="region"`) { |
| + t.Errorf("picker absent: the form posts a region in\n%s", form) |
| } |
| }) |
| |
| @@ -156,7 +182,7 @@ func TestNewAppHACheckboxOnlyForListedAccounts(t *testing.T) { |
| if strings.Contains(form, `id="app-region"`) { |
| t.Fatalf("control: the picker rendered in\n%s", form) |
| } |
| - if strings.Contains(form, `name="ha"`) || strings.Contains(form, `name="region"`) { |
| + if strings.Contains(form, `name="ha"`) || strings.Contains(form, `name="region"`) || strings.Contains(form, `name="ha_region"`) { |
| t.Errorf("unlisted: the form offers HA:\n%s", form) |
| } |
| }) |
| @@ -233,9 +259,20 @@ func TestNewAppHACreatesAndEnables(t *testing.T) { |
| t.Run("picker absent, the home", func(t *testing.T) { |
| w := haPickerAbsentWorld(t, []string{"bab"}) |
| c, csrf := haBrowser(t, w) |
| - status, loc := postNewApp(t, w, c, csrf, url.Values{"name": {"shop"}, "region": {"ie"}, "ha": {"1"}}) |
| + // What the page posts with the box ticked: its hidden fields (the |
| + // HA region among them), the name and ha. |
| + form := renderedNewAppFields(t, w, c) |
| + if form.Get("ha_region") != "ie" || form.Has("region") { |
| + t.Fatalf("fixture: the rendered form posts %v, want ha_region ie and no region", form) |
| + } |
| + form.Set("name", "shop") |
| + form.Set("ha", "1") |
| + status, loc := postNewApp(t, w, c, csrf, form) |
| wantCreated(t, w, "ha at home", "shop", status, loc) |
| wantHAApp(t, w, "shop", "") |
| + if r, src, _ := w.st.AppRegion(OpsAccountID, "shop"); r != "" || src != "chosen" { |
| + t.Errorf("ha at home: AppRegion = %q %q, want the home, chosen", r, src) |
| + } |
| }) |
| |
| t.Run("the enable refuses after the claim", func(t *testing.T) { |
| @@ -254,9 +291,54 @@ func TestNewAppHACreatesAndEnables(t *testing.T) { |
| if _, _, body := get(t, c, w.u("/apps/shop")); !has(body, sentenceCase(ref.Msg)) || has(body, "Created app shop") { |
| t.Errorf("the app page does not carry the enable's refusal alone") |
| } |
| + // The refused enable wrote no manifest. |
| + if _, err := provision.GetManifest(context.Background(), w.rel, w.sqid, "shop"); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("refused enable: manifest read = %v, want it absent", err) |
| + } |
| + // Control: with the fault removed, the same enable succeeds and |
| + // binds the row's pool and region. |
| + w.srv.cfg.AppsDomain = "apps.example.test" |
| + acct, err := w.st.AccountByID(OpsAccountID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if ref := w.srv.enableHAManifest(context.Background(), acct, "shop", "203.0.113.9"); ref != nil { |
| + t.Fatalf("control: enable with the apps domain set = %+v", ref) |
| + } |
| + wantHAApp(t, w, "shop", "us") |
| }) |
| } |
| |
| +// TestNewAppHAUncheckedKeepsRegionSource: with no picker and an HA home, a |
| +// listed account's form carries the HA fallback region, and submitting it |
| +// with the box unticked stores the region and source a plain create stored |
| +// before Task 6 (the form then posted no region), never "chosen". |
| +func TestNewAppHAUncheckedKeepsRegionSource(t *testing.T) { |
| + w := haPickerAbsentWorld(t, []string{"bab"}) |
| + c, csrf := haBrowser(t, w) |
| + // Control: the pre-Task-6 form's post (no region field). |
| + status, loc := postNewApp(t, w, c, csrf, url.Values{"name": {"before"}}) |
| + wantCreated(t, w, "pre-Task-6 form", "before", status, loc) |
| + wantRegion, wantSource, err := w.st.AppRegion(OpsAccountID, "before") |
| + if err != nil || wantSource == "chosen" { |
| + t.Fatalf("control: AppRegion = %q %q %v, want a source the person did not choose", wantRegion, wantSource, err) |
| + } |
| + // What the page posts now with the box unticked. |
| + form := renderedNewAppFields(t, w, c) |
| + if !form.Has("ha_region") { |
| + t.Fatalf("fixture: the rendered form carries no HA fallback: %v", form) |
| + } |
| + form.Set("name", "after") |
| + status, loc = postNewApp(t, w, c, csrf, form) |
| + wantCreated(t, w, "unticked HA form", "after", status, loc) |
| + if r, src, err := w.st.AppRegion(OpsAccountID, "after"); err != nil || r != wantRegion || src != wantSource { |
| + t.Errorf("unticked HA form: AppRegion = %q %q %v, want %q %q as before Task 6", r, src, err, wantRegion, wantSource) |
| + } |
| + if pool, _, _ := w.st.AppHAPool(context.Background(), OpsAccountID, "after"); pool != "" { |
| + t.Errorf("unticked HA form stored pool %q", pool) |
| + } |
| +} |
| + |
| func TestAppSettingsHAStatusLine(t *testing.T) { |
| w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}}) |
| c, csrf := haBrowser(t, w) |