package users import ( "errors" "strings" "testing" "time" ) func TestAgentKeys(t *testing.T) { d := openDB(t) now := time.Date(2026, 11, 26, 10, 0, 0, 0, time.UTC) a, err := CreateAgent(d, "Morning writer", "Drafts posts from What to write next.", now) if err != nil { t.Fatal(err) } if a.Kind != Agent || a.Status != Active || !strings.HasSuffix(a.Email, "@agents.invalid") || !strings.HasPrefix(a.Email, "morning-writer-") { t.Errorf("agent %+v", a) } if _, err := CreateAgent(d, " ", "", now); err == nil { t.Error("no name accepted") } // Agents never sign in with a password. if CheckPassword(a, "") { t.Error("password") } key, k, err := NewKey(d, a.ID, now.Add(30*24*time.Hour), now) if err != nil { t.Fatal(err) } if !strings.HasPrefix(key, "sm_") || len(key) != 43 || k.Prefix != key[:10] { t.Errorf("key %q, prefix %q", key, k.Prefix) } u, got, err := Authenticate(d, key, "203.0.113.7", now) if err != nil || u.ID != a.ID || got.ID != k.ID { t.Fatalf("auth: %v", err) } keys, _ := Keys(d, a.ID) if keys[0].UsedFrom != "203.0.113.7" || !keys[0].Used.Equal(now) { t.Errorf("use not noted: %+v", keys[0]) } for _, bad := range []string{"", "sm_nope", key + "x", strings.ToUpper(key)} { if _, _, err := Authenticate(d, bad, "", now); !errors.Is(err, ErrBadKey) { t.Errorf("%q: %v", bad, err) } } // Expiry. if _, _, err := Authenticate(d, key, "", now.Add(31*24*time.Hour)); !errors.Is(err, ErrBadKey) { t.Error("expired key works") } // Rotation: the old key works for a day; the new one keeps the expiry. key2, k2, err := Rotate(d, a.ID, now) if err != nil { t.Fatal(err) } if !k2.Expires.Equal(now.Add(30 * 24 * time.Hour)) { t.Errorf("new key's expiry %v", k2.Expires) } if _, _, err := Authenticate(d, key, "", now.Add(23*time.Hour)); err != nil { t.Error("old key stopped within the day") } if _, _, err := Authenticate(d, key, "", now.Add(RotationGrace)); !errors.Is(err, ErrBadKey) { t.Error("old key works after a day") } if _, _, err := Authenticate(d, key2, "", now.Add(RotationGrace)); err != nil { t.Error("new key doesn't work") } // Revoking stops it at once; turning the agent off stops every key. Revoke(d, a.ID, k2.ID, now) if _, _, err := Authenticate(d, key2, "", now); !errors.Is(err, ErrBadKey) { t.Error("revoked key works") } key3, _, _ := NewKey(d, a.ID, time.Time{}, now) SetStatus(d, a.ID, Disabled) if _, _, err := Authenticate(d, key3, "", now); !errors.Is(err, ErrBadKey) { t.Error("a turned-off agent's key works") } SetStatus(d, a.ID, Active) RevokeAll(d, a.ID, now) if _, _, err := Authenticate(d, key3, "", now); !errors.Is(err, ErrBadKey) { t.Error("revoke all") } // A person's "key" never authenticates, even if one were stored. p := &User{Email: "pat@example.com", Status: Active} Create(d, p, now) AddKey(d, p.ID, "sm_person_key_0123456789012345678901234", time.Time{}, now) if _, _, err := Authenticate(d, "sm_person_key_0123456789012345678901234", "", now); !errors.Is(err, ErrBadKey) { t.Error("a person's key works") } // A legacy token (64 hex digits) is a key too. legacy := strings.Repeat("ab", 32) AddKey(d, a.ID, legacy, time.Time{}, now) if _, _, err := Authenticate(d, legacy, "", now); err != nil { t.Errorf("legacy token: %v", err) } if err := Delete(d, a.ID); err != nil { t.Fatal(err) } if ks, _ := Keys(d, a.ID); len(ks) != 0 { t.Error("keys outlived the agent") } } func TestAgentLimitsAndCalls(t *testing.T) { d := openDB(t) now := time.Now() a, _ := CreateAgent(d, "Bot", "", now) l, _ := LimitsOf(d, a.ID) if l.PerMinute != DefaultPerMinute || l.OutPerDay != DefaultOutPerDay || !l.Paused.IsZero() { t.Errorf("defaults %+v", l) } if err := SetLimits(d, a.ID, 0, 5); err == nil { t.Error("zero accepted") } SetLimits(d, a.ID, 10, 5) Pause(d, a.ID, true, 7, now) l, _ = LimitsOf(d, a.ID) if l.PerMinute != 10 || l.OutPerDay != 5 || l.Paused.IsZero() || l.PausedBy != 7 { t.Errorf("set %+v", l) } Pause(d, a.ID, false, 7, now) if l, _ = LimitsOf(d, a.ID); !l.Paused.IsZero() || l.PerMinute != 10 { t.Errorf("resumed %+v", l) } RecordCall(d, Call{Agent: a.ID, At: now.Add(-2 * time.Minute), Tool: "x", Outcome: CallOK}) RecordCall(d, Call{Agent: a.ID, At: now, Tool: "y", Outcome: CallError, Detail: strings.Repeat("e", 400), Took: 1500 * time.Millisecond}) RecordCall(d, Call{Agent: a.ID, At: now, Tool: "z", Outcome: CallRefused}) if n, _ := CallsSince(d, a.ID, now.Add(-time.Minute)); n != 1 { t.Errorf("calls in a minute: %d", n) } calls, _ := RecentCalls(d, a.ID, 10) if len(calls) != 3 || calls[1].Tool != "y" || calls[1].Took != 1500*time.Millisecond || len([]rune(calls[1].Detail)) != 300 { t.Errorf("recent %+v", calls) } PruneCalls(d, now.Add(31*24*time.Hour)) if calls, _ = RecentCalls(d, a.ID, 10); len(calls) != 0 { t.Error("not pruned") } }