2015-08-18 05:57:27 +05:30
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2016-04-20 06:57:45 +05:30
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2015-08-18 05:57:27 +05:30
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-04-06 00:07:26 +05:30
|
|
|
"net/url"
|
2015-08-18 05:57:27 +05:30
|
|
|
"testing"
|
|
|
|
|
2016-04-20 06:57:45 +05:30
|
|
|
"github.com/coreos/go-oidc/oidc"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/admin"
|
2016-04-20 06:57:45 +05:30
|
|
|
"github.com/coreos/dex/client"
|
|
|
|
"github.com/coreos/dex/db"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/dex/schema/adminschema"
|
|
|
|
"github.com/coreos/dex/server"
|
|
|
|
"github.com/coreos/dex/user"
|
|
|
|
)
|
|
|
|
|
2015-10-02 00:04:53 +05:30
|
|
|
const (
|
|
|
|
adminAPITestSecret = "admin_secret"
|
|
|
|
)
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
type adminAPITestFixtures struct {
|
|
|
|
ur user.UserRepo
|
|
|
|
pwr user.PasswordInfoRepo
|
2016-04-20 06:57:45 +05:30
|
|
|
cr client.ClientRepo
|
2015-08-18 05:57:27 +05:30
|
|
|
adAPI *admin.AdminAPI
|
|
|
|
adSrv *server.AdminServer
|
|
|
|
hSrv *httptest.Server
|
|
|
|
hc *http.Client
|
|
|
|
adClient *adminschema.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *adminAPITestFixtures) close() {
|
|
|
|
t.hSrv.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
adminUsers = []user.UserWithRemoteIdentities{
|
|
|
|
{
|
|
|
|
User: user.User{
|
|
|
|
ID: "ID-1",
|
|
|
|
Email: "Email-1@example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
User: user.User{
|
|
|
|
ID: "ID-2",
|
|
|
|
Email: "Email-2@example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
User: user.User{
|
|
|
|
ID: "ID-3",
|
|
|
|
Email: "Email-3@example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
adminPasswords = []user.PasswordInfo{
|
|
|
|
{
|
|
|
|
UserID: "ID-1",
|
|
|
|
Password: []byte("hi."),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-10-02 00:04:53 +05:30
|
|
|
type adminAPITransport struct {
|
|
|
|
secret string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *adminAPITransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
r.Header.Set("Authorization", a.secret)
|
|
|
|
return http.DefaultTransport.RoundTrip(r)
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func makeAdminAPITestFixtures() *adminAPITestFixtures {
|
|
|
|
f := &adminAPITestFixtures{}
|
|
|
|
|
2016-04-06 00:07:26 +05:30
|
|
|
dbMap, ur, pwr, um := makeUserObjects(adminUsers, adminPasswords)
|
2016-04-20 06:57:45 +05:30
|
|
|
|
|
|
|
var cliCount int
|
|
|
|
secGen := func() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf("client_%v", cliCount)), nil
|
|
|
|
}
|
|
|
|
cr := db.NewClientRepoWithSecretGenerator(dbMap, secGen)
|
|
|
|
|
|
|
|
f.cr = cr
|
2015-08-18 05:57:27 +05:30
|
|
|
f.ur = ur
|
|
|
|
f.pwr = pwr
|
2016-04-20 06:57:45 +05:30
|
|
|
f.adAPI = admin.NewAdminAPI(ur, pwr, cr, um, "local")
|
2015-10-02 00:04:53 +05:30
|
|
|
f.adSrv = server.NewAdminServer(f.adAPI, nil, adminAPITestSecret)
|
2015-08-18 05:57:27 +05:30
|
|
|
f.hSrv = httptest.NewServer(f.adSrv.HTTPHandler())
|
2015-10-02 00:04:53 +05:30
|
|
|
f.hc = &http.Client{
|
|
|
|
Transport: &adminAPITransport{
|
|
|
|
secret: adminAPITestSecret,
|
|
|
|
},
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
f.adClient, _ = adminschema.NewWithBasePath(f.hc, f.hSrv.URL)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAdmin(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
id string
|
|
|
|
errCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
id: "ID-1",
|
|
|
|
errCode: -1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "ID-2",
|
|
|
|
errCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
func() {
|
|
|
|
f := makeAdminAPITestFixtures()
|
|
|
|
defer f.close()
|
|
|
|
admn, err := f.adClient.Admin.Get(tt.id).Do()
|
|
|
|
if tt.errCode != -1 {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: err was nil", i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gErr, ok := err.(*googleapi.Error)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("case %d: not a googleapi Error: %q", i, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if gErr.Code != tt.errCode {
|
|
|
|
t.Errorf("case %d: want=%d, got=%d", i, tt.errCode, gErr.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
if admn == nil {
|
|
|
|
t.Errorf("case %d: admn was nil", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if admn.Id != "ID-1" {
|
|
|
|
t.Errorf("case %d: want=%q, got=%q", i, tt.id, admn.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAdmin(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-10-14 01:04:28 +05:30
|
|
|
admn *adminschema.Admin
|
|
|
|
errCode int
|
|
|
|
secret string
|
|
|
|
noSecret bool
|
2015-08-18 05:57:27 +05:30
|
|
|
}{
|
|
|
|
{
|
|
|
|
admn: &adminschema.Admin{
|
|
|
|
Email: "foo@example.com",
|
|
|
|
Password: "foopass",
|
|
|
|
},
|
|
|
|
errCode: -1,
|
|
|
|
},
|
2015-10-02 00:04:53 +05:30
|
|
|
{
|
|
|
|
admn: &adminschema.Admin{
|
|
|
|
Email: "foo@example.com",
|
|
|
|
Password: "foopass",
|
|
|
|
},
|
|
|
|
errCode: http.StatusUnauthorized,
|
|
|
|
secret: "bad_secret",
|
|
|
|
},
|
2015-10-14 01:04:28 +05:30
|
|
|
{
|
|
|
|
admn: &adminschema.Admin{
|
|
|
|
Email: "foo@example.com",
|
|
|
|
Password: "foopass",
|
|
|
|
},
|
|
|
|
errCode: http.StatusUnauthorized,
|
|
|
|
noSecret: true,
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
{
|
|
|
|
// duplicate Email
|
|
|
|
admn: &adminschema.Admin{
|
|
|
|
Email: "Email-1@example.com",
|
|
|
|
Password: "foopass",
|
|
|
|
},
|
|
|
|
errCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// missing Email
|
|
|
|
admn: &adminschema.Admin{
|
|
|
|
Password: "foopass",
|
|
|
|
},
|
|
|
|
errCode: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
func() {
|
|
|
|
f := makeAdminAPITestFixtures()
|
2015-10-02 00:04:53 +05:30
|
|
|
if tt.secret != "" {
|
|
|
|
f.hc.Transport = &adminAPITransport{
|
|
|
|
secret: tt.secret,
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 01:04:28 +05:30
|
|
|
if tt.noSecret {
|
|
|
|
f.hc.Transport = http.DefaultTransport
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
defer f.close()
|
|
|
|
|
|
|
|
admn, err := f.adClient.Admin.Create(tt.admn).Do()
|
|
|
|
if tt.errCode != -1 {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: err was nil", i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gErr, ok := err.(*googleapi.Error)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("case %d: not a googleapi Error: %q", i, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if gErr.Code != tt.errCode {
|
|
|
|
t.Errorf("case %d: want=%d, got=%d", i, tt.errCode, gErr.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tt.admn.Id = admn.Id
|
|
|
|
if diff := pretty.Compare(tt.admn, admn); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotAdmn, err := f.adClient.Admin.Get(admn.Id).Do()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(admn, gotAdmn); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := f.ur.GetByRemoteIdentity(nil, user.RemoteIdentity{
|
|
|
|
ConnectorID: "local",
|
|
|
|
ID: tt.admn.Id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if usr.ID != tt.admn.Id {
|
|
|
|
t.Errorf("case %d: want=%q, got=%q", i, tt.admn.Id, usr.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:07:26 +05:30
|
|
|
func TestCreateClient(t *testing.T) {
|
2016-04-20 06:57:45 +05:30
|
|
|
oldGen := admin.ClientIDGenerator
|
|
|
|
admin.ClientIDGenerator = func(hostport string) (string, error) {
|
|
|
|
return fmt.Sprintf("client_%v", hostport), nil
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
admin.ClientIDGenerator = oldGen
|
|
|
|
}()
|
|
|
|
|
|
|
|
mustParseURL := func(s string) *url.URL {
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't parse URL: %v", err)
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
addIDAndSecret := func(cli adminschema.Client) *adminschema.Client {
|
|
|
|
cli.Id = "client_auth.example.com"
|
|
|
|
cli.Secret = base64.URLEncoding.EncodeToString([]byte("client_0"))
|
|
|
|
return &cli
|
|
|
|
}
|
|
|
|
|
|
|
|
adminClientGood := adminschema.Client{
|
|
|
|
RedirectURIs: []string{"https://auth.example.com/"},
|
|
|
|
}
|
|
|
|
clientGood := client.Client{
|
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: "client_auth.example.com",
|
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
|
|
|
RedirectURIs: []url.URL{*mustParseURL("https://auth.example.com/")},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
adminAdminClient := adminClientGood
|
|
|
|
adminAdminClient.IsAdmin = true
|
|
|
|
clientGoodAdmin := clientGood
|
|
|
|
clientGoodAdmin.Admin = true
|
|
|
|
|
|
|
|
adminMultiRedirect := adminClientGood
|
|
|
|
adminMultiRedirect.RedirectURIs = []string{"https://auth.example.com/", "https://auth2.example.com/"}
|
|
|
|
clientMultiRedirect := clientGoodAdmin
|
|
|
|
clientMultiRedirect.Metadata.RedirectURIs = append(
|
|
|
|
clientMultiRedirect.Metadata.RedirectURIs,
|
|
|
|
*mustParseURL("https://auth2.example.com/"))
|
|
|
|
|
2016-04-06 00:07:26 +05:30
|
|
|
tests := []struct {
|
2016-04-20 06:57:45 +05:30
|
|
|
req adminschema.ClientCreateRequest
|
|
|
|
want adminschema.ClientCreateResponse
|
|
|
|
wantClient client.Client
|
|
|
|
wantError int
|
2016-04-06 00:07:26 +05:30
|
|
|
}{
|
|
|
|
{
|
2016-04-20 06:57:45 +05:30
|
|
|
req: adminschema.ClientCreateRequest{},
|
|
|
|
wantError: http.StatusBadRequest,
|
2016-04-06 00:07:26 +05:30
|
|
|
},
|
|
|
|
{
|
2016-04-20 06:57:45 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminschema.Client{
|
|
|
|
IsAdmin: true,
|
2016-04-06 00:07:26 +05:30
|
|
|
},
|
|
|
|
},
|
2016-04-20 06:57:45 +05:30
|
|
|
wantError: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminschema.Client{
|
|
|
|
RedirectURIs: []string{"909090"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantError: http.StatusBadRequest,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminClientGood,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
|
|
|
Client: addIDAndSecret(adminClientGood),
|
|
|
|
},
|
|
|
|
wantClient: clientGood,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminAdminClient,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
|
|
|
Client: addIDAndSecret(adminAdminClient),
|
|
|
|
},
|
|
|
|
wantClient: clientGoodAdmin,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminMultiRedirect,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
|
|
|
Client: addIDAndSecret(adminMultiRedirect),
|
|
|
|
},
|
|
|
|
wantClient: clientMultiRedirect,
|
2016-04-06 00:07:26 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2016-04-20 06:57:45 +05:30
|
|
|
if i != 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f := makeAdminAPITestFixtures()
|
2016-04-15 04:27:53 +05:30
|
|
|
|
2016-04-20 06:57:45 +05:30
|
|
|
resp, err := f.adClient.Client.Create(&tt.req).Do()
|
|
|
|
if tt.wantError != 0 {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: want non-nil error.", i)
|
|
|
|
continue
|
2016-04-06 00:07:26 +05:30
|
|
|
}
|
2016-04-20 06:57:45 +05:30
|
|
|
|
|
|
|
aErr, ok := err.(*googleapi.Error)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("case %d: could not assert as adminSchema.Error: %v", i, err)
|
|
|
|
continue
|
2016-04-06 00:07:26 +05:30
|
|
|
}
|
2016-04-20 06:57:45 +05:30
|
|
|
if aErr.Code != tt.wantError {
|
|
|
|
t.Errorf("case %d: want aErr.Code=%v, got %v", i, tt.wantError, aErr.Code)
|
|
|
|
continue
|
2016-04-06 00:07:26 +05:30
|
|
|
}
|
2016-04-20 06:57:45 +05:30
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:07:26 +05:30
|
|
|
if err != nil {
|
2016-04-20 06:57:45 +05:30
|
|
|
t.Errorf("case %d: unexpected error creating client: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := pretty.Compare(tt.want, resp); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoClient, err := f.cr.Get(resp.Client.Id)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: Unexpected error getting client: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := pretty.Compare(tt.wantClient, repoClient); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(wantClient, repoClient) = %v", i, diff)
|
2016-04-06 00:07:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:57:27 +05:30
|
|
|
func TestGetState(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
addUsers []user.User
|
|
|
|
want adminschema.State
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
addUsers: []user.User{
|
|
|
|
user.User{
|
|
|
|
ID: "ID-admin",
|
|
|
|
Email: "Admin@example.com",
|
|
|
|
Admin: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: adminschema.State{
|
|
|
|
AdminUserCreated: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
want: adminschema.State{
|
|
|
|
AdminUserCreated: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
func() {
|
|
|
|
f := makeAdminAPITestFixtures()
|
|
|
|
defer f.close()
|
|
|
|
|
|
|
|
for _, usr := range tt.addUsers {
|
|
|
|
err := f.ur.Create(nil, usr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("case %d: err != nil: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := f.adClient.State.Get().Do()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: err != nil: %q", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := pretty.Compare(tt.want, got); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %v", i, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|