package web import ( "net/http" "net/url" "strings" "testing" "time" "supermarket/internal/billing" "supermarket/internal/users" ) func TestSeats(t *testing.T) { s, h := newBilled(t) f := newFakeStripe(t, s) 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 invite := func(c *http.Cookie, email string, more url.Values) (int, string) { form := url.Values{"email": {email}, "name": {"X"}, "role": {"admin"}} for k, v := range more { form[k] = v } rec := as(h, c, http.MethodPost, "/settings/users", form) return rec.Code, rec.Body.String() } // The trial has no limit. for _, e := range []string{"a@acme.example", "b@acme.example"} { if code, body := invite(c, e, nil); code != http.StatusSeeOther { t.Fatalf("trial invite %s: %d %s", e, code, body) } } // Free: two people, and the team has three already. setAccount(t, s, team, func(a *billing.Account) { a.Status = billing.Free }) if code, body := invite(c, "c@acme.example", nil); code != http.StatusUnprocessableEntity || !strings.Contains(body, "Free plan has 2 people") { t.Errorf("free invite: %d", code) } // Paid for four: one more fits, then the owner's asked to add a seat. setAccount(t, s, team, func(a *billing.Account) { a.Status, a.Plan, a.Subscription, a.Seats = billing.Active, "paid", "sub_1", 4 }) if code, body := invite(c, "c@acme.example", nil); code != http.StatusSeeOther { t.Fatalf("fourth: %d %s", code, body) } code, body := invite(c, "d@acme.example", nil) if code != http.StatusUnprocessableEntity || !strings.Contains(body, "All 4 seats") || !strings.Contains(body, `name="add_seat"`) { t.Errorf("fifth: %d %s", code, body) } if code, body := invite(c, "d@acme.example", url.Values{"add_seat": {"1"}}); code != http.StatusSeeOther { t.Fatalf("with a seat: %d %s", code, body) } if f.qty != "5" { t.Errorf("Stripe's quantity %q", f.qty) } if a, _ := billing.Get(s.platform(), team); a.Seats != 5 { t.Errorf("seats %d", a.Seats) } // An admin is told to ask the owner, and can't add one. admin := &users.User{Email: "e@acme.example", Name: "E", Status: users.Active} users.Create(s.platform(), admin, time.Now()) users.Join(s.platform(), team, admin.ID, users.Admin, nil, time.Now()) setAccount(t, s, team, func(a *billing.Account) { a.Seats = 6 }) token, _, _ := users.StartSessionIn(s.platform(), admin.ID, team, "", "", time.Now()) ac := &http.Cookie{Name: sessionCookie, Value: token} code, body = invite(ac, "f@acme.example", url.Values{"add_seat": {"1"}}) if code != http.StatusUnprocessableEntity || !strings.Contains(body, "Ask the team's owner") || strings.Contains(body, `name="add_seat"`) { t.Errorf("admin: %d %s", code, body) } if f.qty != "5" { t.Error("an admin added a seat") } // Turning someone back on needs a seat too. b, _ := users.ByEmail(s.platform(), "b@acme.example") as(h, c, http.MethodPost, "/settings/users/"+itoa64(b.ID)+"/disable", nil) invite(c, "g@acme.example", nil) if rec := as(h, c, http.MethodPost, "/settings/users/"+itoa64(b.ID)+"/enable", nil); rec.Code != http.StatusUnprocessableEntity || !strings.Contains(rec.Body.String(), "Turn someone else off") { t.Errorf("enable: %d", rec.Code) } }