| new file mode 100644 |
| index 000000000..c8c063a49 |
| --- /dev/null |
| +++ b/internal/console/billingchange_test.go |
| @@ -0,0 +1,916 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "html" |
| + "net/http" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/billing" |
| + "github.com/carlosframework/platform/internal/billing/billingtest" |
| + "github.com/carlosframework/platform/internal/plans" |
| +) |
| + |
| +// ---- fixture ----------------------------------------------------------------- |
| + |
| +// newChangeFixture is an ops fixture (a synced Personal subscription with |
| +// packs) whose owner is signed in with no fresh step-up proof, counters |
| +// reset. |
| +func newChangeFixture(t *testing.T, packs map[string]int64) *opsFixture { |
| + t.Helper() |
| + o := newOpsFixture(t, packs) |
| + o.signInOwner(t) |
| + if err := o.st.ClearStepUp(o.csrf); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.reset() |
| + return o |
| +} |
| + |
| +// stepUp records a fresh sign-in proof for the owner's session. |
| +func (o *opsFixture) stepUp(t *testing.T) { |
| + t.Helper() |
| + if err := o.st.RecordStepUp(o.csrf, "owner"+o.acct.Sqid+"@example.com", time.Now(), time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func (o *opsFixture) change(t *testing.T, kv ...string) billingReply { |
| + t.Helper() |
| + form := url.Values{} |
| + for i := 0; i+1 < len(kv); i += 2 { |
| + form.Set(kv[i], kv[i+1]) |
| + } |
| + return o.request(t, http.MethodPost, o.billingBase()+"/change", form) |
| +} |
| + |
| +func (o *opsFixture) renewal(t *testing.T, v string) billingReply { |
| + t.Helper() |
| + return o.request(t, http.MethodPost, o.billingBase()+"/renewal", url.Values{"renewal": {v}}) |
| +} |
| + |
| +// wantFlash is a redirect back to the billing page with the copy text. |
| +func (o *opsFixture) wantFlash(t *testing.T, r billingReply, kind, text string) { |
| + t.Helper() |
| + if r.Status != http.StatusSeeOther || r.Loc != o.billingBase() || r.Flash != kind || r.FlashMsg != text { |
| + t.Fatalf("reply = %d %q flash %s:%q, want %s:%q", r.Status, r.Loc, r.Flash, r.FlashMsg, kind, text) |
| + } |
| +} |
| + |
| +// wantQuote is the confirm page for a charge, showing line. |
| +func wantQuote(t *testing.T, r billingReply, line string) { |
| + t.Helper() |
| + if r.Status != http.StatusOK || !strings.Contains(r.Body, html.EscapeString(billingCopy["confirm.title"])) || |
| + !strings.Contains(r.Body, html.EscapeString(line)) || !strings.Contains(r.Body, html.EscapeString(billingCopy["confirm.button"])) { |
| + t.Fatalf("reply = %d %q flash %s:%q, want the confirm page with %q\n%s", r.Status, r.Loc, r.Flash, r.FlashMsg, line, r.Body) |
| + } |
| + if !strings.Contains(r.Body, `name="confirmed" value="1"`) { |
| + t.Fatal("the confirm page does not post confirmed=1") |
| + } |
| +} |
| + |
| +func quoteLine(currency string, minor int64) string { |
| + return fmt.Sprintf(billingCopy["confirm.line"], plans.FormatMinor(currency, minor)) |
| +} |
| + |
| +// ops is every operation the account has, oldest first. |
| +func (o *opsFixture) ops(t *testing.T) []billingOp { |
| + t.Helper() |
| + rows, err := o.st.db.Query(`SELECT id FROM billing_operations WHERE account_id = ? ORDER BY created_at, rowid`, o.acct.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var ids []string |
| + for rows.Next() { |
| + var id string |
| + if err := rows.Scan(&id); err != nil { |
| + t.Fatal(err) |
| + } |
| + ids = append(ids, id) |
| + } |
| + rows.Close() |
| + var out []billingOp |
| + for _, id := range ids { |
| + out = append(out, *o.op(t, id)) |
| + } |
| + return out |
| +} |
| + |
| +func (o *opsFixture) lastOp(t *testing.T) billingOp { |
| + t.Helper() |
| + all := o.ops(t) |
| + if len(all) == 0 { |
| + t.Fatal("no operation") |
| + } |
| + return all[len(all)-1] |
| +} |
| + |
| +// fakeChangeBody is the Fake's encoding of a subscription change. |
| +type fakeChangeBody struct { |
| + Subscription string `json:"subscription"` |
| + Items []billing.ItemChange `json:"items"` |
| + Proration billing.Proration `json:"proration"` |
| + ProrationDate int64 `json:"proration_date"` |
| +} |
| + |
| +func changeBodyOf(t *testing.T, p billing.Prepared) fakeChangeBody { |
| + t.Helper() |
| + var b fakeChangeBody |
| + if err := json.Unmarshal(p.Body, &b); err != nil { |
| + t.Fatal(err) |
| + } |
| + return b |
| +} |
| + |
| +func appsPrice() string { return testPriceID("example-v1/pack/apps/usd") } |
| + |
| +func (o *opsFixture) planRow(t *testing.T) *AccountPlanRow { |
| + t.Helper() |
| + row, err := o.st.AccountPlan(o.acct.ID) |
| + if err != nil || row == nil { |
| + t.Fatalf("plan row: %+v %v", row, err) |
| + } |
| + return row |
| +} |
| + |
| +func (o *opsFixture) invoices(reason string) int { |
| + o.fake.Lock() |
| + defer o.fake.Unlock() |
| + n := 0 |
| + for _, inv := range o.fake.Invoices { |
| + if inv.Reason == reason { |
| + n++ |
| + } |
| + } |
| + return n |
| +} |
| + |
| +// ---- the estimate -------------------------------------------------------------- |
| + |
| +func TestPackEstimateRoundsUp(t *testing.T) { |
| + end := time.Date(2027, 10, 25, 0, 0, 0, 0, time.UTC) |
| + for _, c := range []struct { |
| + annual, qty int64 |
| + now time.Time |
| + want int64 |
| + }{ |
| + {1200, 1, end.AddDate(-1, 0, 0), 1200}, // a whole year left |
| + {1200, 2, end.Add(-365 * 24 * time.Hour / 2), 1204}, // half a year (182.5 days: 183) of two |
| + {1200, 1, end.Add(-24 * time.Hour), 4}, // one day: 3.28, rounded up |
| + {1200, 1, end.Add(-time.Minute), 4}, // part of a day counts as a day |
| + {1200, 1, end, 0}, |
| + {1200, 1, end.Add(time.Hour), 0}, |
| + {1200, 1, end.AddDate(-2, 0, 0), 1200}, // never more than a year |
| + {0, 3, end.AddDate(0, -1, 0), 0}, |
| + } { |
| + if got := prorateEstimate(c.annual, c.qty, c.now, end); got != c.want { |
| + t.Errorf("prorateEstimate(%d, %d, %v) = %d, want %d", c.annual, c.qty, end.Sub(c.now), got, c.want) |
| + } |
| + } |
| +} |
| + |
| +// ---- pack changes --------------------------------------------------------------- |
| + |
| +func TestPackRaiseCharges(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + item := o.packItem(t, "sub_1", "apps") |
| + |
| + // Unconfirmed: the confirm page with the local estimate for two packs, |
| + // one read and nothing written. |
| + r := o.change(t, "pack", "apps", "quantity", "3") |
| + wantQuote(t, r, quoteLine("usd", prorateEstimate(1200, 2, o.providerClock(), periodEnd))) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + if len(o.ops(t)) != 0 { |
| + t.Fatal("the confirm page wrote an operation") |
| + } |
| + |
| + // Confirmed without a fresh sign-in proof: the step-up gate, nothing read |
| + // or sent. |
| + o.reset() |
| + r = o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1") |
| + if r.Status != http.StatusSeeOther || strings.HasPrefix(r.Loc, o.billingBase()) { |
| + t.Fatalf("confirmed without step-up = %d %q, want the step-up redirect", r.Status, r.Loc) |
| + } |
| + wantCounts(t, o.counts(), map[string]int{}) |
| + |
| + // Confirmed with one: S + (1) + S. |
| + o.stepUp(t) |
| + o.reset() |
| + r = o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1") |
| + o.wantFlash(t, r, "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + op := o.lastOp(t) |
| + if op.Kind != opPack || op.State != opApplied || op.StartQuantity == nil || *op.StartQuantity != 1 || |
| + op.Target.Quantities[appsPrice()] != 3 || !op.PeriodEnd.Equal(periodEnd) || op.Prepared.Key != "op:"+op.ID { |
| + t.Fatalf("op = %+v", op) |
| + } |
| + body := changeBodyOf(t, op.Prepared) |
| + if body.Proration != billing.ProrateNow || body.ProrationDate != o.providerClock().Unix() || |
| + len(body.Items) != 1 || body.Items[0].ItemID != item.ItemID || body.Items[0].Quantity != 3 || body.Items[0].PriceID != "" { |
| + t.Fatalf("prepared change = %+v", body) |
| + } |
| + if row := o.planRow(t); row.Packs["apps"] != 3 || row.Source != PlanSourceBilling { |
| + t.Fatalf("plan row = %+v", row) |
| + } |
| + if o.invoices(billingtest.ReasonUpdate) != 1 { |
| + t.Fatalf("proration invoices = %d, want 1", o.invoices(billingtest.ReasonUpdate)) |
| + } |
| +} |
| + |
| +func TestPackReduceAndBuyBack(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 3}) |
| + |
| + // A reduction needs no confirmation and no step-up: it charges nothing, |
| + // and the paid limits stay until renewal. |
| + r := o.change(t, "pack", "apps", "quantity", "1") |
| + o.wantFlash(t, r, "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + op := o.lastOp(t) |
| + if body := changeBodyOf(t, op.Prepared); op.Kind != opPack || body.Proration != billing.ProrateNone || body.Items[0].Quantity != 1 || |
| + op.StartQuantity == nil || *op.StartQuantity != 3 { |
| + t.Fatalf("reduction op = %+v body %+v", op, body) |
| + } |
| + if o.packItem(t, "sub_1", "apps").Quantity != 1 || o.planRow(t).Packs["apps"] != 3 { |
| + t.Fatalf("after the reduction: item %d, plan row %+v", o.packItem(t, "sub_1", "apps").Quantity, o.planRow(t)) |
| + } |
| + |
| + // Buying back up to the paid count is a free restore: no confirm page, no |
| + // step-up, one write. |
| + o.reset() |
| + r = o.change(t, "pack", "apps", "quantity", "3") |
| + o.wantFlash(t, r, "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + op = o.lastOp(t) |
| + if body := changeBodyOf(t, op.Prepared); op.Kind != opRestore || op.State != opApplied || body.Proration != billing.ProrateNone || |
| + body.Items[0].Quantity != 3 || *op.StartQuantity != 1 { |
| + t.Fatalf("restore op = %+v body %+v", op, body) |
| + } |
| + if c, err := o.st.RestoreCheck(op.ID); err != nil || c == nil || c.State != restoreClear { |
| + t.Fatalf("restore obligation = %+v %v, want clear", c, err) |
| + } |
| + if o.invoices(billingtest.ReasonUpdate) != 0 { |
| + t.Fatal("a reduction or restore was charged") |
| + } |
| + |
| + // The same quantity again is no step at all. |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3"), "err", billingCopy["change.none"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| +} |
| + |
| +func TestDeletedPackRestoreThenCharge(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 3}) |
| + old := o.packItem(t, "sub_1", "apps") |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "0"), "ok", billingCopy["change.done"]) |
| + if body := changeBodyOf(t, o.lastOp(t).Prepared); len(body.Items) != 1 || !body.Items[0].Delete || body.Items[0].ItemID != old.ItemID { |
| + t.Fatalf("a reduction to 0 = %+v, want the item deleted", body) |
| + } |
| + for _, it := range o.sub("sub_1").Items { |
| + if it.PriceID == appsPrice() { |
| + t.Fatal("the apps item is still on the subscription") |
| + } |
| + } |
| + |
| + // Unconfirmed: the estimate covers the one charged pack. |
| + o.reset() |
| + wantQuote(t, o.change(t, "pack", "apps", "quantity", "4"), quoteLine("usd", prorateEstimate(1200, 1, o.providerClock(), periodEnd))) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + |
| + // Confirmed: restore 3 as a new item, then charge the item the restore's |
| + // answer names up to 4. S + (1) + (1) + S. |
| + o.stepUp(t) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "4", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 2}) |
| + all := o.ops(t) |
| + if len(all) != 3 { |
| + t.Fatalf("ops = %d, want the reduction, the restore and the charge", len(all)) |
| + } |
| + restore, charge := all[1], all[2] |
| + now := o.packItem(t, "sub_1", "apps") |
| + rb, cb := changeBodyOf(t, restore.Prepared), changeBodyOf(t, charge.Prepared) |
| + if restore.Kind != opRestore || restore.State != opApplied || rb.Proration != billing.ProrateNone || |
| + rb.Items[0].ItemID != "" || rb.Items[0].PriceID != appsPrice() || rb.Items[0].Quantity != 3 || *restore.StartQuantity != 0 { |
| + t.Fatalf("restore = %+v body %+v", restore, rb) |
| + } |
| + if charge.Kind != opPack || charge.State != opApplied || cb.Proration != billing.ProrateNow || |
| + cb.Items[0].ItemID != now.ItemID || now.ItemID == old.ItemID || cb.Items[0].Quantity != 4 || *charge.StartQuantity != 3 { |
| + t.Fatalf("charge = %+v body %+v (item now %s)", charge, cb, now.ItemID) |
| + } |
| + if now.Quantity != 4 || o.planRow(t).Packs["apps"] != 4 { |
| + t.Fatalf("item %+v, plan row %+v", now, o.planRow(t)) |
| + } |
| + |
| + // A restore with no definite outcome is never followed by a charge. |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "0"), "ok", billingCopy["change.done"]) |
| + o.fake.Lock() |
| + o.fake.FailBeforeDelivery = 100 |
| + o.fake.Unlock() |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "5", "confirmed", "1"), "err", billingCopy["change.uncertain"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 5, "Send": 4}) |
| + if op := o.lastOp(t); op.Kind != opRestore || op.State != opUnresolved { |
| + t.Fatalf("last op = %+v, want the unresolved restore and no charge", op) |
| + } |
| +} |
| + |
| +func TestRestoreRefusedNearRenewal(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 3}) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "1"), "ok", billingCopy["change.done"]) |
| + before := len(o.ops(t)) |
| + o.stepUp(t) |
| + |
| + for _, left := range []time.Duration{24 * time.Hour, restoreMargin} { |
| + o.fake.SetProviderClock(periodEnd.Add(-left)) |
| + // A restore alone, and a restore with a charge: the whole request is |
| + // refused before any write. |
| + for _, q := range []string{"3", "4"} { |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", q, "confirmed", "1"), "err", billingCopy["change.near_renewal"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + } |
| + // Unconfirmed, the handler refuses rather than quoting a charge it |
| + // could not make. |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "4"), "err", billingCopy["change.near_renewal"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + } |
| + // A charge that needs no restore is not refused: inside the margin it is |
| + // an ordinary charged add. |
| + o.reset() |
| + wantQuote(t, o.change(t, "pack", "email", "quantity", "1"), quoteLine("usd", prorateEstimate(2400, 1, o.providerClock(), periodEnd))) |
| + |
| + // No provider time at all: change.clock, nothing written. |
| + o.fake.SetProviderClock(periodEnd.AddDate(0, -6, 0)) |
| + o.headerlessReads(false) |
| + o.fake.Lock() |
| + o.fake.ProbeErr = errors.New("probe down") |
| + o.fake.Unlock() |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3"), "err", billingCopy["change.clock"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1, "ClockProbe": 1}) |
| + if len(o.ops(t)) != before { |
| + t.Fatal("a refused restore wrote an operation") |
| + } |
| +} |
| + |
| +func TestRestoreIgnoresConsoleClock(t *testing.T) { |
| + // The renewal is three days ahead of the console's own clock, but the |
| + // provider's clock is two days ahead of the console's: provider time |
| + // says one day is left, so the restore is refused. |
| + o := newChangeFixture(t, nil) |
| + pe := time.Now().UTC().Truncate(time.Second).Add(72 * time.Hour) |
| + s := o.personalSub("sub_1", "active", map[string]int64{"apps": 3}) |
| + s.PeriodEnd = pe |
| + o.put(s) |
| + if err := o.srv.syncSubscription(context.Background(), "sub_1", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "1"), "ok", billingCopy["change.done"]) |
| + o.fake.SetProviderClock(pe.Add(-24 * time.Hour)) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3"), "err", billingCopy["change.near_renewal"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + |
| + // And the other way: the console's clock a day before the renewal, the |
| + // provider's three days before it: allowed. |
| + pe2 := time.Now().UTC().Truncate(time.Second).Add(24 * time.Hour) |
| + s = o.sub("sub_1") |
| + s.PeriodEnd = pe2 |
| + o.put(s) |
| + o.fake.SetProviderClock(pe2.Add(-72 * time.Hour)) |
| + if err := o.srv.syncSubscription(context.Background(), "sub_1", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "0"), "ok", billingCopy["change.done"]) |
| + b, _ := o.st.BillingAccount(o.acct.ID) |
| + b.PaidPacks = map[string]int{"apps": 3} // the new period's paid count, as if bought this period |
| + if err := o.st.PutBillingState(*b); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "2"), "ok", billingCopy["change.done"]) |
| + if op := o.lastOp(t); op.Kind != opRestore || op.State != opApplied { |
| + t.Fatalf("restore = %+v", op) |
| + } |
| +} |
| + |
| +func TestRestoreResendWindowEndsBeforeRenewal(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 3}) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "1"), "ok", billingCopy["change.done"]) |
| + // One second outside the margin: the first send is allowed. Its delivery |
| + // is lost, and the handler's backoff is real time passing, so by the |
| + // retry's read the restore is inside the margin and is not resent. |
| + o.fake.SetProviderClock(periodEnd.Add(-restoreMargin - time.Second)) |
| + o.srv.billing.wait = func(_ context.Context, d time.Duration) { o.fake.Elapse(d) } |
| + o.fake.Lock() |
| + o.fake.FailBeforeDelivery = 1 |
| + o.fake.Unlock() |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3"), "err", billingCopy["change.uncertain"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 3, "Send": 1}) |
| + op := o.lastOp(t) |
| + if op.Kind != opRestore || op.State != opUnresolved { |
| + t.Fatalf("restore = %+v", op) |
| + } |
| + // Confirm now does not resend it either. |
| + o.reset() |
| + if status, _ := o.confirm(t); status != http.StatusSeeOther { |
| + t.Fatalf("confirm = %d", status) |
| + } |
| + if n := o.counts()["Send"]; n != 0 { |
| + t.Fatalf("Confirm now resent the restore inside the margin: %d sends", n) |
| + } |
| +} |
| + |
| +func TestOneOutstandingOperation(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + o.stepUp(t) |
| + // A charge answered with a cached 500: indeterminate, never resent. |
| + o.fake.Lock() |
| + o.fake.Cached500Before = true |
| + o.fake.Unlock() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "2", "confirmed", "1"), "err", billingCopy["change.uncertain"]) |
| + if op := o.lastOp(t); op.State != opUnresolved || !op.Indeterminate { |
| + t.Fatalf("op = %+v", op) |
| + } |
| + |
| + // Every later change on the subscription is refused after F (one read, |
| + // no resend), and nothing new is written. |
| + for _, form := range [][]string{ |
| + {"pack", "apps", "quantity", "0"}, |
| + {"pack", "email", "quantity", "1", "confirmed", "1"}, |
| + {"plan", "community", "confirmed", "1"}, |
| + } { |
| + o.reset() |
| + o.wantFlash(t, o.change(t, form...), "err", billingCopy["change.outstanding"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + } |
| + o.reset() |
| + o.wantFlash(t, o.renewal(t, "stop"), "err", billingCopy["change.outstanding"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + if n := len(o.ops(t)); n != 1 { |
| + t.Fatalf("ops = %d, want 1", n) |
| + } |
| +} |
| + |
| +func TestRetryAfterDeclineIsRefusedUntilSettled(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + o.stepUp(t) |
| + |
| + // A soft decline leaves a pending update: the change waits for payment. |
| + o.fake.Lock() |
| + o.fake.FailPayment = 1 |
| + o.fake.Unlock() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "2", "confirmed", "1"), "err", billingCopy["change.pending"]) |
| + first := o.lastOp(t) |
| + if first.State != opPending { |
| + t.Fatalf("op = %+v", first) |
| + } |
| + // Retrying while it waits is refused after one read. |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "2", "confirmed", "1"), "err", billingCopy["change.outstanding"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + |
| + // Once the provider drops the pending update, the next request settles it |
| + // (abandoned) and charges afresh. |
| + if err := o.fake.ExpirePending("sub_1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "2", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 3, "Send": 1}) |
| + if got := o.op(t, first.ID); got.State != opAbandoned { |
| + t.Fatalf("first op = %+v, want abandoned", got) |
| + } |
| + |
| + // A hard decline is terminal: the retry is a new operation at once. |
| + o.fake.Lock() |
| + o.fake.DeclineHard = 1 |
| + o.fake.Unlock() |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "err", billingCopy["change.declined"]) |
| + if op := o.lastOp(t); op.State != opFailed { |
| + t.Fatalf("declined op = %+v", op) |
| + } |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| +} |
| + |
| +func TestChangeTwiceChargesOnce(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + o.stepUp(t) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + // The double submit: the fresh read shows 3, so there is no step. |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "err", billingCopy["change.none"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + |
| + // A first submit whose answer was lost after the provider applied it: the |
| + // second finds it applied and charges nothing more. |
| + op := o.packOp(t, "op_lost", "sub_1", "apps", 5, billing.ProrateNow, opPack) |
| + o.unresolved(t, op) |
| + o.setQuantity("sub_1", appsPrice(), 5) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "5", "confirmed", "1"), "err", billingCopy["change.none"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2}) |
| + if got := o.op(t, "op_lost"); got.State != opApplied { |
| + t.Fatalf("lost op = %+v, want applied", got) |
| + } |
| + if n := o.invoices(billingtest.ReasonUpdate); n != 1 { |
| + t.Fatalf("proration invoices = %d, want 1", n) |
| + } |
| +} |
| + |
| +func TestChangeComputesFromTheFreshRead(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + o.stepUp(t) |
| + // The provider moved the quantity since the console last read it. |
| + o.setQuantity("sub_1", appsPrice(), 3) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "4", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + op := o.lastOp(t) |
| + if body := changeBodyOf(t, op.Prepared); op.Kind != opPack || *op.StartQuantity != 3 || body.Proration != billing.ProrateNow || body.Items[0].Quantity != 4 { |
| + t.Fatalf("charge = %+v body %+v", op, body) |
| + } |
| + // The estimate is from the fresh read too: one pack, not three. |
| + o.setQuantity("sub_1", appsPrice(), 6) |
| + o.reset() |
| + wantQuote(t, o.change(t, "pack", "apps", "quantity", "7"), quoteLine("usd", prorateEstimate(1200, 1, o.providerClock(), periodEnd))) |
| + // A reduction from the fresh read. |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "5"), "ok", billingCopy["change.done"]) |
| + if op := o.lastOp(t); op.Kind != opPack || *op.StartQuantity != 6 || changeBodyOf(t, op.Prepared).Proration != billing.ProrateNone { |
| + t.Fatalf("reduction = %+v", op) |
| + } |
| +} |
| + |
| +func TestChangeAfterDelayedRenewal(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 3}) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "1"), "ok", billingCopy["change.done"]) |
| + // The subscription renews at 1; the console has not seen it. |
| + if err := o.fake.Renew("sub_1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.stepUp(t) |
| + o.reset() |
| + // Last period's paid 3 is gone: back to 3 is a charge, not a restore. |
| + wantQuote(t, o.change(t, "pack", "apps", "quantity", "3"), quoteLine("usd", prorateEstimate(1200, 2, o.providerClock(), periodEnd.AddDate(1, 0, 0)))) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + op := o.lastOp(t) |
| + if op.Kind != opPack || *op.StartQuantity != 1 || !op.PeriodEnd.Equal(periodEnd.AddDate(1, 0, 0)) || changeBodyOf(t, op.Prepared).Proration != billing.ProrateNow { |
| + t.Fatalf("charge = %+v", op) |
| + } |
| + if b, _ := o.st.BillingAccount(o.acct.ID); !b.PaidPeriodEnd.Equal(periodEnd.AddDate(1, 0, 0)) || b.PaidPacks["apps"] != 3 { |
| + t.Fatalf("billing row = %+v", b) |
| + } |
| +} |
| + |
| +func TestChangeAfterReplacementSubscription(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 3}) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "1"), "ok", billingCopy["change.done"]) |
| + // sub_1 ends; a new subscription replaces it. |
| + o.put(o.personalSub("sub_1", "canceled", map[string]int64{"apps": 1})) |
| + if err := o.srv.syncSubscription(context.Background(), "sub_1", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.put(o.personalSub("sub_2", "active", map[string]int64{"apps": 1})) |
| + if err := o.srv.syncSubscription(context.Background(), "sub_2", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if b, _ := o.st.BillingAccount(o.acct.ID); b.Subscription != "sub_2" { |
| + t.Fatalf("billing row = %+v", b) |
| + } |
| + o.stepUp(t) |
| + o.reset() |
| + // The old subscription's paid 3 does not carry: a charge on sub_2. |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + op := o.lastOp(t) |
| + if op.Subscription != "sub_2" || op.Kind != opPack || *op.StartQuantity != 1 || changeBodyOf(t, op.Prepared).Subscription != "sub_2" { |
| + t.Fatalf("charge = %+v", op) |
| + } |
| +} |
| + |
| +func TestChangeUnresolvedFinishedOnNextRequest(t *testing.T) { |
| + t.Run("applied while unanswered", func(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + o.stepUp(t) |
| + op := o.packOp(t, "op_prior", "sub_1", "apps", 2, billing.ProrateNow, opPack) |
| + o.unresolved(t, op) |
| + o.setQuantity("sub_1", appsPrice(), 2) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + // F (one read, no resend), then S + (1) + S. |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 3, "Send": 1}) |
| + if got := o.op(t, "op_prior"); got.State != opApplied { |
| + t.Fatalf("prior = %+v", got) |
| + } |
| + if next := o.lastOp(t); next.ID == "op_prior" || *next.StartQuantity != 2 || next.Target.Quantities[appsPrice()] != 3 { |
| + t.Fatalf("next = %+v", next) |
| + } |
| + }) |
| + t.Run("resent from its stored bytes", func(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + o.stepUp(t) |
| + op := o.packOp(t, "op_prior", "sub_1", "apps", 2, billing.ProrateNow, opPack) |
| + o.unresolved(t, op) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 3, "Send": 2}) |
| + sent := o.sent() |
| + if sent[0].Key != op.Prepared.Key || string(sent[0].Body) != string(op.Prepared.Body) { |
| + t.Fatalf("first send = %+v, want the stored bytes", sent[0]) |
| + } |
| + if got := o.op(t, "op_prior"); got.State != opApplied { |
| + t.Fatalf("prior = %+v", got) |
| + } |
| + if o.packItem(t, "sub_1", "apps").Quantity != 3 { |
| + t.Fatal("the new change did not apply") |
| + } |
| + }) |
| +} |
| + |
| +// ---- plan upgrades ----------------------------------------------------------------- |
| + |
| +func TestPlanUpgrade(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + members, err := o.st.MemberCount(o.acct.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + seats := int64(o.srv.cfg.PlanCatalog.Plans["community"].Team.Billed(members)) |
| + planItem, ok := billing.FindItem(o.sub("sub_1"), o.srv.billing.cfg.Prices, billing.KindPlan, "") |
| + if !ok || planItem.ItemID == "" { |
| + t.Fatalf("plan item = %+v", planItem) |
| + } |
| + |
| + wantQuote(t, o.change(t, "plan", "community"), quoteLine("usd", prorateEstimate(600*seats-2800, 1, o.providerClock(), periodEnd))) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + |
| + o.stepUp(t) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "plan", "community", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + op := o.lastOp(t) |
| + body := changeBodyOf(t, op.Prepared) |
| + community, personal := testPriceID("example-v1/community/usd"), testPriceID("example-v1/personal/usd") |
| + if op.Kind != opPlan || op.State != opApplied || body.Proration != billing.ProrateNow || len(body.Items) != 1 || |
| + body.Items[0].ItemID != planItem.ItemID || body.Items[0].PriceID != community || body.Items[0].Quantity != seats || |
| + *op.StartQuantity != 1 || op.Target.Quantities[community] != seats || op.Target.Quantities[personal] != 0 || len(op.Target.Quantities) != 2 { |
| + t.Fatalf("upgrade = %+v body %+v", op, body) |
| + } |
| + if row := o.planRow(t); row.Plan != "community" || row.Packs["apps"] != 1 { |
| + t.Fatalf("plan row = %+v", row) |
| + } |
| + |
| + // The same plan is no step; a lower one is not self-serve. |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "plan", "community", "confirmed", "1"), "err", billingCopy["change.none"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "plan", "personal", "confirmed", "1"), "err", billingCopy["change.book"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| +} |
| + |
| +func TestUpgradeRefusedWhenPackNotOffered(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"email": 1}) |
| + cat := o.srv.cfg.PlanCatalog |
| + business := cat.Plans["business"] |
| + business.Packs = []string{"apps", "disk", "storage", "bandwidth"} |
| + cat.Plans["business"] = business |
| + o.stepUp(t) |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "plan", "business", "confirmed", "1"), "err", billingCopy["change.book"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + if o.fake.Writes() != 0 { |
| + t.Fatal("a refused upgrade wrote") |
| + } |
| + // A plan that offers it is fine. |
| + o.wantFlash(t, o.change(t, "plan", "community", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| +} |
| + |
| +// subscriptionsBookPrices is the example price map plus an earlier book, |
| +// example-v0, that priced Personal, Community and the apps pack in usd only. |
| +func subscriptionsBookPrices(t *testing.T, book *plans.PriceBook) billing.PriceMap { |
| + t.Helper() |
| + raw := map[string]string{} |
| + for name := range book.Plans { |
| + for _, c := range book.Currencies { |
| + k := billing.PriceRef{Book: book.Version, Kind: billing.KindPlan, Name: name, Currency: c}.Key() |
| + raw[k] = testPriceID(k) |
| + } |
| + } |
| + for name := range book.Packs { |
| + for _, c := range book.Currencies { |
| + k := billing.PriceRef{Book: book.Version, Kind: billing.KindPack, Name: name, Currency: c}.Key() |
| + raw[k] = testPriceID(k) |
| + } |
| + } |
| + for _, k := range []string{"example-v0/personal/usd", "example-v0/community/usd", "example-v0/pack/apps/usd"} { |
| + raw[k] = testPriceID(k) |
| + } |
| + b, _ := json.Marshal(raw) |
| + m, err := billing.ParsePriceMap(string(b), book) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return m |
| +} |
| + |
| +func TestChangeUsesTheSubscriptionsBook(t *testing.T) { |
| + o := newChangeFixture(t, nil) |
| + o.srv.billing.cfg.Prices = subscriptionsBookPrices(t, o.srv.billing.cfg.Book) |
| + s := o.personalSub("sub_1", "active", nil) |
| + s.Items = []billing.Item{ |
| + {PriceID: testPriceID("example-v0/personal/usd"), Quantity: 1, Currency: "usd"}, |
| + {PriceID: testPriceID("example-v0/pack/apps/usd"), Quantity: 1, Currency: "usd"}, |
| + } |
| + o.put(s) |
| + if err := o.srv.syncSubscription(context.Background(), "sub_1", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if b, _ := o.st.BillingAccount(o.acct.ID); b.Book != "example-v0" { |
| + t.Fatalf("billing row = %+v", b) |
| + } |
| + o.stepUp(t) |
| + o.reset() |
| + |
| + // The earlier book's amounts are not known here: no estimate. |
| + wantQuote(t, o.change(t, "pack", "apps", "quantity", "2"), billingCopy["change.no_estimate"]) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "2", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + if op := o.lastOp(t); op.Target.Quantities[testPriceID("example-v0/pack/apps/usd")] != 2 { |
| + t.Fatalf("pack change = %+v", op) |
| + } |
| + // A pack the earlier book never priced is not available. |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "pack", "email", "quantity", "1", "confirmed", "1"), "err", billingCopy["change.book"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + // An upgrade is priced from the subscription's book. |
| + o.wantFlash(t, o.change(t, "plan", "community", "confirmed", "1"), "ok", billingCopy["change.done"]) |
| + if body := changeBodyOf(t, o.lastOp(t).Prepared); body.Items[0].PriceID != testPriceID("example-v0/community/usd") { |
| + t.Fatalf("upgrade = %+v", body) |
| + } |
| + // One the earlier book never priced is not. |
| + o.reset() |
| + o.wantFlash(t, o.change(t, "plan", "business", "confirmed", "1"), "err", billingCopy["change.book"]) |
| + if o.counts()["Send"] != 0 { |
| + t.Fatal("an unpriced upgrade was sent") |
| + } |
| +} |
| + |
| +// ---- renewal -------------------------------------------------------------------------- |
| + |
| +func TestRenewalStopAndResume(t *testing.T) { |
| + o := newChangeFixture(t, nil) |
| + // No step-up is needed, and stopping makes one write. |
| + r := o.renewal(t, "stop") |
| + o.wantFlash(t, r, "ok", fmt.Sprintf(billingCopy["renewal.stopped"], billingDate(periodEnd))) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + op := o.lastOp(t) |
| + if op.Kind != opRenewal || op.State != opApplied || !op.Target.Renewal || !op.Target.CancelAtPeriodEnd { |
| + t.Fatalf("op = %+v", op) |
| + } |
| + if !o.sub("sub_1").CancelAtPeriodEnd { |
| + t.Fatal("the provider still renews") |
| + } |
| + if b, _ := o.st.BillingAccount(o.acct.ID); !b.CancelAtPeriodEnd { |
| + t.Fatalf("billing row = %+v", b) |
| + } |
| + // Stopping again writes nothing. |
| + o.reset() |
| + o.wantFlash(t, o.renewal(t, "stop"), "ok", fmt.Sprintf(billingCopy["renewal.stopped"], billingDate(periodEnd))) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 1}) |
| + |
| + o.reset() |
| + o.wantFlash(t, o.renewal(t, "resume"), "ok", billingCopy["renewal.resumed"]) |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 2, "Send": 1}) |
| + if o.sub("sub_1").CancelAtPeriodEnd { |
| + t.Fatal("the provider still ends it") |
| + } |
| + |
| + o.reset() |
| + o.wantFlash(t, o.renewal(t, "maybe"), "err", billingCopy["change.failed"]) |
| + wantCounts(t, o.counts(), map[string]int{}) |
| +} |
| + |
| +func TestRenewalDoors(t *testing.T) { |
| + o := newChangeFixture(t, nil) |
| + // No CSRF: 403, nothing read. |
| + for _, path := range []string{"/renewal", "/change"} { |
| + rr := httpPostNoCSRF(t, o, o.billingBase()+path, url.Values{"renewal": {"stop"}, "pack": {"apps"}, "quantity": {"1"}}) |
| + if rr != http.StatusForbidden { |
| + t.Fatalf("%s without CSRF = %d", path, rr) |
| + } |
| + } |
| + wantCounts(t, o.counts(), map[string]int{}) |
| + // Retiring: refused before any read. |
| + if err := o.st.BeginBillingRetirement(o.acct.ID, "owner", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.wantFlash(t, o.renewal(t, "stop"), "err", billingCopy["billing.retiring"]) |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "1"), "err", billingCopy["billing.retiring"]) |
| + wantCounts(t, o.counts(), map[string]int{}) |
| + // Billing off: 404. |
| + o.srv.billing = nil |
| + for _, path := range []string{"/renewal", "/change"} { |
| + if r := o.request(t, http.MethodPost, o.billingBase()+path, url.Values{"renewal": {"stop"}}); r.Status != http.StatusNotFound { |
| + t.Fatalf("%s with billing off = %d", path, r.Status) |
| + } |
| + } |
| +} |
| + |
| +func TestPackChangeRefusesOperatorPlanAndProblems(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + if _, err := o.st.OpenBillingProblem(o.acct.ID, "sub_1", problemUnrecognised, "test", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "0"), "err", billingCopy["notice.problem"]) |
| + wantCounts(t, o.counts(), map[string]int{}) |
| + |
| + // An operator's plan: billing leaves it alone. |
| + o = newChangeFixture(t, map[string]int64{"apps": 1}) |
| + if _, err := o.st.db.Exec(`UPDATE account_plans SET source = ? WHERE account_id = ?`, PlanSourceOperator, o.acct.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + o.wantFlash(t, o.change(t, "pack", "apps", "quantity", "0"), "err", billingCopy["notice.operator_plan"]) |
| + wantCounts(t, o.counts(), map[string]int{}) |
| + |
| + // Bad input: change.failed, nothing read. |
| + o = newChangeFixture(t, map[string]int64{"apps": 1}) |
| + for _, form := range [][]string{ |
| + {"pack", "nope", "quantity", "1"}, |
| + {"pack", "apps", "quantity", "-1"}, |
| + {"pack", "apps", "quantity", "21"}, |
| + {"pack", "apps"}, |
| + {"plan", "free"}, |
| + {}, |
| + } { |
| + o.wantFlash(t, o.change(t, form...), "err", billingCopy["change.failed"]) |
| + } |
| + wantCounts(t, o.counts(), map[string]int{}) |
| +} |
| + |
| +// httpPostNoCSRF posts form as the owner without the CSRF field. |
| +func httpPostNoCSRF(t *testing.T, o *opsFixture, path string, form url.Values) int { |
| + t.Helper() |
| + req, _ := http.NewRequest(http.MethodPost, o.ts.URL+path, strings.NewReader(form.Encode())) |
| + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| + resp, err := o.owner.Do(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + resp.Body.Close() |
| + return resp.StatusCode |
| +} |
| + |
| +// ---- the timeout -------------------------------------------------------------------------- |
| + |
| +func TestChangeAndRenewalReturnUnderTimeout(t *testing.T) { |
| + for _, c := range []struct { |
| + name string |
| + do func(t *testing.T, o *opsFixture) billingReply |
| + }{ |
| + {"change", func(t *testing.T, o *opsFixture) billingReply { |
| + return o.change(t, "pack", "apps", "quantity", "3", "confirmed", "1") |
| + }}, |
| + {"renewal", func(t *testing.T, o *opsFixture) billingReply { return o.renewal(t, "stop") }}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + o := newChangeFixture(t, map[string]int64{"apps": 1}) |
| + o.stepUp(t) |
| + var waited time.Duration |
| + o.srv.billing.wait = func(_ context.Context, d time.Duration) { |
| + waited += d |
| + o.fake.AdvanceMonotonic(d) |
| + time.Sleep(d / 50) |
| + } |
| + o.fake.Lock() |
| + o.fake.FailBeforeDelivery = 100 |
| + o.fake.Unlock() |
| + o.reset() |
| + done := make(chan billingReply, 1) |
| + go func() { done <- c.do(t, o) }() |
| + select { |
| + case r := <-done: |
| + o.wantFlash(t, r, "err", billingCopy["change.uncertain"]) |
| + case <-time.After(15 * time.Second): |
| + t.Fatal("the handler did not return within 15 s") |
| + } |
| + // One send and three retries, each after a read; about 7 s of |
| + // backoff in all. |
| + wantCounts(t, o.counts(), map[string]int{"Subscription": 5, "Send": 4}) |
| + if waited != 7*time.Second { |
| + t.Fatalf("backoff = %v, want 7s", waited) |
| + } |
| + }) |
| + } |
| +} |