package web import ( "context" "net/http" "net/url" "strings" "testing" "time" "supermarket/internal/billing" "supermarket/internal/users" ) // Moving down to Free: the people past its two read, and storage past its // gigabyte keeps nothing new. Nothing's deleted. func TestMovingDownToFree(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 person := func(email string, role users.Role, mods map[string]users.Access, joined time.Time) (*users.User, *http.Cookie) { p := &users.User{Email: email, Name: strings.Split(email, "@")[0], Status: users.Active} if err := users.Create(s.platform(), p, joined); err != nil { t.Fatal(err) } if err := users.Join(s.platform(), team, p.ID, role, mods, joined); err != nil { t.Fatal(err) } token, _, _ := users.StartSessionIn(s.platform(), p.ID, team, "", "", time.Now()) return p, &http.Cookie{Name: sessionCookie, Value: token} } ada, ac := person("ada@acme.example", users.Admin, nil, time.Now().Add(time.Hour)) bo, bc := person("bo@acme.example", users.Member, map[string]users.Access{"content": users.Edit}, time.Now().Add(2*time.Hour)) write := func(c *http.Cookie) *http.Response { return as(h, c, http.MethodPost, "/content/999/fetch", nil).Result() } // On the trial, everyone edits. if r := write(bc); r.StatusCode == http.StatusForbidden { t.Fatal("Bo can't edit on the trial") } // Free: the owner and the first to join edit; Bo reads. setAccount(t, s, team, func(a *billing.Account) { a.Status = billing.Free }) if r := write(ac); r.StatusCode == http.StatusForbidden { t.Error("Ada can't edit") } rec := as(h, bc, http.MethodPost, "/content/999/fetch", nil) if rec.Code != http.StatusForbidden || !strings.Contains(rec.Body.String(), "Free plan has 2 people") { t.Errorf("Bo: %d %s", rec.Code, rec.Body) } if rec := as(h, bc, http.MethodGet, "/content", nil); rec.Code != http.StatusOK { t.Errorf("Bo can't read: %d", rec.Code) } // The owner keeps Bo instead: Ada reads, as a member, everything. rec = as(h, c, http.MethodGet, "/settings/billing", nil) if !strings.Contains(rec.Body.String(), "Who keeps editing") { t.Errorf("billing page: %s", rec.Body) } if rec := as(h, c, http.MethodPost, "/settings/billing/keep", url.Values{"user": {itoa64(bo.ID)}}); rec.Code != http.StatusSeeOther { t.Fatalf("keep: %d %s", rec.Code, rec.Body) } if r := write(bc); r.StatusCode == http.StatusForbidden { t.Error("Bo still can't edit") } if r := write(ac); r.StatusCode != http.StatusForbidden { t.Error("Ada still edits") } if rec := as(h, ac, http.MethodPost, "/settings/users", url.Values{"email": {"x@acme.example"}, "name": {"X"}}); rec.Code != http.StatusForbidden { t.Errorf("Ada still invites: %d", rec.Code) } if rec := as(h, ac, http.MethodGet, "/content", nil); rec.Code != http.StatusOK { t.Errorf("Ada can't read: %d", rec.Code) } if rec := as(h, ac, http.MethodPost, "/settings/billing/keep", url.Values{"user": {itoa64(ada.ID)}}); rec.Code != http.StatusForbidden { t.Errorf("Ada chose: %d", rec.Code) } if n, _ := users.Members(s.platform(), team); len(n) < 3 { t.Error("someone was removed") } // Storage: measured each hour; past the plan's, nothing new is kept. ts, _ := s.forTeam(team) ts.Files.Put(context.Background(), "exports/x.tar.gz", strings.NewReader("12345"), 5) s.billingTurn(context.Background(), time.Now()) a, _ := billing.Get(s.platform(), team) if a.StorageUsed < 5 { t.Errorf("measured %d", a.StorageUsed) } if ts.storageFull() != nil || !ts.jobAllowed("seo") { t.Error("full already") } setAccount(t, s, team, func(a *billing.Account) { a.StorageUsed = 2 << 30 }) if err := ts.storageFull(); err == nil || !strings.Contains(err.Error(), "storage is full") { t.Errorf("full: %v", err) } if ts.jobAllowed("seo") { t.Error("the crawl runs with the storage full") } }