| index f0e93b1f1..41de921f6 100644 |
| --- a/internal/console/billingpage_test.go |
| +++ b/internal/console/billingpage_test.go |
| @@ -2,6 +2,7 @@ package console |
| |
| import ( |
| "fmt" |
| + "html" |
| "net/http" |
| "net/http/httptest" |
| "net/url" |
| @@ -112,7 +113,7 @@ func TestDecideBillingPage(t *testing.T) { |
| "team.members", "renews", "button.confirm", "button.continue", "button.start_over", "button.match_seats", |
| "button.portal", "upgrade.not_owner_title", "upgrade.not_owner", "pick.title", |
| // Task 8's own labels and messages (no wording outside the map). |
| - "page.eyebrow", "page.read_failed", "upgrade.read_failed", "error.title", "currency.show", "upgrade.new_account"} { |
| + "page.eyebrow", "page.read_failed", "upgrade.read_failed", "error.title", "upgrade.new_account"} { |
| if billingCopy[k] == "" { |
| t.Errorf("billingCopy has no %q", k) |
| } |
| @@ -673,109 +674,209 @@ func htmlForm(body, open string) string { |
| } |
| |
| var ( |
| - currencyField = regexp.MustCompile(`<input type="([a-z]+)" name="currency" value="([^"]*)"`) |
| - hiddenField = regexp.MustCompile(`<input type="hidden" name="([^"]+)" value="([^"]*)"`) |
| + radioField = regexp.MustCompile(`<input type="radio" name="currency" value="([^"]*)"([^>]*)>`) |
| + hiddenCcy = regexp.MustCompile(`<input type="hidden" name="currency" value="([^"]*)">`) |
| + planBlock = regexp.MustCompile(`(?s)<p data-plan="([^"]+)">(.*?)</p>`) |
| + packBlock = regexp.MustCompile(`(?s)<tr data-pack="([^"]+)">(.*?)</tr>`) |
| + priceSpan = regexp.MustCompile(`<span class="small price price-([a-z0-9]+)( total)?" data-currency="([a-z0-9]+)">([^<]*)</span>`) |
| + anyPriceTag = regexp.MustCompile(`class="[^"]*\bprice\b`) |
| ) |
| |
| -// TestBillingPagePricesFollowShownCurrency: the purchase form submits the |
| -// currency its prices are rendered in, for an absent, sold or refused |
| -// query; the currency radios are their own GET form back to the page, so |
| -// choosing one reloads the prices, keeping the chosen plan. |
| -func TestBillingPagePricesFollowShownCurrency(t *testing.T) { |
| - pf := newBillingPageFixture(t) |
| - book := pf.srv.billing.cfg.Book |
| - buyOpen := `<form method="post" action="/a/` + pf.acct.Sqid + `/billing/checkout">` |
| - radiosOpen := `<form method="get" action="/a/` + pf.acct.Sqid + `/billing">` |
| +// buyFormPrices is the purchase form the billing page renders, parsed: the |
| +// currencies it can submit (each radio, or the fixed hidden field), which |
| +// radios are checked and required, and every price element by plan or |
| +// pack, currency and kind ("price" or "total"). |
| +type buyFormPrices struct { |
| + submits, checked []string |
| + allRequired bool |
| + prices map[string]map[string]map[string]string // plan or "pack:<name>" -> currency -> kind -> text |
| +} |
| |
| - // check is the page at query: its purchase form posts priced, once, as |
| - // a hidden field, and shows personal's price in priced. |
| - check := func(t *testing.T, query, priced string) string { |
| - t.Helper() |
| - _, body := pf.page(t, query) |
| - buy := htmlForm(body, buyOpen) |
| - if buy == "" { |
| - t.Fatalf("?%s: no purchase form:\n%s", query, body) |
| - } |
| - fields := currencyField.FindAllStringSubmatch(buy, -1) |
| - if len(fields) != 1 || fields[0][1] != "hidden" || fields[0][2] != priced { |
| - t.Fatalf("?%s: the purchase form's currency fields = %q, want one hidden %q", query, fields, priced) |
| - } |
| - price, ok := book.PlanPrice("personal", priced) |
| - if !ok { |
| - t.Fatalf("no personal price in %s", priced) |
| +func parseBuyForm(t *testing.T, form string) buyFormPrices { |
| + t.Helper() |
| + got := buyFormPrices{allRequired: true, prices: map[string]map[string]map[string]string{}} |
| + for _, m := range radioField.FindAllStringSubmatch(form, -1) { |
| + got.submits = append(got.submits, m[1]) |
| + if strings.Contains(m[2], " checked") { |
| + got.checked = append(got.checked, m[1]) |
| } |
| - if want := esc(fmt.Sprintf(billingCopy["plan.price"], plans.FormatMinor(priced, price))); !strings.Contains(buy, want) { |
| - t.Errorf("?%s: the purchase form is not priced in %s (%q):\n%s", query, priced, want, buy) |
| + if !strings.Contains(m[2], " required") { |
| + got.allRequired = false |
| } |
| - for _, other := range book.Currencies { |
| - if other == priced { |
| - continue |
| + } |
| + for _, m := range hiddenCcy.FindAllStringSubmatch(form, -1) { |
| + got.submits = append(got.submits, m[1]) |
| + } |
| + spans := 0 |
| + add := func(owner, body string) { |
| + for _, m := range priceSpan.FindAllStringSubmatch(body, -1) { |
| + spans++ |
| + if m[1] != m[3] { |
| + t.Errorf("%s: a price tagged price-%s carries data-currency %s", owner, m[1], m[3]) |
| + } |
| + kind := "price" |
| + if m[2] != "" { |
| + kind = "total" |
| + } |
| + if got.prices[owner] == nil { |
| + got.prices[owner] = map[string]map[string]string{} |
| } |
| - if p, ok := book.PlanPrice("personal", other); ok && strings.Contains(buy, esc(plans.FormatMinor(other, p))+" ") { |
| - t.Errorf("?%s: a %s price shows beside a %s purchase", query, other, priced) |
| + if got.prices[owner][m[1]] == nil { |
| + got.prices[owner][m[1]] = map[string]string{} |
| } |
| + if _, dup := got.prices[owner][m[1]][kind]; dup { |
| + t.Errorf("%s: two %s %s elements", owner, m[1], kind) |
| + } |
| + got.prices[owner][m[1]][kind] = html.UnescapeString(m[4]) |
| } |
| - return body |
| } |
| - for _, c := range []struct{ query, priced string }{ |
| - {"", book.DefaultCurrency}, // no country: the book's default |
| - {"currency=gbp", "gbp"}, |
| - {"currency=eur", "eur"}, |
| - {"currency=jpy", book.DefaultCurrency}, // refused: nothing preselected, priced as shown |
| - {"currency=" + url.QueryEscape("<script>"), book.DefaultCurrency}, |
| - {"currency=USD", book.DefaultCurrency}, |
| - } { |
| - t.Run(c.query, func(t *testing.T) { check(t, c.query, c.priced) }) |
| + for _, m := range planBlock.FindAllStringSubmatch(form, -1) { |
| + add(m[1], m[2]) |
| + } |
| + for _, m := range packBlock.FindAllStringSubmatch(form, -1) { |
| + add("pack:"+m[1], m[2]) |
| } |
| + // No price outside a plan or pack, and none untagged. |
| + if n := len(anyPriceTag.FindAllString(form, -1)); n != spans { |
| + t.Errorf("the purchase form has %d price elements, %d of them tagged by plan or pack and currency", n, spans) |
| + } |
| + return got |
| +} |
| |
| - // The radios: a GET form to the page itself, a radio per sold |
| - // currency, its own button, and none of them inside the purchase form. |
| - body := check(t, "plan=community", book.DefaultCurrency) |
| - radios := htmlForm(body, radiosOpen) |
| - if radios == "" { |
| - t.Fatalf("no currency GET form %q:\n%s", radiosOpen, body) |
| +// assertBuyFormPriced is the guard: every currency the purchase form can |
| +// submit has a price for every offered plan (and a total on a team plan) |
| +// and every offered pack, each the book's; no price is in a currency the |
| +// form can't submit; and with radios, a CSS rule per sold currency hides |
| +// every price but the checked currency's. |
| +func assertBuyFormPriced(t *testing.T, pf *billingPageFixture, body string, members int) buyFormPrices { |
| + t.Helper() |
| + cat, book := pf.srv.cfg.PlanCatalog, pf.srv.billing.cfg.Book |
| + form := htmlForm(body, `<form class="buy" method="post" action="/a/`+pf.acct.Sqid+`/billing/checkout">`) |
| + if form == "" { |
| + t.Fatalf("no purchase form:\n%s", body) |
| + } |
| + got := parseBuyForm(t, form) |
| + if len(got.submits) == 0 { |
| + t.Fatalf("the purchase form submits no currency:\n%s", form) |
| + } |
| + var paid, packs []string |
| + for name, p := range cat.Plans { |
| + if !p.Paid { |
| + continue |
| + } |
| + paid = append(paid, name) |
| + for _, pk := range p.Packs { |
| + if !slices.Contains(packs, pk) { |
| + packs = append(packs, pk) |
| + } |
| + } |
| } |
| - for _, code := range book.Currencies { |
| - if !strings.Contains(radios, `<input type="radio" name="currency" value="`+code+`"`) { |
| - t.Errorf("the currency form has no %s radio", code) |
| + for _, c := range got.submits { |
| + for _, name := range paid { |
| + p := cat.Plans[name] |
| + price, _ := book.PlanPrice(name, c) |
| + want := map[string]string{"price": fmt.Sprintf(billingCopy["plan.price"], plans.FormatMinor(c, price))} |
| + if p.Team != nil { |
| + want = map[string]string{ |
| + "price": fmt.Sprintf(billingCopy["team.price"], plans.FormatMinor(c, price), p.Team.MinMembers), |
| + "total": fmt.Sprintf(billingCopy["team.total"], plans.FormatMinor(c, price*int64(p.Team.Billed(members)))), |
| + } |
| + } |
| + for kind, w := range want { |
| + if g := got.prices[name][c][kind]; g != w { |
| + t.Errorf("plan %s, %s %s = %q, want %q", name, c, kind, g, w) |
| + } |
| + } |
| + } |
| + for _, pk := range packs { |
| + price, _ := book.PackPrice(pk, c) |
| + if g, w := got.prices["pack:"+pk][c]["price"], fmt.Sprintf(billingCopy["pack.price"], plans.FormatMinor(c, price)); g != w { |
| + t.Errorf("pack %s, %s price = %q, want %q", pk, c, g, w) |
| + } |
| } |
| } |
| - if !strings.Contains(radios, esc(billingCopy["currency.show"])) { |
| - t.Error("the currency form has no button") |
| + for owner, byCcy := range got.prices { |
| + for c := range byCcy { |
| + if !slices.Contains(got.submits, c) { |
| + t.Errorf("%s shows a %s price, which the form can't submit (%v)", owner, c, got.submits) |
| + } |
| + } |
| } |
| - if strings.Contains(htmlForm(body, buyOpen), `type="radio" name="currency"`) { |
| - t.Error("a currency radio sits inside the purchase form") |
| + rule := func(c string) string { |
| + return `.buy:has(input[name=currency][value=` + c + `]:checked) .price:not(.price-` + c + `){display:none}` |
| } |
| - |
| - // Choosing gbp: submit the radio form as a browser would, its hidden |
| - // fields plus the chosen radio. The plan stays chosen and the prices |
| - // and the posted currency are gbp. |
| - q := url.Values{} |
| - for _, m := range hiddenField.FindAllStringSubmatch(radios, -1) { |
| - q.Add(m[1], m[2]) |
| + // With radios, one rule per sold currency and no other price rule, so |
| + // nothing is hidden while no radio is checked; a fixed currency has |
| + // none. |
| + fixed := len(hiddenCcy.FindAllString(form, -1)) > 0 |
| + for _, c := range book.Currencies { |
| + if has := strings.Contains(body, rule(c)); has == fixed { |
| + t.Errorf("fixed %v: the CSS rule for %s present = %v", fixed, c, has) |
| + } |
| } |
| - if q.Get("plan") != "community" { |
| - t.Fatalf("the currency form drops the chosen plan: %v", q) |
| + if n, want := strings.Count(body, " .price"), len(book.Currencies); !fixed && n != want || fixed && n != 0 { |
| + t.Errorf("%d CSS rules name .price, want one per sold currency (fixed %v)", n, fixed) |
| } |
| - q.Set("currency", "gbp") |
| - body = check(t, q.Encode(), "gbp") |
| - if !strings.Contains(body, `name="plan" value="community" checked`) || !strings.Contains(body, `name="currency" value="gbp" checked`) { |
| - t.Errorf("after choosing gbp, community or gbp is not chosen:\n%s", body) |
| + return got |
| +} |
| + |
| +// TestBillingPagePricesFollowShownCurrency: the purchase form can only |
| +// submit a currency whose prices are on the page, and shows the checked |
| +// one's. Absent query: the default preselected; sold code: it; refused: |
| +// nothing checked and the radios required, so the owner must choose a |
| +// sold one; fixed: text, a hidden field and its prices alone. |
| +func TestBillingPagePricesFollowShownCurrency(t *testing.T) { |
| + pf := newBillingPageFixture(t) |
| + book := pf.srv.billing.cfg.Book |
| + for _, c := range []struct { |
| + query string |
| + checked []string |
| + }{ |
| + {"", []string{book.DefaultCurrency}}, // no country: the book's default |
| + {"currency=gbp", []string{"gbp"}}, |
| + {"currency=eur", []string{"eur"}}, |
| + {"currency=jpy", nil}, |
| + {"currency=" + url.QueryEscape("<script>"), nil}, |
| + {"currency=USD", nil}, |
| + {"plan=community¤cy=gbp", []string{"gbp"}}, |
| + } { |
| + t.Run(c.query, func(t *testing.T) { |
| + _, body := pf.page(t, c.query) |
| + got := assertBuyFormPriced(t, pf, body, 1) |
| + if !slices.Equal(got.submits, book.Currencies) { |
| + t.Errorf("radios = %v, want the book's %v", got.submits, book.Currencies) |
| + } |
| + if !slices.Equal(got.checked, c.checked) { |
| + t.Errorf("checked = %v, want %v", got.checked, c.checked) |
| + } |
| + if !got.allRequired { |
| + t.Error("the currency radios are not required") |
| + } |
| + if strings.Contains(body, `<form method="get" action="/a/`+pf.acct.Sqid+`/billing">`) { |
| + t.Error("a separate currency form is back") |
| + } |
| + }) |
| } |
| - // A plan the catalog doesn't sell is not carried. |
| - _, body = pf.page(t, "plan="+url.QueryEscape("../x")) |
| - if strings.Contains(htmlForm(body, radiosOpen), `name="plan"`) { |
| - t.Error("the currency form carried an unknown plan") |
| + |
| + // Team totals follow the member count in every currency. |
| + for i := 0; i < 11; i++ { |
| + if err := pf.st.AddMember(pf.acct.ID, fmt.Sprintf("t%d@example.com", i), RoleMember, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| } |
| + _, body := pf.page(t, "") |
| + assertBuyFormPriced(t, pf, body, 12) |
| |
| - // A fixed currency: text and the hidden field, no currency form. |
| + // A fixed currency: text and the hidden field, its prices alone, no |
| + // radios and no rules. |
| if err := pf.st.PutBillingState(billingAccount{AccountID: pf.acct.ID, Subscription: "sub_old", State: billing.StateEnded, |
| Currency: "eur", PaidPacks: map[string]int{}}); err != nil { |
| t.Fatal(err) |
| } |
| - body = check(t, "currency=gbp", "eur") |
| - if htmlForm(body, radiosOpen) != "" { |
| - t.Error("a fixed currency still offers the currency form") |
| + _, body = pf.page(t, "currency=gbp") |
| + got := assertBuyFormPriced(t, pf, body, 12) |
| + if !slices.Equal(got.submits, []string{"eur"}) || len(got.checked) != 0 { |
| + t.Errorf("fixed eur: submits %v, checked %v", got.submits, got.checked) |
| } |
| } |
| |