Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestASessionUnusedForADayEnds(t *testing.T) {
|
|
db := capacityStore(t)
|
|
ctx := t.Context()
|
|
if err := db.CreateUser(ctx, User{ID: "u1", Email: "a@example.com", Name: "a",
|
|
PasswordHash: "x", State: UserActive, CreatedAt: time.Now()}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tok := range []string{"fresh", "idle"} {
|
|
if err := db.CreateSession(ctx, "u1", tok, tok+"-csrf", 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := db.db.ExecContext(ctx, `UPDATE sessions SET last_seen = ? WHERE token_hash = ?`,
|
|
millis(time.Now().Add(-SessionIdle-time.Minute)), HashKey("idle")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := db.SessionByToken(ctx, "fresh"); err != nil {
|
|
t.Errorf("a session in use: %v", err)
|
|
}
|
|
if _, _, err := db.SessionByToken(ctx, "idle"); !errors.Is(err, ErrNoSession) {
|
|
t.Errorf("a session idle past %s: %v", SessionIdle, err)
|
|
}
|
|
n, err := db.PurgeSessions(ctx, time.Now())
|
|
if err != nil || n != 1 {
|
|
t.Errorf("purged %d, %v; expected the idle one", n, err)
|
|
}
|
|
if _, _, err := db.SessionByToken(ctx, "fresh"); err != nil {
|
|
t.Errorf("the session in use was purged: %v", err)
|
|
}
|
|
}
|