dex/admin/api_test.go

255 lines
5.2 KiB
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package admin
import (
"testing"
"github.com/coreos/dex/client"
clientmanager "github.com/coreos/dex/client/manager"
"github.com/coreos/dex/connector"
2016-02-10 01:52:40 +05:30
"github.com/coreos/dex/db"
2015-08-18 05:57:27 +05:30
"github.com/coreos/dex/schema/adminschema"
"github.com/coreos/dex/user"
"github.com/coreos/dex/user/manager"
2015-08-18 05:57:27 +05:30
"github.com/kylelemons/godebug/pretty"
)
type testFixtures struct {
ur user.UserRepo
pwr user.PasswordInfoRepo
cr client.ClientRepo
cm *clientmanager.ClientManager
mgr *manager.UserManager
2015-08-18 05:57:27 +05:30
adAPI *AdminAPI
}
func makeTestFixtures() *testFixtures {
f := &testFixtures{}
2016-02-10 01:52:40 +05:30
dbMap := db.NewMemDB()
f.ur = func() user.UserRepo {
repo, err := db.NewUserRepoFromUsers(dbMap, []user.UserWithRemoteIdentities{
{
User: user.User{
ID: "ID-1",
Email: "email-1@example.com",
DisplayName: "Name-1",
},
},
2016-02-10 01:52:40 +05:30
{
User: user.User{
ID: "ID-2",
Email: "email-2@example.com",
DisplayName: "Name-2",
},
2015-08-18 05:57:27 +05:30
},
2016-02-10 01:52:40 +05:30
})
if err != nil {
panic("Failed to create user repo: " + err.Error())
}
return repo
}()
2016-02-10 02:27:42 +05:30
f.pwr = func() user.PasswordInfoRepo {
repo, err := db.NewPasswordInfoRepoFromPasswordInfos(dbMap, []user.PasswordInfo{
{
UserID: "ID-1",
Password: []byte("hi."),
},
})
if err != nil {
panic("Failed to create user repo: " + err.Error())
}
return repo
}()
ccr := func() connector.ConnectorConfigRepo {
c := []connector.ConnectorConfig{&connector.LocalConnectorConfig{ID: "local"}}
repo := db.NewConnectorConfigRepo(dbMap)
if err := repo.Set(c); err != nil {
panic(err)
}
return repo
}()
2016-02-10 01:52:40 +05:30
f.mgr = manager.NewUserManager(f.ur, f.pwr, ccr, db.TransactionFactory(dbMap), manager.ManagerOptions{})
f.cm = clientmanager.NewClientManager(f.cr, db.TransactionFactory(dbMap), clientmanager.ManagerOptions{})
f.adAPI = NewAdminAPI(f.ur, f.pwr, f.cr, f.mgr, f.cm, "local")
2015-08-18 05:57:27 +05:30
return f
}
func TestGetAdmin(t *testing.T) {
tests := []struct {
id string
wantErr error
2015-08-18 05:57:27 +05:30
}{
{
id: "ID-1",
2015-08-18 05:57:27 +05:30
},
{
// Not found
id: "ID-3",
wantErr: user.ErrorNotFound,
2015-08-18 05:57:27 +05:30
},
}
for i, tt := range tests {
f := makeTestFixtures()
admn, err := f.adAPI.GetAdmin(tt.id)
if tt.wantErr != nil {
2015-08-18 05:57:27 +05:30
if err == nil {
t.Errorf("case %d: err was nil", i)
continue
}
aErr, ok := err.(Error)
if !ok {
t.Errorf("case %d: not an admin.Error: %q", i, err)
continue
}
if aErr.Internal != tt.wantErr {
t.Errorf("case %d: want=%q, got=%q", i, tt.wantErr, aErr.Internal)
2015-08-18 05:57:27 +05:30
continue
}
} else {
if err != nil {
t.Errorf("case %d: err != nil: %q", i, err)
continue
2015-08-18 05:57:27 +05:30
}
if admn.Id != "ID-1" {
t.Errorf("case %d: want=%q, got=%q", i, tt.id, admn.Id)
}
}
}
}
func TestCreateAdmin(t *testing.T) {
hashedPassword, _ := user.NewPasswordFromPlaintext("foopass")
2015-08-18 05:57:27 +05:30
tests := []struct {
admn adminschema.Admin
wantErr error
2015-08-18 05:57:27 +05:30
}{
{
//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
2015-08-18 05:57:27 +05:30
admn: adminschema.Admin{
Email: "email-2@example.com",
Password: "foopass",
2015-08-18 05:57:27 +05:30
},
wantErr: user.ErrorDuplicateEmail,
2015-08-18 05:57:27 +05:30
},
{
// bad email
2015-08-18 05:57:27 +05:30
admn: adminschema.Admin{
Email: "badEmailexample",
Password: "foopass",
2015-08-18 05:57:27 +05:30
},
wantErr: user.ErrorInvalidEmail,
2015-08-18 05:57:27 +05:30
},
{
// missing Email
2015-08-18 05:57:27 +05:30
admn: adminschema.Admin{
Password: "foopass",
2015-08-18 05:57:27 +05:30
},
wantErr: user.ErrorInvalidEmail,
2015-08-18 05:57:27 +05:30
},
}
for i, tt := range tests {
f := makeTestFixtures()
id, err := f.adAPI.CreateAdmin(tt.admn)
if tt.wantErr != nil {
2015-08-18 05:57:27 +05:30
if err == nil {
t.Errorf("case %d: err was nil", i)
continue
}
aErr, ok := err.(Error)
if !ok {
t.Errorf("case %d: not a admin.Error: %#v", i, err)
continue
}
if aErr.Internal != tt.wantErr {
t.Errorf("case %d: want=%q, got=%q", i, tt.wantErr, aErr.Internal)
2015-08-18 05:57:27 +05:30
continue
}
} else {
if err != nil {
t.Errorf("case %d: err != nil: %q", i, err)
}
gotAdmn, err := f.adAPI.GetAdmin(id)
if err != nil {
t.Errorf("case %d: err != nil: %q", i, err)
}
tt.admn.Id = id
if diff := pretty.Compare(tt.admn, gotAdmn); diff != "" {
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
}
}
}
}
func TestGetState(t *testing.T) {
tests := []struct {
addUsers []user.User
want adminschema.State
}{
{
addUsers: []user.User{
user.User{
ID: "ID-3",
Email: "email-3@example.com",
DisplayName: "Admin",
Admin: true,
2015-08-18 05:57:27 +05:30
},
},
want: adminschema.State{
AdminUserCreated: true,
},
},
{
want: adminschema.State{
AdminUserCreated: false,
},
},
}
for i, tt := range tests {
f := makeTestFixtures()
for _, usr := range tt.addUsers {
_, err := f.mgr.CreateUser(usr, user.Password("foopass"), f.adAPI.localConnectorID)
2015-08-18 05:57:27 +05:30
if err != nil {
t.Fatalf("case %d: err != nil: %q", i, err)
2015-08-18 05:57:27 +05:30
}
}
got, err := f.adAPI.GetState()
if err != nil {
t.Errorf("case %d: err != nil: %q", i, err)
2015-08-18 05:57:27 +05:30
}
if diff := pretty.Compare(tt.want, got); diff != "" {
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
}
}
}