| index d0382c212..d79416146 100644 |
| --- a/internal/billingstripe/annual_test.go |
| +++ b/internal/billingstripe/annual_test.go |
| @@ -1,11 +1,14 @@ |
| package billingstripe |
| |
| import ( |
| + "bytes" |
| "context" |
| "fmt" |
| + "log" |
| "net/http" |
| "net/http/httptest" |
| "reflect" |
| + "strings" |
| "testing" |
| "time" |
| ) |
| @@ -37,7 +40,10 @@ func TestAnnualOf(t *testing.T) { |
| {"50% forever", sub([]SubscriptionItem{personal}, pct(50, "forever", time.Time{})), 1400, true}, |
| {"amount off", sub([]SubscriptionItem{personal}, off(500, "usd")), 2300, true}, |
| {"amount off over the total", sub([]SubscriptionItem{personal}, off(5000, "usd")), 0, true}, |
| - {"once", sub([]SubscriptionItem{personal}, pct(50, "once", time.Time{})), 2800, true}, |
| + {"once still attached", sub([]SubscriptionItem{personal}, pct(50, "once", time.Time{})), 0, false}, |
| + {"amount off then percent", sub([]SubscriptionItem{personal}, off(500, "usd"), pct(50, "forever", time.Time{})), 1150, true}, |
| + {"percent then amount off", sub([]SubscriptionItem{personal}, pct(50, "forever", time.Time{}), off(500, "usd")), 900, true}, |
| + {"floored before a later discount", sub([]SubscriptionItem{personal}, off(5000, "usd"), pct(50, "forever", time.Time{})), 0, true}, |
| {"repeating past the renewal", sub([]SubscriptionItem{personal}, pct(50, "repeating", periodEnd.AddDate(0, 1, 0))), 1400, true}, |
| {"repeating ends before the renewal", sub([]SubscriptionItem{personal}, pct(50, "repeating", periodEnd.AddDate(0, -1, 0))), 2800, true}, |
| {"amount off in another currency", sub([]SubscriptionItem{personal}, off(500, "eur")), 0, false}, |
| @@ -124,6 +130,13 @@ func TestSubscriptionAnnualReadsCoupons(t *testing.T) { |
| })) |
| return prepProvider(t, srv.URL, nil), srv.Close |
| } |
| + // The coupon failure is logged: capture the log so it is asserted, not |
| + // printed. |
| + var logged bytes.Buffer |
| + prevOut, prevFlags := log.Writer(), log.Flags() |
| + log.SetOutput(&logged) |
| + log.SetFlags(0) |
| + t.Cleanup(func() { log.SetOutput(prevOut); log.SetFlags(prevFlags) }) |
| |
| p, done := run("half", func(w http.ResponseWriter) { |
| fmt.Fprint(w, `{"id":"half","valid":true,"livemode":false,"percent_off":50,"duration":"forever"}`) |
| @@ -142,6 +155,9 @@ func TestSubscriptionAnnualReadsCoupons(t *testing.T) { |
| if err != nil || sub.AnnualKnown || sub.Annual != 0 || !sub.FreshRead { |
| t.Errorf("product-limited: %+v, %v", sub, err) |
| } |
| + if logged.Len() != 0 { |
| + t.Errorf("readable coupons logged: %q", logged.String()) |
| + } |
| |
| p, done = run("coupon fails", func(w http.ResponseWriter) { |
| w.WriteHeader(http.StatusInternalServerError) |
| @@ -155,6 +171,10 @@ func TestSubscriptionAnnualReadsCoupons(t *testing.T) { |
| if sub.AnnualKnown || sub.Annual != 0 || !sub.FreshRead { |
| t.Errorf("coupon fails: %+v", sub) |
| } |
| + const want = `billingstripe: subscription sub_1: reading coupon "half": billingstripe: api_error status=500 stripe_type=api_error; its yearly amount is unknown` |
| + if got := strings.TrimSpace(logged.String()); got != want { |
| + t.Errorf("coupon fails: logged %q, want %q", got, want) |
| + } |
| if sub.ID != "sub_1" || sub.Customer != "cus_1" || sub.Account != "bab" || sub.Currency != "usd" || sub.ProviderStatus != "active" || |
| len(sub.Items) != 1 || sub.Items[0].PriceID != "price_p" || !sub.PeriodEnd.Equal(time.Unix(1824422400, 0).UTC()) { |
| t.Errorf("coupon fails: other fields not mapped: %+v", sub) |