a4fbd60441
The core of #4, and what #1 is for: terdut stops being one shared space. A team owns its incidents, alerts, schedule and integrations; a user sees exactly the teams they are in. Everything that existed moves into one Default team and every existing user becomes an owner of it, so the upgrade is a no-op for the people using it. Ingestion is the load-bearing half. An alert arrives on a team's integration key, and the key is both the credential and the routing: it says that the sender may post, and which team the alerts belong to. That also closes the unauthenticated webhook -- the old path stays for one release, deprecated and routed to the oldest team, so an upgrade does not stop delivering while somebody edits the Alertmanager config. Scoping is enforced in as few places as possible, because the failure mode is silent. serveAs loads the caller's memberships once; list queries carry `team_id = ANY(...)`; and every incident route goes through incidentIDParam, which now parses the id AND checks the team in the same call, so a new handler cannot remember the first half and forget the second. Anything in another team is 404, never 403: whether an incident exists is that team's business. Two bugs this found, both of which would have been silent: * upsertAlerts decided "is this a new occurrence" by looking up the fingerprint alone. Across teams that made team B's first alert look like a re-send of team A's, so it opened no incident at all. The lookups are keyed on (team_id, fingerprint) now, as the index is. * Every uniqueness rule was written for one tenant. Two teams watching two clusters legitimately see the same fingerprint, the same groupKey, and want somebody on call on the same day; all three constraints move to include team_id. Roles inside a team are separate from the system administrator flag: an owner configures the team, a member works its incidents, and an admin is NOT implicitly in every team -- administration is about accounts, not about reading other people's incidents. An admin can still repair a team whose owner has left, which is why requireTeamOwner lets them through. A shift can only be given to somebody in the team. Paging a person who cannot open the incident is worse than paging nobody. The UI is updated only as far as keeping it working: it loads the viewer's teams with the session and uses the first one, since nobody has a second yet. "On call now" shows every team the viewer is in, named only when there is more than one, so the common case reads exactly as before. The team switcher, badges and per-team settings pages are the next step. Breaking for API clients: the schedule endpoints moved under the team, and /api/schedule/current returns an array rather than an object or a 404. terdut-tui will need a version for that. Per-team dead-man configuration is deliberately not here. A heartbeat's incident already opens in the team whose key received it, which is the part that matters for isolation; moving the matchers out of env into per-team rows is a change to how deadman.go is configured rather than to who sees what. Claude-Session: https://claude.ai/code/session_01RHPj4ggeFdEjKKfm4SHbD7
105 lines
3.1 KiB
Go
105 lines
3.1 KiB
Go
package api_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.ryuvia.com/niklas/terdut-server/internal/db"
|
|
)
|
|
|
|
// Tests run against a real Postgres, because the server does. SQLite's
|
|
// ":memory:" gave every test a private database for free; Postgres has no
|
|
// equivalent, so isolation is bought with a schema per test.
|
|
//
|
|
// A schema rather than a database: CREATE DATABASE copies a template on disk and
|
|
// costs a hundred milliseconds or so each time, while CREATE SCHEMA plus the one
|
|
// baseline migration is a few, and the suite runs a few hundred of them. Each
|
|
// test's pool is pinned to its own schema through search_path, so two tests
|
|
// cannot see each other's rows even though they share a server.
|
|
//
|
|
// TERDUT_TEST_DSN must point at a database the test role may create schemas in:
|
|
//
|
|
// postgres://terdut:terdut@localhost:5432/terdut_test?sslmode=disable
|
|
//
|
|
// `make test-db` starts one locally; ci.yaml runs one as a service container.
|
|
// An unset DSN fails rather than skips, deliberately — a suite that quietly
|
|
// tests nothing is worse than one that does not run.
|
|
const testDSNEnv = "TERDUT_TEST_DSN"
|
|
|
|
// defaultTeam is the team migration 003 creates and the bootstrap user owns, as
|
|
// a path segment. Every test that does not say otherwise works inside it.
|
|
const defaultTeam = "1"
|
|
|
|
var schemaSeq int
|
|
|
|
// newTestDB returns a migrated database private to this test, and drops it
|
|
// afterwards.
|
|
func newTestDB(t *testing.T) *sql.DB {
|
|
t.Helper()
|
|
|
|
dsn := os.Getenv(testDSNEnv)
|
|
if dsn == "" {
|
|
t.Fatalf("%s is not set: these tests need Postgres.\n"+
|
|
"Run `make test-db` for a local one, then\n"+
|
|
" export %s=postgres://terdut:terdut@localhost:5432/terdut_test?sslmode=disable",
|
|
testDSNEnv, testDSNEnv)
|
|
}
|
|
|
|
schemaSeq++
|
|
schema := fmt.Sprintf("test_%d_%d", os.Getpid(), schemaSeq)
|
|
|
|
admin, err := sql.Open("pgx", dsn)
|
|
if err != nil {
|
|
t.Fatalf("connect to %s: %v", testDSNEnv, err)
|
|
}
|
|
defer admin.Close()
|
|
if _, err := admin.Exec("CREATE SCHEMA " + schema); err != nil {
|
|
t.Fatalf("create schema %s: %v", schema, err)
|
|
}
|
|
|
|
database, err := db.Open(withSearchPath(dsn, schema))
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
if err := db.Migrate(database); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
database.Close()
|
|
cleanup, err := sql.Open("pgx", dsn)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer cleanup.Close()
|
|
if _, err := cleanup.Exec("DROP SCHEMA " + schema + " CASCADE"); err != nil {
|
|
t.Logf("drop schema %s: %v", schema, err)
|
|
}
|
|
})
|
|
|
|
return database
|
|
}
|
|
|
|
// withSearchPath pins a DSN to one schema, so every connection the pool opens
|
|
// lands there and nothing has to qualify a table name.
|
|
//
|
|
// Handles both DSN spellings: a postgres:// URL, and libpq's keyword/value form.
|
|
func withSearchPath(dsn, schema string) string {
|
|
opt := "-csearch_path=" + schema
|
|
|
|
if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") {
|
|
u, err := url.Parse(dsn)
|
|
if err == nil {
|
|
q := u.Query()
|
|
q.Set("options", opt)
|
|
u.RawQuery = q.Encode()
|
|
return u.String()
|
|
}
|
|
}
|
|
return dsn + " options='" + opt + "'"
|
|
}
|