Phase 14, part 1: plans, each team's account, and their limits
The multi-tenant edition's plans (Free and Paid as decided, or a plans file) and each team's billing account, with a 14-day trial of Paid. A team's plan caps what its people and agents may change: modules outside it stay readable, a lapsed team changes nothing, and agents, Market entries, crawls and background jobs keep to it. Without a Stripe key, no billing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/billing"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
// newBilled is the multi-tenant edition with billing on.
|
||||
func newBilled(t *testing.T) (*Server, http.Handler) {
|
||||
t.Helper()
|
||||
s, h := newMultiEdition(t)
|
||||
p, err := billing.Load("", "price_month", "price_year", "price_block")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.Plans = p
|
||||
return s, h
|
||||
}
|
||||
|
||||
// signUp makes a team through the sign-up page (so it gets its trial).
|
||||
func signUp(t *testing.T, h http.Handler, email, company, site string) *http.Cookie {
|
||||
t.Helper()
|
||||
rec := postForm(h, "/signup", url.Values{"email": {email}, "password": {"a long enough one"}, "company": {company}, "website": {site}})
|
||||
c := cookieOf(rec, sessionCookie)
|
||||
if rec.Code != http.StatusSeeOther || c == nil {
|
||||
t.Fatalf("sign up: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// setAccount changes a team's billing, as Stripe's events would.
|
||||
func setAccount(t *testing.T, s *Server, team int64, fn func(a *billing.Account)) {
|
||||
t.Helper()
|
||||
a, err := billing.Get(s.platform(), team)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fn(a)
|
||||
if err := billing.Save(s.platform(), a, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlansLimitTeams(t *testing.T) {
|
||||
s, h := newBilled(t)
|
||||
c := signUp(t, h, "olu@acme.example", "Acme", "acme.example")
|
||||
u, _ := users.ByEmail(s.platform(), "olu@acme.example")
|
||||
list, _ := users.TeamsOf(s.platform(), u.ID)
|
||||
team := list[0].ID
|
||||
ts, _ := s.forTeam(team)
|
||||
|
||||
// A new team's trial: Paid, everything.
|
||||
a, _ := billing.Get(s.platform(), team)
|
||||
if a.Status != billing.Trialing || a.TrialEnds.Before(time.Now().Add(13*24*time.Hour)) {
|
||||
t.Fatalf("trial %+v", a)
|
||||
}
|
||||
if rec := as(h, c, http.MethodPost, "/prospects/new", url.Values{"company_name": {"Lead Co"}, "website": {"https://lead.example"}}); rec.Code == http.StatusForbidden {
|
||||
t.Error("Prospects closed on the trial")
|
||||
}
|
||||
if !ts.jobAllowed("outreach") {
|
||||
t.Error("outreach job held on the trial")
|
||||
}
|
||||
if n, every := ts.crawlLimits(); n != 10000 || every != 24*time.Hour {
|
||||
t.Errorf("trial crawl %d %v", n, every)
|
||||
}
|
||||
|
||||
// The trial over, without a subscription: Free.
|
||||
setAccount(t, s, team, func(a *billing.Account) { a.TrialEnds = time.Now().Add(-time.Hour) })
|
||||
if rec := as(h, c, http.MethodPost, "/prospects/new", url.Values{"company_name": {"Other"}, "website": {"https://other.example"}}); rec.Code != http.StatusForbidden || !strings.Contains(rec.Body.String(), "Free plan") {
|
||||
t.Errorf("Prospects on Free: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := as(h, c, http.MethodGet, "/prospects", nil); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Lead Co") {
|
||||
t.Errorf("Prospects' data on Free: %d", rec.Code)
|
||||
}
|
||||
if ts.jobAllowed("outreach") || !ts.jobAllowed("seo") {
|
||||
t.Error("jobs on Free")
|
||||
}
|
||||
if n, every := ts.crawlLimits(); n != 500 || every != 7*24*time.Hour {
|
||||
t.Errorf("free crawl %d %v", n, every)
|
||||
}
|
||||
// One agent on Free.
|
||||
if rec := as(h, c, http.MethodPost, "/settings/agents", url.Values{"name": {"One"}, "scope:seo": {"read"}}); rec.Code != http.StatusOK {
|
||||
t.Fatalf("first agent: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := as(h, c, http.MethodPost, "/settings/agents", url.Values{"name": {"Two"}, "scope:seo": {"read"}}); rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "1 agents") {
|
||||
t.Errorf("second agent on Free: %d", rec.Code)
|
||||
}
|
||||
// Ten Market entries on Free.
|
||||
for i := 0; i < 10; i++ {
|
||||
if rec := as(h, c, http.MethodPost, "/market/save", url.Values{"name": {"Entry " + strconv.Itoa(i)}, "relationship": {"partner"}, "description": {"x"}}); rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("entry %d: %d", i, rec.Code)
|
||||
}
|
||||
}
|
||||
if rec := as(h, c, http.MethodPost, "/market/save", url.Values{"name": {"Eleven"}, "relationship": {"partner"}, "description": {"x"}}); rec.Code == http.StatusSeeOther {
|
||||
t.Error("an eleventh entry on Free")
|
||||
}
|
||||
|
||||
// Paid and active: everything again.
|
||||
setAccount(t, s, team, func(a *billing.Account) { a.Status = billing.Active })
|
||||
if rec := as(h, c, http.MethodPost, "/market/save", url.Values{"name": {"Eleven"}, "relationship": {"partner"}, "description": {"x"}}); rec.Code != http.StatusSeeOther {
|
||||
t.Errorf("Paid: %d", rec.Code)
|
||||
}
|
||||
|
||||
// Past due for 15 days: read-only, for people and agents.
|
||||
setAccount(t, s, team, func(a *billing.Account) {
|
||||
a.Status, a.PastDueSince = billing.PastDue, time.Now().Add(-15*24*time.Hour)
|
||||
})
|
||||
rec := as(h, c, http.MethodPost, "/market/save", url.Values{"name": {"Twelve"}, "relationship": {"partner"}, "description": {"x"}})
|
||||
if rec.Code != http.StatusForbidden || !strings.Contains(rec.Body.String(), "lapsed") {
|
||||
t.Errorf("lapsed: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := as(h, c, http.MethodGet, "/market", nil); rec.Code != http.StatusOK {
|
||||
t.Errorf("lapsed, reading: %d", rec.Code)
|
||||
}
|
||||
_, key := newAgentIn(t, ts, "Late", map[string]users.Access{"market": users.View})
|
||||
if code, _ := mcpTools(h, key); code != http.StatusForbidden {
|
||||
t.Errorf("agent of a lapsed team: %d", code)
|
||||
}
|
||||
if ts.jobAllowed("seo") || !ts.jobAllowed("accounts") {
|
||||
t.Error("jobs of a lapsed team")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoBillingNoLimits(t *testing.T) {
|
||||
s, h := newMultiEdition(t)
|
||||
c := signUp(t, h, "olu@acme.example", "Acme", "acme.example")
|
||||
if rec := as(h, c, http.MethodPost, "/prospects/new", url.Values{"company_name": {"Lead"}, "website": {"https://lead.example"}}); rec.Code == http.StatusForbidden {
|
||||
t.Error("limited without billing")
|
||||
}
|
||||
if a, _ := billing.Get(s.platform(), 1); a.Status != billing.Free || a.Customer != "" {
|
||||
t.Errorf("an account without billing: %+v", a)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user