Phase 13, part 7: the platform's own admin

The people running the multi-tenant service (SUPERMARKET_PLATFORM_ADMINS,
with two-factor sign-in) see every team at /platform with its size, people
and what failed lately, and can suspend or restore a team, resend its
invitations and release its domain. They don't see a team's data unless
they view as it: with a reason, for at most an hour, read-only, recorded in
the team's own audit log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 12:35:53 -07:00
co-authored by Claude Opus 5.5
parent 76407cdea8
commit d35ed7cbb2
15 changed files with 459 additions and 10 deletions
+72
View File
@@ -12,6 +12,7 @@ import (
"testing"
"time"
"supermarket/internal/audit"
"supermarket/internal/config"
"supermarket/internal/content"
"supermarket/internal/db"
@@ -380,3 +381,74 @@ func TestCorePerTeam(t *testing.T) {
t.Error("not on Connections")
}
}
func TestPlatformAdmin(t *testing.T) {
s, h := newMultiEdition(t)
s.Config.PlatformAdmins = []string{"ops@platform.example"}
a, _, ac := newTeam(t, s, "Acme", "olu@acme.example")
as(h, ac, http.MethodPost, "/settings/profile", url.Values{"name": {"Acme Inc"}, "description": {"Secret plans"}})
as(h, ac, http.MethodPost, "/market/save", url.Values{"name": {"Rival"}, "relationship": {"competitor"}, "description": {"x"}})
ops := &users.User{Email: "ops@platform.example", Name: "Ops", Status: users.Active}
users.Create(s.platform(), ops, time.Now())
users.SetPassword(s.platform(), ops, "a long enough one")
// Not in any team, they can still sign in, to the platform.
rec := signin(h, "203.0.113.9", url.Values{"email": {"ops@platform.example"}, "password": {"a long enough one"}})
oc := cookieOf(rec, sessionCookie)
if oc == nil {
t.Fatalf("sign in: %d %s", rec.Code, rec.Body.String())
}
if rec := as(h, oc, http.MethodGet, "/market", nil); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/platform" {
t.Errorf("a team page: %d %s", rec.Code, rec.Header().Get("Location"))
}
if body := as(h, oc, http.MethodGet, "/platform", nil).Body.String(); !strings.Contains(body, "two-factor") {
t.Error("no two-factor demanded")
}
users.SetTwoFactor(s.platform(), ops, true, []string{"x"})
body := as(h, oc, http.MethodGet, "/platform", nil).Body.String()
if !strings.Contains(body, "Acme") || !strings.Contains(body, "acme.example") || strings.Contains(body, "Secret plans") || strings.Contains(body, "Rival") {
t.Errorf("platform page: %s", body)
}
// A team's owner isn't a platform admin.
if rec := as(h, ac, http.MethodGet, "/platform", nil); rec.Code != http.StatusForbidden {
t.Errorf("owner on /platform: %d", rec.Code)
}
id := strconv.FormatInt(a.ID, 10)
// Suspend: the team's people see it closed; restore.
as(h, oc, http.MethodPost, "/platform/teams/"+id+"/suspend", url.Values{"reason": {"unpaid"}})
if rec := as(h, ac, http.MethodGet, "/market", nil); rec.Code != http.StatusForbidden || !strings.Contains(rec.Body.String(), "suspended") {
t.Errorf("suspended: %d", rec.Code)
}
as(h, oc, http.MethodPost, "/platform/teams/"+id+"/restore", url.Values{})
if rec := as(h, ac, http.MethodGet, "/market", nil); rec.Code != http.StatusOK {
t.Errorf("restored: %d", rec.Code)
}
// View as: a reason, a while, read-only, and in the team's log.
if rec := as(h, oc, http.MethodPost, "/platform/teams/"+id+"/view-as", url.Values{"minutes": {"10"}}); !strings.Contains(rec.Header().Get("Location"), "view-needs-reason") {
t.Error("viewed without a reason")
}
as(h, oc, http.MethodPost, "/platform/teams/"+id+"/view-as", url.Values{"reason": {"ticket 42"}, "minutes": {"600"}})
body = as(h, oc, http.MethodGet, "/market", nil).Body.String()
if !strings.Contains(body, "Rival") || !strings.Contains(body, "Viewing this team as the platform") || !strings.Contains(body, "ticket 42") {
t.Errorf("view as: %s", body)
}
if rec := as(h, oc, http.MethodPost, "/market/save", url.Values{"name": {"X"}, "relationship": {"partner"}}); rec.Code != http.StatusForbidden {
t.Errorf("changed while viewing: %d", rec.Code)
}
if rec := as(h, oc, http.MethodGet, "/settings/profile", nil); rec.Code != http.StatusForbidden {
t.Errorf("Settings while viewing: %d", rec.Code)
}
ts, _ := s.forTeam(a.ID)
entries, _ := audit.List(ts.DB, audit.Filter{Action: "viewed-as"}, 5)
if len(entries) != 1 || !strings.Contains(entries[0].Detail, "ticket 42 (for 60 minutes)") || !strings.Contains(entries[0].UserName, "platform") {
t.Errorf("team's log: %+v", entries)
}
// It runs out.
sess, _, _ := users.Lookup(s.platform(), oc.Value, time.Now())
users.ViewAs(s.platform(), sess.ID, a.ID, time.Now().Add(-time.Minute), "ticket 42")
if rec := as(h, oc, http.MethodGet, "/market", nil); rec.Header().Get("Location") != "/platform" {
t.Errorf("after the time: %d", rec.Code)
}
}