A person was in exactly one project, chosen at approval, and could not use the gateway before that. People belong to several teams and to none, so: - memberships: a person is a member of any number of organizations, teams and projects, or of none; a member of a team or organization may use every project in it. users join/leave, and the people page adds and takes out from either side. Approval no longer needs a project. - personal keys: a key may have an owner and no project. /profile asks what a new key spends against; keys create --for does the same. auth.personal sets default limits for personal use or turns it off. - limits for people: --scope user holds a person across every key, project and the chat page; --per-member on an org, team or project gives each member their own share there. Resolution folds the tree, each per-member limit for the key's owner, and the owner's own, each in its own pool; the quotas page shows and sets both. - the chat page asks what to spend against and checks it per message. - usage records who spent it, so a person's budget survives a restart, personal use and chat included; restore no longer drops rows with no project. Leaving a project revokes the keys made there unless another membership still reaches it. A personal key keeps no threads or notes, which are per project, and is refused rather than dropped into the anonymous pool. Migration 0010 turns each person's project into a membership, adds usage.user_id and backfills it, and makes api_keys.project_id nullable. SQLite cannot drop NOT NULL, so the table is rebuilt; that needs foreign keys off, which only works outside a transaction, so migrations marked for it run on one connection with foreign_key_check before commit. Tested on a populated SQLite store with claims, and on a copy of the cluster's PostgreSQL store: 5 people became members, 17 keys and 3 claims intact, 145 of 577 usage rows and all 69 chat rows attributed. The PostgreSQL store tests, including new membership cases, pass on 16. users move is gone; join and leave replace it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
88 lines
3.3 KiB
Go
88 lines
3.3 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// The membership queries on PostgreSQL: the unions that decide which projects
|
|
// a person may use, the path each membership is named by, the revocation that
|
|
// follows leaving, a personal key's resolution, and spend with no project.
|
|
// Each passed on SQLite first; these are what would catch one that does not
|
|
// on the database the cluster runs.
|
|
func TestPostgresMemberships(t *testing.T) {
|
|
f := newFixtureOn(t, openPG(t))
|
|
ctx := t.Context()
|
|
|
|
if err := f.s.AddMembership(ctx, f.user, ScopeTeam, f.ml, RoleMember, "t", time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.s.AddMembership(ctx, f.user, ScopeTeam, f.ml, RoleMember, "t", time.Now()); err != nil {
|
|
t.Errorf("adding again should change nothing: %v", err)
|
|
}
|
|
for proj, want := range map[string]bool{f.gateway: true, f.research: true, f.search: false} {
|
|
if ok, err := f.s.CanUseProject(ctx, f.user, proj); err != nil || ok != want {
|
|
t.Errorf("CanUseProject %s = %v, %v; want %v", proj, ok, err, want)
|
|
}
|
|
}
|
|
if ps, err := f.s.ProjectsFor(ctx, f.user); err != nil || len(ps) != 2 {
|
|
t.Errorf("ProjectsFor = %+v, %v", ps, err)
|
|
}
|
|
ms, err := f.s.MembershipsOf(ctx, f.user)
|
|
if err != nil || len(ms) != 2 || ms[0].Path != "Acme / platform / gateway" || ms[1].Path != "Scania / ml" {
|
|
t.Errorf("MembershipsOf = %+v, %v", ms, err)
|
|
}
|
|
if all, err := f.s.AllMemberships(ctx); err != nil || len(all) != 2 {
|
|
t.Errorf("AllMemberships = %+v, %v", all, err)
|
|
}
|
|
|
|
// Leaving gateway revokes the key made there, and leaves a personal one.
|
|
_, personal, _ := NewKey("key-p", "personal", "")
|
|
if err := f.s.CreateUserKey(ctx, f.user, personal); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := f.s.RemoveMembership(ctx, f.user, ScopeProject, f.gateway, time.Now()); err != nil || n != 1 {
|
|
t.Errorf("RemoveMembership revoked %d, %v; want 1", n, err)
|
|
}
|
|
|
|
// A personal key resolves, under the personal defaults.
|
|
f.s.SetPersonalDefaults(&Limits{RPM: i(9)})
|
|
defer f.s.SetPersonalDefaults(nil)
|
|
e, err := f.s.Resolve(ctx, personal.ID)
|
|
if err != nil || e.UserID != f.user || e.ProjectID != "" || e.RPM == nil || *e.RPM != 9 {
|
|
t.Errorf("personal key resolved to %+v, %v", e, err)
|
|
}
|
|
if e, err := f.s.ResolveFor(ctx, "session:"+f.user, f.user, f.research); err != nil || e.OrgID != f.scania {
|
|
t.Errorf("ResolveFor in a project = %+v, %v", e, err)
|
|
}
|
|
|
|
// Spend with no project is kept, and attributed.
|
|
now := time.Now()
|
|
if err := f.s.WriteUsage(ctx, []Usage{
|
|
{At: now, KeyID: personal.ID, UserID: f.user, CostMicros: 5, Model: "m", ServedModel: "m"},
|
|
{At: now, KeyID: "session:" + f.user, UserID: f.user, ProjectID: f.research, CostMicros: 7, Model: "m", ServedModel: "m"},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rows, err := f.s.SpendSinceByScope(ctx, now.Add(-time.Minute))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var total int64
|
|
for _, r := range rows {
|
|
if r.UserID == f.user {
|
|
total += r.Micros
|
|
}
|
|
}
|
|
if total != 12 {
|
|
t.Errorf("restored %d for the person, want 12: %+v", total, rows)
|
|
}
|
|
byUser, err := f.s.UsageByUser(ctx, now.Add(-time.Minute))
|
|
if err != nil || len(byUser) != 1 || byUser[0].UserID != f.user || byUser[0].Requests != 2 {
|
|
t.Errorf("UsageByUser = %+v, %v", byUser, err)
|
|
}
|
|
if keys, err := f.s.AllKeys(ctx); err != nil || len(keys) != 3 {
|
|
t.Errorf("AllKeys = %d rows, %v", len(keys), err)
|
|
}
|
|
}
|