forked from mystiq/dex
server: remove boilerplate setup code part deux
Use the test fixture setup stuff in testutil instead.
This commit is contained in:
parent
ad1d5ab253
commit
a33d61c8e2
3 changed files with 39 additions and 75 deletions
|
@ -188,7 +188,7 @@ func TestList(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
// empty repo
|
// empty repo
|
||||||
{
|
{
|
||||||
cs: nil,
|
cs: []client.Client{},
|
||||||
want: nil,
|
want: nil,
|
||||||
},
|
},
|
||||||
// single client
|
// single client
|
||||||
|
@ -244,20 +244,14 @@ func TestList(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
dbm := db.NewMemDB()
|
f, err := makeTestFixturesWithOptions(testFixtureOptions{
|
||||||
clientIDGenerator := func(hostport string) (string, error) {
|
clients: tt.cs,
|
||||||
return hostport, nil
|
})
|
||||||
}
|
|
||||||
secGen := func() ([]byte, error) {
|
|
||||||
return []byte("secret"), nil
|
|
||||||
}
|
|
||||||
clientRepo := db.NewClientRepo(dbm)
|
|
||||||
clientManager, err := manager.NewClientManagerFromClients(clientRepo, db.TransactionFactory(dbm), tt.cs, manager.ManagerOptions{ClientIDGenerator: clientIDGenerator, SecretGenerator: secGen})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create client identity manager: %v", err)
|
t.Fatalf("error making test fixtures: %v", err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
res := &clientResource{manager: clientManager}
|
|
||||||
|
res := &clientResource{manager: f.clientManager}
|
||||||
|
|
||||||
r, err := http.NewRequest("GET", "http://example.com/clients", nil)
|
r, err := http.NewRequest("GET", "http://example.com/clients", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -17,10 +17,7 @@ import (
|
||||||
"github.com/jonboulle/clockwork"
|
"github.com/jonboulle/clockwork"
|
||||||
|
|
||||||
"github.com/coreos/dex/client"
|
"github.com/coreos/dex/client"
|
||||||
clientmanager "github.com/coreos/dex/client/manager"
|
|
||||||
"github.com/coreos/dex/connector"
|
"github.com/coreos/dex/connector"
|
||||||
"github.com/coreos/dex/db"
|
|
||||||
"github.com/coreos/dex/session/manager"
|
|
||||||
"github.com/coreos/go-oidc/jose"
|
"github.com/coreos/go-oidc/jose"
|
||||||
"github.com/coreos/go-oidc/oauth2"
|
"github.com/coreos/go-oidc/oauth2"
|
||||||
"github.com/coreos/go-oidc/oidc"
|
"github.com/coreos/go-oidc/oidc"
|
||||||
|
@ -76,38 +73,6 @@ func TestHandleAuthFuncResponsesSingleRedirectURL(t *testing.T) {
|
||||||
idpcs := []connector.Connector{
|
idpcs := []connector.Connector{
|
||||||
&fakeConnector{loginURL: "http://fake.example.com"},
|
&fakeConnector{loginURL: "http://fake.example.com"},
|
||||||
}
|
}
|
||||||
dbm := db.NewMemDB()
|
|
||||||
clients := []client.Client{
|
|
||||||
client.Client{
|
|
||||||
Credentials: oidc.ClientCredentials{
|
|
||||||
ID: "client.example.com",
|
|
||||||
Secret: base64.URLEncoding.EncodeToString([]byte("secret")),
|
|
||||||
},
|
|
||||||
Metadata: oidc.ClientMetadata{
|
|
||||||
RedirectURIs: []url.URL{
|
|
||||||
url.URL{Scheme: "http", Host: "client.example.com", Path: "/callback"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
clientIDGenerator := func(hostport string) (string, error) {
|
|
||||||
return hostport, nil
|
|
||||||
}
|
|
||||||
secGen := func() ([]byte, error) {
|
|
||||||
return []byte("secret"), nil
|
|
||||||
}
|
|
||||||
clientRepo := db.NewClientRepo(dbm)
|
|
||||||
clientManager, err := clientmanager.NewClientManagerFromClients(clientRepo, db.TransactionFactory(dbm), clients, clientmanager.ManagerOptions{ClientIDGenerator: clientIDGenerator, SecretGenerator: secGen})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create client identity manager: %v", err)
|
|
||||||
}
|
|
||||||
srv := &Server{
|
|
||||||
IssuerURL: url.URL{Scheme: "http", Host: "server.example.com"},
|
|
||||||
SessionManager: manager.NewSessionManager(db.NewSessionRepo(db.NewMemDB()), db.NewSessionKeyRepo(db.NewMemDB())),
|
|
||||||
ClientRepo: clientRepo,
|
|
||||||
ClientManager: clientManager,
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
query url.Values
|
query url.Values
|
||||||
|
@ -118,7 +83,7 @@ func TestHandleAuthFuncResponsesSingleRedirectURL(t *testing.T) {
|
||||||
{
|
{
|
||||||
query: url.Values{
|
query: url.Values{
|
||||||
"response_type": []string{"code"},
|
"response_type": []string{"code"},
|
||||||
"client_id": []string{"client.example.com"},
|
"client_id": []string{testClientID},
|
||||||
"connector_id": []string{"fake"},
|
"connector_id": []string{"fake"},
|
||||||
"scope": []string{"openid"},
|
"scope": []string{"openid"},
|
||||||
},
|
},
|
||||||
|
@ -210,7 +175,12 @@ func TestHandleAuthFuncResponsesSingleRedirectURL(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
hdlr := handleAuthFunc(srv, idpcs, nil, true)
|
f, err := makeTestFixtures()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error making test fixtures: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
hdlr := handleAuthFunc(f.srv, idpcs, nil, true)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
u := fmt.Sprintf("http://server.example.com?%s", tt.query.Encode())
|
u := fmt.Sprintf("http://server.example.com?%s", tt.query.Encode())
|
||||||
req, err := http.NewRequest("GET", u, nil)
|
req, err := http.NewRequest("GET", u, nil)
|
||||||
|
@ -237,7 +207,6 @@ func TestHandleAuthFuncResponsesMultipleRedirectURLs(t *testing.T) {
|
||||||
&fakeConnector{loginURL: "http://fake.example.com"},
|
&fakeConnector{loginURL: "http://fake.example.com"},
|
||||||
}
|
}
|
||||||
|
|
||||||
dbm := db.NewMemDB()
|
|
||||||
clients := []client.Client{
|
clients := []client.Client{
|
||||||
client.Client{
|
client.Client{
|
||||||
Credentials: oidc.ClientCredentials{
|
Credentials: oidc.ClientCredentials{
|
||||||
|
@ -252,23 +221,11 @@ func TestHandleAuthFuncResponsesMultipleRedirectURLs(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
f, err := makeTestFixturesWithOptions(testFixtureOptions{
|
||||||
clientIDGenerator := func(hostport string) (string, error) {
|
clients: clients,
|
||||||
return hostport, nil
|
})
|
||||||
}
|
|
||||||
secGen := func() ([]byte, error) {
|
|
||||||
return []byte("secret"), nil
|
|
||||||
}
|
|
||||||
clientRepo := db.NewClientRepo(dbm)
|
|
||||||
clientManager, err := clientmanager.NewClientManagerFromClients(clientRepo, db.TransactionFactory(dbm), clients, clientmanager.ManagerOptions{ClientIDGenerator: clientIDGenerator, SecretGenerator: secGen})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to create client identity manager: %v", err)
|
t.Fatalf("error making test fixtures: %v", err)
|
||||||
}
|
|
||||||
srv := &Server{
|
|
||||||
IssuerURL: url.URL{Scheme: "http", Host: "server.example.com"},
|
|
||||||
SessionManager: manager.NewSessionManager(db.NewSessionRepo(db.NewMemDB()), db.NewSessionKeyRepo(db.NewMemDB())),
|
|
||||||
ClientRepo: clientRepo,
|
|
||||||
ClientManager: clientManager,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
@ -327,7 +284,7 @@ func TestHandleAuthFuncResponsesMultipleRedirectURLs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
hdlr := handleAuthFunc(srv, idpcs, nil, true)
|
hdlr := handleAuthFunc(f.srv, idpcs, nil, true)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
u := fmt.Sprintf("http://server.example.com?%s", tt.query.Encode())
|
u := fmt.Sprintf("http://server.example.com?%s", tt.query.Encode())
|
||||||
req, err := http.NewRequest("GET", u, nil)
|
req, err := http.NewRequest("GET", u, nil)
|
||||||
|
|
|
@ -95,6 +95,10 @@ type testFixtures struct {
|
||||||
clientManager *clientmanager.ClientManager
|
clientManager *clientmanager.ClientManager
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type testFixtureOptions struct {
|
||||||
|
clients []client.Client
|
||||||
|
}
|
||||||
|
|
||||||
func sequentialGenerateCodeFunc() sessionmanager.GenerateCodeFunc {
|
func sequentialGenerateCodeFunc() sessionmanager.GenerateCodeFunc {
|
||||||
x := 0
|
x := 0
|
||||||
return func() (string, error) {
|
return func() (string, error) {
|
||||||
|
@ -104,6 +108,10 @@ func sequentialGenerateCodeFunc() sessionmanager.GenerateCodeFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTestFixtures() (*testFixtures, error) {
|
func makeTestFixtures() (*testFixtures, error) {
|
||||||
|
return makeTestFixturesWithOptions(testFixtureOptions{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeTestFixturesWithOptions(options testFixtureOptions) (*testFixtures, error) {
|
||||||
dbMap := db.NewMemDB()
|
dbMap := db.NewMemDB()
|
||||||
userRepo, err := db.NewUserRepoFromUsers(dbMap, testUsers)
|
userRepo, err := db.NewUserRepoFromUsers(dbMap, testUsers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -150,7 +158,9 @@ func makeTestFixtures() (*testFixtures, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
clients := []client.Client{
|
var clients []client.Client
|
||||||
|
if options.clients == nil {
|
||||||
|
clients = []client.Client{
|
||||||
client.Client{
|
client.Client{
|
||||||
Credentials: testClientCredentials,
|
Credentials: testClientCredentials,
|
||||||
Metadata: oidc.ClientMetadata{
|
Metadata: oidc.ClientMetadata{
|
||||||
|
@ -160,6 +170,9 @@ func makeTestFixtures() (*testFixtures, error) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
clients = options.clients
|
||||||
|
}
|
||||||
|
|
||||||
clientIDGenerator := func(hostport string) (string, error) {
|
clientIDGenerator := func(hostport string) (string, error) {
|
||||||
return hostport, nil
|
return hostport, nil
|
||||||
|
|
Loading…
Reference in a new issue