Phase 13, part 1: a Postgres schema per team
On the multi-tenant edition each team's data is in its own schema; the tables every team shares (people, sessions, memberships, keys, locks, and a new teams table) stay in the main one. A team's connection pool searches its own schema first, so the app's SQL runs unchanged and can't reach another team's tables. Each request is served by a copy of the Server for the viewer's team, with that team's database, secrets and caches; agents work on their own team's. The local and server editions are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/config"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
// newMultiEdition is a test server run as the multi-tenant edition: each
|
||||
// team in its own schema. Postgres only.
|
||||
func newMultiEdition(t *testing.T) (*Server, http.Handler) {
|
||||
t.Helper()
|
||||
if !db.OnPostgres() {
|
||||
t.Skip("the multi-tenant edition needs Postgres")
|
||||
}
|
||||
s, h := newServerEdition(t)
|
||||
s.Config = &config.Config{Edition: config.Multi}
|
||||
teams, err := s.DB.Teams()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(teams.Close)
|
||||
s.Teams, s.Platform = teams, s.DB
|
||||
return s, h
|
||||
}
|
||||
|
||||
// newTeam makes a team with its schema and an owner, signed in.
|
||||
func newTeam(t *testing.T, s *Server, name, owner string) (*users.TeamInfo, *users.User, *http.Cookie) {
|
||||
t.Helper()
|
||||
team, err := users.CreateTeam(s.platform(), name, strings.ToLower(name)+".example", time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Teams.Create(team.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u := &users.User{Email: owner, Name: name + " owner", 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")
|
||||
if err := users.Join(s.platform(), team.ID, u.ID, users.Owner, nil, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, _, err := users.StartSessionIn(s.platform(), u.ID, team.ID, "", "", time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return team, u, &http.Cookie{Name: sessionCookie, Value: token}
|
||||
}
|
||||
|
||||
func TestTeamsDontSeeEachOther(t *testing.T) {
|
||||
s, h := newMultiEdition(t)
|
||||
a, _, ac := newTeam(t, s, "Acme", "olu@acme.example")
|
||||
b, _, bc := newTeam(t, s, "Bolt", "bea@bolt.example")
|
||||
|
||||
// Each saves its profile and a Market entry, through the pages.
|
||||
for _, c := range []struct {
|
||||
cookie *http.Cookie
|
||||
name string
|
||||
}{{ac, "Acme"}, {bc, "Bolt"}} {
|
||||
if rec := as(h, c.cookie, http.MethodPost, "/settings/profile", url.Values{"name": {c.name + " Inc"}, "description": {"We are " + c.name}}); rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("%s profile: %d %s", c.name, rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := as(h, c.cookie, http.MethodPost, "/market/save", url.Values{"name": {c.name + "'s rival"}, "relationship": {"competitor"}, "description": {"x"}}); rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("%s market: %d %s", c.name, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
cookie *http.Cookie
|
||||
mine, other string
|
||||
}{{ac, "Acme", "Bolt"}, {bc, "Bolt", "Acme"}} {
|
||||
for _, p := range []string{"/settings/profile", "/market", "/search?q=rival", "/"} {
|
||||
body := as(h, c.cookie, http.MethodGet, p, nil).Body.String()
|
||||
if strings.Contains(body, c.other) {
|
||||
t.Errorf("%s sees %s on %s", c.mine, c.other, p)
|
||||
}
|
||||
}
|
||||
if body := as(h, c.cookie, http.MethodGet, "/market", nil).Body.String(); !strings.Contains(body, c.mine+"'s rival") {
|
||||
t.Errorf("%s doesn't see its own entry", c.mine)
|
||||
}
|
||||
}
|
||||
// In the database: each team's rows in its own schema, none in the main.
|
||||
if list, _ := inventory.List(s.platform(), ""); len(list) != 0 {
|
||||
t.Errorf("rows in the main schema: %v", list)
|
||||
}
|
||||
ta, _ := s.forTeam(a.ID)
|
||||
tb, _ := s.forTeam(b.ID)
|
||||
if la, _ := inventory.List(ta.DB, ""); len(la) != 1 || la[0].Name != "Acme's rival" {
|
||||
t.Errorf("team a: %v", la)
|
||||
}
|
||||
if lb, _ := inventory.List(tb.DB, ""); len(lb) != 1 || lb[0].Name != "Bolt's rival" {
|
||||
t.Errorf("team b: %v", lb)
|
||||
}
|
||||
|
||||
// Agents: each team's agent sees its own team's data only.
|
||||
_, key := newAgentIn(t, ta, "A's agent", map[string]users.Access{"market": users.View})
|
||||
text, _ := mcpCall(t, h, key, "market_list", map[string]any{})
|
||||
if !strings.Contains(text, "Acme's rival") || strings.Contains(text, "Bolt") {
|
||||
t.Errorf("agent's market: %s", text)
|
||||
}
|
||||
text, _ = mcpCall(t, h, key, "profile_get", map[string]any{})
|
||||
if !strings.Contains(text, "Acme Inc") {
|
||||
t.Errorf("agent's profile: %s", text)
|
||||
}
|
||||
// Secrets too: each team's own.
|
||||
as(h, ac, http.MethodPost, "/settings/sources", url.Values{"hunter.api_key": {"acme-key"}})
|
||||
if ta.Secrets.Get("hunter.api_key") != "acme-key" || tb.Secrets.Has("hunter.api_key") {
|
||||
t.Error("secrets shared between teams")
|
||||
}
|
||||
}
|
||||
|
||||
// newAgentIn makes an agent in a team.
|
||||
func newAgentIn(t *testing.T, ts *Server, name string, scopes map[string]users.Access) (*users.User, string) {
|
||||
t.Helper()
|
||||
u, err := users.CreateAgent(ts.platform(), name, "", time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := users.Join(ts.platform(), ts.team(), u.ID, users.Member, scopes, time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key, _, err := users.NewKey(ts.platform(), u.ID, time.Time{}, time.Now())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return u, key
|
||||
}
|
||||
Reference in New Issue
Block a user