2016-02-10 00:40:28 +05:30
|
|
|
package manager
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2016-02-10 00:40:28 +05:30
|
|
|
"github.com/coreos/dex/db"
|
|
|
|
"github.com/coreos/dex/session"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/go-oidc/oidc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func staticGenerateCodeFunc(code string) GenerateCodeFunc {
|
|
|
|
return func() (string, error) {
|
|
|
|
return code, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:50:48 +05:30
|
|
|
func newManager() *SessionManager {
|
2016-02-10 00:40:28 +05:30
|
|
|
dbMap := db.NewMemDB()
|
|
|
|
return NewSessionManager(db.NewSessionRepo(dbMap), db.NewSessionKeyRepo(dbMap))
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func TestSessionManagerNewSession(t *testing.T) {
|
2016-02-17 07:50:48 +05:30
|
|
|
sm := newManager()
|
2015-08-18 05:57:27 +05:30
|
|
|
sm.GenerateCode = staticGenerateCodeFunc("boo")
|
2015-09-01 02:08:06 +05:30
|
|
|
got, err := sm.NewSession("bogus_idpc", "XXX", "bogus", url.URL{}, "", false, []string{"openid"})
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if got != "boo" {
|
|
|
|
t.Fatalf("Incorrect Session ID: want=%s got=%s", "boo", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionAttachRemoteIdentityTwice(t *testing.T) {
|
2016-02-17 07:50:48 +05:30
|
|
|
sm := newManager()
|
2015-09-01 02:08:06 +05:30
|
|
|
sessionID, err := sm.NewSession("bogus_idpc", "XXX", "bogus", url.URL{}, "", false, []string{"openid"})
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ident := oidc.Identity{ID: "YYY", Name: "elroy", Email: "elroy@example.com"}
|
|
|
|
if _, err := sm.AttachRemoteIdentity(sessionID, ident); err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sm.AttachRemoteIdentity(sessionID, ident); err == nil {
|
|
|
|
t.Fatalf("Expected non-nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionManagerExchangeKey(t *testing.T) {
|
2016-02-17 07:50:48 +05:30
|
|
|
sm := newManager()
|
2015-09-01 02:08:06 +05:30
|
|
|
sessionID, err := sm.NewSession("connector_id", "XXX", "bogus", url.URL{}, "", false, []string{"openid"})
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := sm.NewSessionKey(sessionID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := sm.ExchangeKey(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if got != sessionID {
|
|
|
|
t.Fatalf("Incorrect Session ID: want=%s got=%s", sessionID, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sm.ExchangeKey(key); err == nil {
|
|
|
|
t.Fatalf("Received nil response from attempt with spent Session key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionManagerGetSessionInStateNoExist(t *testing.T) {
|
2016-02-17 07:50:48 +05:30
|
|
|
sm := newManager()
|
2016-02-10 00:40:28 +05:30
|
|
|
ses, err := sm.getSessionInState("123", session.SessionStateNew)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected non-nil error")
|
|
|
|
}
|
|
|
|
if ses != nil {
|
|
|
|
t.Errorf("Expected nil Session")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionManagerGetSessionInStateWrongState(t *testing.T) {
|
2016-02-17 07:50:48 +05:30
|
|
|
sm := newManager()
|
2015-09-01 02:08:06 +05:30
|
|
|
sessionID, err := sm.NewSession("connector_id", "XXX", "bogus", url.URL{}, "", false, []string{"openid"})
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
2016-02-10 00:40:28 +05:30
|
|
|
ses, err := sm.getSessionInState(sessionID, session.SessionStateDead)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected non-nil error")
|
|
|
|
}
|
|
|
|
if ses != nil {
|
|
|
|
t.Errorf("Expected nil Session")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionManagerKill(t *testing.T) {
|
2016-02-17 07:50:48 +05:30
|
|
|
sm := newManager()
|
2015-09-01 02:08:06 +05:30
|
|
|
sessionID, err := sm.NewSession("connector_id", "XXX", "bogus", url.URL{}, "", false, []string{"openid"})
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ident := oidc.Identity{ID: "YYY", Name: "elroy", Email: "elroy@example.com"}
|
|
|
|
if _, err := sm.AttachRemoteIdentity(sessionID, ident); err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ses, err := sm.Kill(sessionID)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if ses == nil {
|
|
|
|
t.Fatalf("Expected non-nil Session")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ses.ClientState != "bogus" {
|
|
|
|
t.Errorf("Unexpected Session: %#v", ses)
|
|
|
|
}
|
|
|
|
}
|