Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAKeyIsMadeWithinTheShortestLifeItsScopesAllow(t *testing.T) {
|
|
db := capacityStore(t)
|
|
ctx := t.Context()
|
|
org, _ := db.CreateOrg(ctx, "acme")
|
|
team, _ := db.CreateTeam(ctx, org, "ml")
|
|
project, _ := db.CreateProject(ctx, team, "train")
|
|
key := func(id string, life time.Duration) Key {
|
|
k := Key{ID: id, Hash: []byte(id), Prefix: id, Name: id, ProjectID: project, CreatedAt: time.Now()}
|
|
if life > 0 {
|
|
k.ExpiresAt = k.CreatedAt.Add(life)
|
|
}
|
|
return k
|
|
}
|
|
if err := db.CreateKey(ctx, key("k0", 0)); err != nil {
|
|
t.Fatalf("nothing limits it: %v", err)
|
|
}
|
|
_ = db.SetMaxKeyTTL(ctx, ScopeGateway, "", 90*24*time.Hour)
|
|
_ = db.SetMaxKeyTTL(ctx, ScopeTeam, team, 30*24*time.Hour)
|
|
if max, by, _ := db.MaxKeyTTL(ctx, project); max != 30*24*time.Hour || by != "team:"+team {
|
|
t.Errorf("max %s by %s", max, by)
|
|
}
|
|
// One that asks for no expiry is given the longest allowed.
|
|
if err := db.CreateKey(ctx, key("k1", 0)); err != nil {
|
|
t.Errorf("one that asks for no expiry: %v", err)
|
|
}
|
|
if k, err := db.KeyByHash(ctx, []byte("k1")); err != nil || k.ExpiresAt.Sub(k.CreatedAt) < 29*24*time.Hour ||
|
|
k.ExpiresAt.Sub(k.CreatedAt) > 31*24*time.Hour {
|
|
t.Errorf("given %v %v", k.ExpiresAt.Sub(k.CreatedAt), err)
|
|
}
|
|
if err := db.CreateKey(ctx, key("k2", 60*24*time.Hour)); !errors.Is(err, ErrKeyTTL) {
|
|
t.Errorf("one past the team's: %v", err)
|
|
}
|
|
if err := db.CreateKey(ctx, key("k3", 7*24*time.Hour)); err != nil {
|
|
t.Errorf("one within: %v", err)
|
|
}
|
|
// A personal key has only the gateway's.
|
|
if max, _, _ := db.MaxKeyTTL(ctx, ""); max != 90*24*time.Hour {
|
|
t.Errorf("personal %s", max)
|
|
}
|
|
_ = db.SetMaxKeyTTL(ctx, ScopeTeam, team, 0)
|
|
if max, _, _ := db.MaxKeyTTL(ctx, project); max != 90*24*time.Hour {
|
|
t.Errorf("after the team's is taken away: %s", max)
|
|
}
|
|
}
|