dex/admin/api_test.go

229 lines
4.5 KiB
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package admin
import (
"testing"
"github.com/coreos/dex/connector"
"github.com/coreos/dex/repo"
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
mgr *manager.UserManager
2015-08-18 05:57:27 +05:30
adAPI *AdminAPI
}
func makeTestFixtures() *testFixtures {
f := &testFixtures{}
f.ur = user.NewUserRepoFromUsers([]user.UserWithRemoteIdentities{
{
User: user.User{
ID: "ID-1",
Email: "email-1@example.com",
DisplayName: "Name-1",
},
},
{
User: user.User{
ID: "ID-2",
Email: "email-2@example.com",
DisplayName: "Name-2",
2015-08-18 05:57:27 +05:30
},
},
})
f.pwr = user.NewPasswordInfoRepoFromPasswordInfos([]user.PasswordInfo{
{
UserID: "ID-1",
Password: []byte("hi."),
},
})
ccr := connector.NewConnectorConfigRepoFromConfigs([]connector.ConnectorConfig{
&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")
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)
}
}
}