60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
|
|
package sessions
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"basicstack.de/proofedmesh/services/approval-service/internal/store"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestStartSession_UsesTTL(t *testing.T) {
|
||
|
|
st := store.NewMemory()
|
||
|
|
m := New(st)
|
||
|
|
m.SessionTTL = 5 * time.Minute
|
||
|
|
m.Now = func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }
|
||
|
|
s, err := m.StartSession(context.Background(), "alice")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("StartSession: %v", err)
|
||
|
|
}
|
||
|
|
want := time.Date(2026, 1, 1, 12, 5, 0, 0, time.UTC)
|
||
|
|
if !s.ActiveUntil.Equal(want) {
|
||
|
|
t.Errorf("ActiveUntil = %v want %v", s.ActiveUntil, want)
|
||
|
|
}
|
||
|
|
if s.UserSub != "alice" {
|
||
|
|
t.Errorf("UserSub = %q", s.UserSub)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetActiveSession_WrongUser(t *testing.T) {
|
||
|
|
st := store.NewMemory()
|
||
|
|
m := New(st)
|
||
|
|
m.Now = func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) }
|
||
|
|
s, err := m.StartSession(context.Background(), "alice")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("StartSession: %v", err)
|
||
|
|
}
|
||
|
|
_, err = m.GetActiveSession(context.Background(), s.ID, "bob")
|
||
|
|
if !errors.Is(err, store.ErrNotFound) {
|
||
|
|
t.Fatalf("want ErrNotFound, got %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestGetActiveSession_Expired(t *testing.T) {
|
||
|
|
st := store.NewMemory()
|
||
|
|
m := New(st)
|
||
|
|
m.SessionTTL = time.Minute
|
||
|
|
t0 := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
|
||
|
|
m.Now = func() time.Time { return t0 }
|
||
|
|
s, err := m.StartSession(context.Background(), "alice")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
m.Now = func() time.Time { return t0.Add(2 * time.Minute) }
|
||
|
|
_, err = m.GetActiveSession(context.Background(), s.ID, "alice")
|
||
|
|
if !errors.Is(err, ErrSessionExpired) {
|
||
|
|
t.Fatalf("want ErrSessionExpired, got %v", err)
|
||
|
|
}
|
||
|
|
}
|