2015-08-18 05:57:27 +05:30
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-01-25 18:08:45 +05:30
|
|
|
"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"
|
2016-01-25 18:08:45 +05:30
|
|
|
"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
|
2016-01-25 18:08:45 +05:30
|
|
|
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-01-25 18:08:45 +05:30
|
|
|
},
|
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
|
|
|
|
}()
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
f.pwr = user.NewPasswordInfoRepoFromPasswordInfos([]user.PasswordInfo{
|
|
|
|
{
|
|
|
|
UserID: "ID-1",
|
|
|
|
Password: []byte("hi."),
|
|
|
|
},
|
|
|
|
})
|
2016-01-25 18:08:45 +05:30
|
|
|
ccr := connector.NewConnectorConfigRepoFromConfigs([]connector.ConnectorConfig{
|
|
|
|
&connector.LocalConnectorConfig{ID: "local"},
|
|
|
|
})
|
2016-02-10 01:52:40 +05:30
|
|
|
f.mgr = manager.NewUserManager(f.ur, f.pwr, ccr, db.TransactionFactory(dbMap), manager.ManagerOptions{})
|
2016-01-25 18:08:45 +05:30
|
|
|
f.adAPI = NewAdminAPI(f.mgr, f.ur, f.pwr, "local")
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAdmin(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
id string
|
2016-01-25 18:08:45 +05:30
|
|
|
wantErr error
|
2015-08-18 05:57:27 +05:30
|
|
|
}{
|
|
|
|
{
|
2016-01-25 18:08:45 +05:30
|
|
|
id: "ID-1",
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
2016-01-25 18:08:45 +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)
|
2016-01-25 18:08:45 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-25 18:08:45 +05:30
|
|
|
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)
|
2016-01-25 18:08:45 +05:30
|
|
|
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) {
|
2016-01-25 18:08:45 +05:30
|
|
|
hashedPassword, _ := user.NewPasswordFromPlaintext("foopass")
|
2015-08-18 05:57:27 +05:30
|
|
|
tests := []struct {
|
|
|
|
admn adminschema.Admin
|
2016-01-25 18:08:45 +05:30
|
|
|
wantErr error
|
2015-08-18 05:57:27 +05:30
|
|
|
}{
|
|
|
|
{
|
2016-01-25 18:08:45 +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{
|
2016-01-25 18:08:45 +05:30
|
|
|
Email: "email-2@example.com",
|
|
|
|
Password: "foopass",
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
2016-01-25 18:08:45 +05:30
|
|
|
wantErr: user.ErrorDuplicateEmail,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
2016-01-25 18:08:45 +05:30
|
|
|
// bad email
|
2015-08-18 05:57:27 +05:30
|
|
|
admn: adminschema.Admin{
|
2016-01-25 18:08:45 +05:30
|
|
|
Email: "badEmailexample",
|
|
|
|
Password: "foopass",
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
2016-01-25 18:08:45 +05:30
|
|
|
wantErr: user.ErrorInvalidEmail,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
2016-01-25 18:08:45 +05:30
|
|
|
// missing Email
|
2015-08-18 05:57:27 +05:30
|
|
|
admn: adminschema.Admin{
|
2016-01-25 18:08:45 +05:30
|
|
|
Password: "foopass",
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
2016-01-25 18:08:45 +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)
|
2016-01-25 18:08:45 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-25 18:08:45 +05:30
|
|
|
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{
|
2016-01-25 18:08:45 +05:30
|
|
|
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 {
|
2016-01-25 18:08:45 +05:30
|
|
|
_, err := f.mgr.CreateUser(usr, user.Password("foopass"), f.adAPI.localConnectorID)
|
2015-08-18 05:57:27 +05:30
|
|
|
if err != nil {
|
2016-01-25 18:08:45 +05:30
|
|
|
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 {
|
2016-01-25 18:08:45 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|