Commit 5961122c authored by Eric Chiang's avatar Eric Chiang

Merge pull request #291 from Tecsisa/admintest

 admin: tests do not compile (Fixes #257)
parents c7606ae3 4c7fc432
// NOTE: These tests are begin updated so they compile (see #257). Until then ignore.
// +build ignore
package admin package admin
import ( import (
"net/http"
"testing" "testing"
"github.com/coreos/dex/connector"
"github.com/coreos/dex/repo"
"github.com/coreos/dex/schema/adminschema" "github.com/coreos/dex/schema/adminschema"
"github.com/coreos/dex/user" "github.com/coreos/dex/user"
"github.com/coreos/dex/user/manager"
"github.com/kylelemons/godebug/pretty" "github.com/kylelemons/godebug/pretty"
) )
...@@ -16,6 +15,7 @@ import ( ...@@ -16,6 +15,7 @@ import (
type testFixtures struct { type testFixtures struct {
ur user.UserRepo ur user.UserRepo
pwr user.PasswordInfoRepo pwr user.PasswordInfoRepo
mgr *manager.UserManager
adAPI *AdminAPI adAPI *AdminAPI
} }
...@@ -25,8 +25,16 @@ func makeTestFixtures() *testFixtures { ...@@ -25,8 +25,16 @@ func makeTestFixtures() *testFixtures {
f.ur = user.NewUserRepoFromUsers([]user.UserWithRemoteIdentities{ f.ur = user.NewUserRepoFromUsers([]user.UserWithRemoteIdentities{
{ {
User: user.User{ User: user.User{
ID: "ID-1", ID: "ID-1",
Name: "Name-1", Email: "email-1@example.com",
DisplayName: "Name-1",
},
},
{
User: user.User{
ID: "ID-2",
Email: "email-2@example.com",
DisplayName: "Name-2",
}, },
}, },
}) })
...@@ -36,8 +44,11 @@ func makeTestFixtures() *testFixtures { ...@@ -36,8 +44,11 @@ func makeTestFixtures() *testFixtures {
Password: []byte("hi."), Password: []byte("hi."),
}, },
}) })
ccr := connector.NewConnectorConfigRepoFromConfigs([]connector.ConnectorConfig{
f.adAPI = NewAdminAPI(f.ur, f.pwr) &connector.LocalConnectorConfig{ID: "local"},
})
f.mgr = manager.NewUserManager(f.ur, f.pwr, ccr, repo.InMemTransactionFactory, manager.ManagerOptions{})
f.adAPI = NewAdminAPI(f.mgr, f.ur, f.pwr, "local")
return f return f
} }
...@@ -45,15 +56,15 @@ func makeTestFixtures() *testFixtures { ...@@ -45,15 +56,15 @@ func makeTestFixtures() *testFixtures {
func TestGetAdmin(t *testing.T) { func TestGetAdmin(t *testing.T) {
tests := []struct { tests := []struct {
id string id string
errCode int wantErr error
}{ }{
{ {
id: "ID-1", id: "ID-1",
errCode: -1,
}, },
{ {
id: "ID-2", // Not found
errCode: http.StatusNotFound, id: "ID-3",
wantErr: user.ErrorNotFound,
}, },
} }
...@@ -61,7 +72,7 @@ func TestGetAdmin(t *testing.T) { ...@@ -61,7 +72,7 @@ func TestGetAdmin(t *testing.T) {
f := makeTestFixtures() f := makeTestFixtures()
admn, err := f.adAPI.GetAdmin(tt.id) admn, err := f.adAPI.GetAdmin(tt.id)
if tt.errCode != -1 { if tt.wantErr != nil {
if err == nil { if err == nil {
t.Errorf("case %d: err was nil", i) t.Errorf("case %d: err was nil", i)
continue continue
...@@ -72,15 +83,15 @@ func TestGetAdmin(t *testing.T) { ...@@ -72,15 +83,15 @@ func TestGetAdmin(t *testing.T) {
continue continue
} }
if aErr.Code != tt.errCode { if aErr.Internal != tt.wantErr {
t.Errorf("case %d: want=%d, got=%d", i, tt.errCode, aErr.Code) t.Errorf("case %d: want=%q, got=%q", i, tt.wantErr, aErr.Internal)
continue continue
} }
} else { } else {
if err != nil { if err != nil {
t.Errorf("case %d: err != nil: %q", i, err) t.Errorf("case %d: err != nil: %q", i, err)
continue
} }
continue
if admn.Id != "ID-1" { if admn.Id != "ID-1" {
t.Errorf("case %d: want=%q, got=%q", i, tt.id, admn.Id) t.Errorf("case %d: want=%q, got=%q", i, tt.id, admn.Id)
...@@ -92,38 +103,54 @@ func TestGetAdmin(t *testing.T) { ...@@ -92,38 +103,54 @@ func TestGetAdmin(t *testing.T) {
} }
func TestCreateAdmin(t *testing.T) { func TestCreateAdmin(t *testing.T) {
hashedPassword, _ := user.NewPasswordFromPlaintext("foopass")
tests := []struct { tests := []struct {
admn adminschema.Admin admn adminschema.Admin
errCode int wantErr error
}{ }{
{ {
//hashed password
admn: adminschema.Admin{
Email: "goodemail@example.com",
Password: string(hashedPassword),
},
},
{
//plaintext password
admn: adminschema.Admin{
Email: "goodemail@example.com",
Password: "foopass",
},
},
{
// duplicate Email
admn: adminschema.Admin{ admn: adminschema.Admin{
Name: "foo", Email: "email-2@example.com",
PasswordHash: user.Password([]byte("foopass")).EncodeBase64(), Password: "foopass",
}, },
errCode: -1, wantErr: user.ErrorDuplicateEmail,
}, },
{ {
// duplicate Name // bad email
admn: adminschema.Admin{ admn: adminschema.Admin{
Name: "Name-1", Email: "badEmailexample",
PasswordHash: user.Password([]byte("foopass")).EncodeBase64(), Password: "foopass",
}, },
errCode: http.StatusBadRequest, wantErr: user.ErrorInvalidEmail,
}, },
{ {
// missing Name // missing Email
admn: adminschema.Admin{ admn: adminschema.Admin{
PasswordHash: user.Password([]byte("foopass")).EncodeBase64(), Password: "foopass",
}, },
errCode: http.StatusBadRequest, wantErr: user.ErrorInvalidEmail,
}, },
} }
for i, tt := range tests { for i, tt := range tests {
f := makeTestFixtures() f := makeTestFixtures()
id, err := f.adAPI.CreateAdmin(tt.admn) id, err := f.adAPI.CreateAdmin(tt.admn)
if tt.errCode != -1 { if tt.wantErr != nil {
if err == nil { if err == nil {
t.Errorf("case %d: err was nil", i) t.Errorf("case %d: err was nil", i)
continue continue
...@@ -134,8 +161,8 @@ func TestCreateAdmin(t *testing.T) { ...@@ -134,8 +161,8 @@ func TestCreateAdmin(t *testing.T) {
continue continue
} }
if aErr.Code != tt.errCode { if aErr.Internal != tt.wantErr {
t.Errorf("case %d: want=%d, got=%d", i, tt.errCode, aErr.Code) t.Errorf("case %d: want=%q, got=%q", i, tt.wantErr, aErr.Internal)
continue continue
} }
} else { } else {
...@@ -164,8 +191,10 @@ func TestGetState(t *testing.T) { ...@@ -164,8 +191,10 @@ func TestGetState(t *testing.T) {
{ {
addUsers: []user.User{ addUsers: []user.User{
user.User{ user.User{
Name: "Admin", ID: "ID-3",
Admin: true, Email: "email-3@example.com",
DisplayName: "Admin",
Admin: true,
}, },
}, },
want: adminschema.State{ want: adminschema.State{
...@@ -181,15 +210,15 @@ func TestGetState(t *testing.T) { ...@@ -181,15 +210,15 @@ func TestGetState(t *testing.T) {
for i, tt := range tests { for i, tt := range tests {
f := makeTestFixtures() f := makeTestFixtures()
for _, usr := range tt.addUsers { for _, usr := range tt.addUsers {
_, err := f.ur.Create(usr) _, err := f.mgr.CreateUser(usr, user.Password("foopass"), f.adAPI.localConnectorID)
if err != nil { if err != nil {
t.Fatalf("case %d: err != nil", i, err) t.Fatalf("case %d: err != nil: %q", i, err)
} }
} }
got, err := f.adAPI.GetState() got, err := f.adAPI.GetState()
if err != nil { if err != nil {
t.Errorf("case %d: err != nil", i, err) t.Errorf("case %d: err != nil: %q", i, err)
} }
if diff := pretty.Compare(tt.want, got); diff != "" { if diff := pretty.Compare(tt.want, got); diff != "" {
......
...@@ -14,7 +14,7 @@ COVER=${COVER:-"-cover"} ...@@ -14,7 +14,7 @@ COVER=${COVER:-"-cover"}
source ./build source ./build
TESTABLE="connector db integration pkg/crypto pkg/flag pkg/http pkg/net pkg/time pkg/html functional/repo server session user user/api user/manager email" TESTABLE="connector db integration pkg/crypto pkg/flag pkg/http pkg/net pkg/time pkg/html functional/repo server session user user/api user/manager email admin"
FORMATTABLE="$TESTABLE cmd/dexctl cmd/dex-worker cmd/dex-overlord examples/app functional pkg/log" FORMATTABLE="$TESTABLE cmd/dexctl cmd/dex-worker cmd/dex-overlord examples/app functional pkg/log"
# user has not provided PKG override # user has not provided PKG override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment