Refs #42 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func testSessions(t *testing.T) (*Sessions, *time.Time) {
|
|
t.Helper()
|
|
url := os.Getenv("UPTIME_TEST_REDIS_URL")
|
|
if url == "" {
|
|
t.Skip("UPTIME_TEST_REDIS_URL not set")
|
|
}
|
|
opts, err := redis.ParseURL(url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rdb := redis.NewClient(opts)
|
|
t.Cleanup(func() { _ = rdb.Close() })
|
|
now := time.Now()
|
|
s := NewSessions(rdb, time.Hour, 24*time.Hour)
|
|
s.now = func() time.Time { return now }
|
|
return s, &now
|
|
}
|
|
|
|
func TestSessionRoundTrip(t *testing.T) {
|
|
s, _ := testSessions(t)
|
|
ctx := context.Background()
|
|
user, org := uuid.New(), uuid.New()
|
|
|
|
created, err := s.Create(ctx, user, org, "de")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := s.Get(ctx, created.Token)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.UserID != user || got.OrgID != org || got.Locale != "de" || got.CSRF != created.CSRF {
|
|
t.Fatalf("mismatch: %+v vs %+v", got, created)
|
|
}
|
|
if !got.CheckCSRF(created.CSRF) || got.CheckCSRF("") || got.CheckCSRF("x") {
|
|
t.Fatal("csrf check wrong")
|
|
}
|
|
if err := s.Delete(ctx, got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Get(ctx, created.Token); !errors.Is(err, ErrNoSession) {
|
|
t.Fatalf("deleted session still valid: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionAbsoluteLifetime(t *testing.T) {
|
|
s, now := testSessions(t)
|
|
ctx := context.Background()
|
|
sess, err := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
*now = now.Add(25 * time.Hour)
|
|
if _, err := s.Get(ctx, sess.Token); !errors.Is(err, ErrNoSession) {
|
|
t.Fatalf("expired session accepted: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteAllForUser(t *testing.T) {
|
|
s, _ := testSessions(t)
|
|
ctx := context.Background()
|
|
user := uuid.New()
|
|
a, _ := s.Create(ctx, user, uuid.Nil, "")
|
|
b, _ := s.Create(ctx, user, uuid.Nil, "")
|
|
other, _ := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
|
|
|
if err := s.DeleteAllForUser(ctx, user); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tok := range []string{a.Token, b.Token} {
|
|
if _, err := s.Get(ctx, tok); !errors.Is(err, ErrNoSession) {
|
|
t.Fatal("session survived DeleteAllForUser")
|
|
}
|
|
}
|
|
if _, err := s.Get(ctx, other.Token); err != nil {
|
|
t.Fatalf("unrelated session removed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTokenNotStoredInPlain(t *testing.T) {
|
|
s, _ := testSessions(t)
|
|
ctx := context.Background()
|
|
sess, _ := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
|
if n, _ := s.rdb.Exists(ctx, "session:"+sess.Token).Result(); n != 0 {
|
|
t.Fatal("raw token used as redis key")
|
|
}
|
|
}
|