Phase 13, part 8: a team's data is theirs
The owner exports everything from Settings → Your data (the keys only with their password), and can delete the team: it closes at once, everyone in it is told, it can be undone for 30 days, and then its schema, files, agents and memberships go for good. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -18,6 +20,7 @@ import (
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/posting"
|
||||
"supermarket/internal/transfer"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
@@ -452,3 +455,99 @@ func TestPlatformAdmin(t *testing.T) {
|
||||
t.Errorf("after the time: %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTeamDataExportAndDeletion(t *testing.T) {
|
||||
s, h := newMultiEdition(t)
|
||||
a, owner, ac := newTeam(t, s, "Acme", "olu@acme.example")
|
||||
newTeam(t, s, "Bolt", "bea@bolt.example")
|
||||
as(h, ac, http.MethodPost, "/market/save", url.Values{"name": {"Rival"}, "relationship": {"competitor"}, "description": {"x"}})
|
||||
admin := addUserIn(t, s, a.ID, "ada@acme.example", users.Admin)
|
||||
adc := sessionIn(t, s, admin, a.ID)
|
||||
if rec := as(h, adc, http.MethodGet, "/settings/data", nil); rec.Code != http.StatusForbidden {
|
||||
t.Errorf("admin on Your data: %d", rec.Code)
|
||||
}
|
||||
// Export, without and with the keys.
|
||||
if rec := as(h, ac, http.MethodPost, "/settings/data/export", url.Values{"with_secrets": {"on"}, "password": {"wrong"}}); rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("keys without the password: %d", rec.Code)
|
||||
}
|
||||
rec := as(h, ac, http.MethodPost, "/settings/data/export", url.Values{})
|
||||
if rec.Code != http.StatusSeeOther || !strings.Contains(rec.Header().Get("Location"), "exported") {
|
||||
t.Fatalf("export: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
body := as(h, ac, http.MethodGet, "/settings/data", nil).Body.String()
|
||||
m := regexp.MustCompile(`href="/settings/data/exports/(supermarket-[0-9-]+\.tar\.gz)"`).FindStringSubmatch(body)
|
||||
if m == nil {
|
||||
t.Fatalf("no export listed: %s", body)
|
||||
}
|
||||
dl := as(h, ac, http.MethodGet, "/settings/data/exports/"+m[1], nil)
|
||||
man, err := transfer.ReadManifest(bytes.NewReader(dl.Body.Bytes()))
|
||||
if err != nil || man.Tables["inventory"] != 1 || man.Tables["users"] != 0 {
|
||||
t.Errorf("manifest %+v %v", man, err)
|
||||
}
|
||||
// Bolt can't download Acme's.
|
||||
_, _, bc := newTeamOwnerCookie(t, s, 2)
|
||||
if rec := as(h, bc, http.MethodGet, "/settings/data/exports/"+m[1], nil); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("another team's export: %d", rec.Code)
|
||||
}
|
||||
|
||||
// Delete: the name and the password; then closed, everyone told.
|
||||
if rec := as(h, ac, http.MethodPost, "/settings/data/delete-team", url.Values{"confirm": {"Acmee"}, "password": {"a long enough one"}}); rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("wrong name: %d", rec.Code)
|
||||
}
|
||||
as(h, ac, http.MethodPost, "/settings/data/delete-team", url.Values{"confirm": {"acme"}, "password": {"a long enough one"}})
|
||||
tm, _ := users.GetTeam(s.platform(), a.ID)
|
||||
if tm.Status != users.TeamDeleting || tm.DeleteAt.Before(time.Now().Add(29*24*time.Hour)) {
|
||||
t.Fatalf("deleting: %+v", tm)
|
||||
}
|
||||
waitMail(t, s, "ada@acme.example", 1)
|
||||
rec = as(h, ac, http.MethodGet, "/market", nil)
|
||||
if rec.Code != http.StatusForbidden || !strings.Contains(rec.Body.String(), "/teams/undelete") {
|
||||
t.Errorf("closed: %d", rec.Code)
|
||||
}
|
||||
if rec := as(h, adc, http.MethodPost, "/teams/undelete", url.Values{}); rec.Code != http.StatusForbidden {
|
||||
t.Errorf("an admin undoes: %d", rec.Code)
|
||||
}
|
||||
as(h, ac, http.MethodPost, "/teams/undelete", url.Values{})
|
||||
if tm, _ := users.GetTeam(s.platform(), a.ID); tm.Status != users.TeamActive {
|
||||
t.Error("not undone")
|
||||
}
|
||||
|
||||
// After 30 days: gone, all of it; the person keeps their account.
|
||||
users.SetTeamStatus(s.platform(), a.ID, users.TeamDeleting, time.Now().Add(-time.Hour))
|
||||
s.purgeTeams(context.Background(), time.Now())
|
||||
if _, err := users.GetTeam(s.platform(), a.ID); err == nil {
|
||||
t.Error("team row kept")
|
||||
}
|
||||
if _, err := s.Teams.Open(a.ID); err == nil {
|
||||
t.Error("schema kept")
|
||||
}
|
||||
if list, _ := users.Members(s.platform(), a.ID); len(list) != 0 {
|
||||
t.Error("memberships kept")
|
||||
}
|
||||
if files, _ := s.Files.List(context.Background(), "teams/"+strconv.FormatInt(a.ID, 10)+"/"); len(files) != 0 {
|
||||
t.Errorf("files kept: %v", files)
|
||||
}
|
||||
if _, err := users.ByID(s.platform(), owner.ID); err != nil {
|
||||
t.Error("the person's account went")
|
||||
}
|
||||
}
|
||||
|
||||
func addUserIn(t *testing.T, s *Server, team int64, email string, role users.Role) *users.User {
|
||||
t.Helper()
|
||||
u := &users.User{Email: email, Name: email, Status: users.Active}
|
||||
if err := users.Create(s.platform(), u, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users.SetPassword(s.platform(), u, "a long enough one")
|
||||
users.Join(s.platform(), team, u.ID, role, nil, time.Now())
|
||||
return u
|
||||
}
|
||||
|
||||
func sessionIn(t *testing.T, s *Server, u *users.User, team int64) *http.Cookie {
|
||||
t.Helper()
|
||||
tok, _, err := users.StartSessionIn(s.platform(), u.ID, team, "", "", time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &http.Cookie{Name: sessionCookie, Value: tok}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user