spark-server/internal/module/auth/logic/static_test.go

56 lines
1.5 KiB
Go

package logic
import (
"context"
"testing"
"time"
)
func TestStaticUserAuthenticateEmail(t *testing.T) {
s := StaticUser{Email: "ops@spark.com", Code: "42", UserID: 99, SparkID: "sp99"}
ctx := context.Background()
u, err := s.AuthenticateEmail(ctx, "ops@spark.com", "42", nil)
if err != nil || u.ID != 99 || u.SparkID != "sp99" {
t.Fatalf("ok %+v %v", u, err)
}
if _, err := s.AuthenticateEmail(ctx, "other@spark.com", "42", nil); err == nil {
t.Fatal("wrong email")
}
if _, err := s.AuthenticateEmail(ctx, "ops@spark.com", "bad", nil); err == nil {
t.Fatal("wrong code")
}
}
func TestNewRedisPairsDefaultPrefix(t *testing.T) {
p := NewRedisPairsMode(nil, "", PairKeyTriple)
if p.prefix != "refresh:" {
t.Fatalf("prefix %q", p.prefix)
}
}
func TestMemoryPairsPutSkipsInvalid(t *testing.T) {
p := NewMemoryPairs()
ctx := t.Context()
if err := p.Put(ctx, 0, 1, 1, TokenPair{Access: "a"}, time.Minute); err != nil {
t.Fatal(err)
}
if err := p.Put(ctx, 1, 0, 1, TokenPair{Access: "a"}, time.Minute); err != nil {
t.Fatal(err)
}
got, err := p.Get(ctx, 1, 1, 1)
if err != nil || got.Access != "" {
t.Fatalf("empty %+v %v", got, err)
}
}
func TestMemoryPairsListByUserDropsExpired(t *testing.T) {
p := NewMemoryPairs()
ctx := t.Context()
_ = p.Put(ctx, 3, 4, 1, TokenPair{Access: "x"}, time.Millisecond)
time.Sleep(3 * time.Millisecond)
rows, err := p.ListByUser(ctx, 3)
if err != nil || len(rows) != 0 {
t.Fatalf("expired listed %+v %v", rows, err)
}
}