A team's connections take a Postgres role that owns its schema and may use the shared tables, nothing else: even SQL naming another team's schema is refused. The second guard behind the search_path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package db
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTeamsSchemas(t *testing.T) {
|
|
if !OnPostgres() {
|
|
t.Skip("teams are Postgres's")
|
|
}
|
|
d, err := Open(filepath.Join(t.TempDir(), "app.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.Close()
|
|
teams, err := d.Teams()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer teams.Close()
|
|
if _, err := teams.Open(1); err == nil {
|
|
t.Error("opened a team that wasn't created")
|
|
}
|
|
for _, id := range []int64{1, 2} {
|
|
if err := teams.Create(id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
a, err := teams.Open(1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := teams.Open(2)
|
|
if a2, _ := teams.Open(1); a2 != a {
|
|
t.Error("a second pool")
|
|
}
|
|
if v, err := a.SchemaVersion(); err != nil || v != Schema() {
|
|
t.Errorf("team schema at %d: %v", v, err)
|
|
}
|
|
// Each team's own tables.
|
|
if err := a.PutJSON("company", "Acme"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := b.PutJSON("company", "Bolt"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got string
|
|
a.GetJSON("company", &got)
|
|
if got != "Acme" {
|
|
t.Errorf("team 1 sees %q", got)
|
|
}
|
|
b.GetJSON("company", &got)
|
|
if got != "Bolt" {
|
|
t.Errorf("team 2 sees %q", got)
|
|
}
|
|
var n int
|
|
d.QueryRow(`SELECT count(*) FROM settings WHERE key = 'company'`).Scan(&n)
|
|
if n != 0 {
|
|
t.Error("a team's setting in the main schema")
|
|
}
|
|
// The shared tables: only in the main schema, reached from a team's.
|
|
for _, tbl := range PlatformTables {
|
|
var inTeam bool
|
|
d.QueryRow(`SELECT to_regclass($1) IS NOT NULL`, teams.Schema(1)+"."+tbl).Scan(&inTeam)
|
|
if inTeam {
|
|
t.Errorf("%s is in the team's schema", tbl)
|
|
}
|
|
}
|
|
if _, err := d.Exec(`INSERT INTO teams (name, created_at) VALUES ('Acme', 'now')`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.QueryRow(`SELECT count(*) FROM teams`).Scan(&n); err != nil || n != 1 {
|
|
t.Errorf("the shared teams table from a team: %d %v", n, err)
|
|
}
|
|
// The second guard: a team's connections can't reach another team's
|
|
// schema, nor the main schema's own copies of the team tables, even
|
|
// by name.
|
|
for _, q := range []string{
|
|
`SELECT count(*) FROM ` + teams.Schema(2) + `.settings`,
|
|
`SELECT count(*) FROM ` + d.mainSchema(t) + `.settings`,
|
|
`INSERT INTO ` + teams.Schema(2) + `.settings (key, value) VALUES ('x', '"y"')`,
|
|
} {
|
|
if _, err := a.Exec(q); err == nil || !strings.Contains(err.Error(), "permission denied") {
|
|
t.Errorf("%s: %v", q, err)
|
|
}
|
|
}
|
|
// Creating again only migrates; dropping removes it all.
|
|
if err := teams.Create(1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := teams.Drop(2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := teams.Open(2); err == nil {
|
|
t.Error("a dropped team opens")
|
|
}
|
|
}
|
|
|
|
// mainSchema is the database's main schema (a test's own).
|
|
func (d *DB) mainSchema(t *testing.T) string {
|
|
var s string
|
|
if err := d.SQL.QueryRow(`SELECT current_schema()`).Scan(&s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s
|
|
}
|