2015-08-18 05:57:27 +05:30
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/db"
|
|
|
|
"github.com/coreos/dex/session"
|
|
|
|
)
|
|
|
|
|
2016-02-09 05:31:44 +05:30
|
|
|
func newSessionRepo(t *testing.T) (session.SessionRepo, clockwork.FakeClock) {
|
|
|
|
clock := clockwork.NewFakeClock()
|
|
|
|
if os.Getenv("DEX_TEST_DSN") == "" {
|
2016-02-10 00:40:28 +05:30
|
|
|
return db.NewSessionRepoWithClock(db.NewMemDB(), clock), clock
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2016-02-09 05:31:44 +05:30
|
|
|
dbMap := connect(t)
|
|
|
|
return db.NewSessionRepoWithClock(dbMap, clock), clock
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
2016-02-09 05:31:44 +05:30
|
|
|
func newSessionKeyRepo(t *testing.T) (session.SessionKeyRepo, clockwork.FakeClock) {
|
|
|
|
clock := clockwork.NewFakeClock()
|
|
|
|
if os.Getenv("DEX_TEST_DSN") == "" {
|
2016-02-10 00:40:28 +05:30
|
|
|
return db.NewSessionKeyRepoWithClock(db.NewMemDB(), clock), clock
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2016-02-09 05:31:44 +05:30
|
|
|
dbMap := connect(t)
|
|
|
|
return db.NewSessionKeyRepoWithClock(dbMap, clock), clock
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionKeyRepoPopNoExist(t *testing.T) {
|
2016-02-09 05:31:44 +05:30
|
|
|
r, _ := newSessionKeyRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
_, err := r.Pop("123")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error, got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionKeyRepoPushPop(t *testing.T) {
|
2016-02-09 05:31:44 +05:30
|
|
|
r, _ := newSessionKeyRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
key := "123"
|
|
|
|
sessionID := "456"
|
|
|
|
|
|
|
|
r.Push(session.SessionKey{Key: key, SessionID: sessionID}, time.Second)
|
|
|
|
|
|
|
|
got, err := r.Pop(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected nil error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got != sessionID {
|
|
|
|
t.Fatalf("Incorrect sessionID: want=%s got=%s", sessionID, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionKeyRepoExpired(t *testing.T) {
|
2016-02-09 05:31:44 +05:30
|
|
|
r, fc := newSessionKeyRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
key := "123"
|
|
|
|
sessionID := "456"
|
|
|
|
|
|
|
|
r.Push(session.SessionKey{Key: key, SessionID: sessionID}, time.Second)
|
|
|
|
|
|
|
|
fc.Advance(2 * time.Second)
|
|
|
|
|
|
|
|
_, err := r.Pop(key)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error, got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionRepoGetNoExist(t *testing.T) {
|
2016-02-09 05:31:44 +05:30
|
|
|
r, _ := newSessionRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
ses, err := r.Get("123")
|
|
|
|
if ses != nil {
|
|
|
|
t.Fatalf("Expected nil, got %#v", ses)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected non-nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionRepoCreateGet(t *testing.T) {
|
|
|
|
tests := []session.Session{
|
|
|
|
session.Session{
|
|
|
|
ID: "123",
|
|
|
|
ClientState: "blargh",
|
|
|
|
ExpiresAt: time.Unix(123, 0).UTC(),
|
|
|
|
},
|
|
|
|
session.Session{
|
|
|
|
ID: "456",
|
|
|
|
ClientState: "argh",
|
|
|
|
ExpiresAt: time.Unix(456, 0).UTC(),
|
|
|
|
Register: true,
|
|
|
|
},
|
|
|
|
session.Session{
|
|
|
|
ID: "789",
|
|
|
|
ClientState: "blargh",
|
|
|
|
ExpiresAt: time.Unix(789, 0).UTC(),
|
|
|
|
Nonce: "oncenay",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2016-02-09 05:31:44 +05:30
|
|
|
r, _ := newSessionRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
r.Create(tt)
|
|
|
|
|
|
|
|
ses, _ := r.Get(tt.ID)
|
|
|
|
if ses == nil {
|
|
|
|
t.Fatalf("case %d: Expected non-nil Session", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := pretty.Compare(tt, ses); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionRepoCreateUpdate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
initial session.Session
|
|
|
|
update session.Session
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
initial: session.Session{
|
|
|
|
ID: "123",
|
|
|
|
ClientState: "blargh",
|
|
|
|
ExpiresAt: time.Unix(123, 0).UTC(),
|
|
|
|
},
|
|
|
|
update: session.Session{
|
|
|
|
ID: "123",
|
|
|
|
ClientState: "boom",
|
|
|
|
ExpiresAt: time.Unix(123, 0).UTC(),
|
|
|
|
Register: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2016-02-09 05:31:44 +05:30
|
|
|
r, _ := newSessionRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
r.Create(tt.initial)
|
|
|
|
|
|
|
|
ses, _ := r.Get(tt.initial.ID)
|
|
|
|
if diff := pretty.Compare(tt.initial, ses); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Update(tt.update)
|
|
|
|
ses, _ = r.Get(tt.initial.ID)
|
|
|
|
if ses == nil {
|
|
|
|
t.Fatalf("Expected non-nil Session")
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(tt.update, ses); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionRepoUpdateNoExist(t *testing.T) {
|
2016-02-09 05:31:44 +05:30
|
|
|
r, _ := newSessionRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
err := r.Update(session.Session{ID: "123", ClientState: "boom"})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected non-nil error")
|
|
|
|
}
|
|
|
|
}
|