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"
|
2016-05-12 22:23:01 +05:30
|
|
|
"github.com/coreos/dex/client/manager"
|
2016-04-20 06:57:45 +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/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."),
|
|
|
|
},
|
|
|
|
}
|
2016-06-29 04:39:20 +05:30
|
|
|
|
|
|
|
clients = []client.Client{
|
|
|
|
{
|
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: "client-1",
|
|
|
|
Secret: "Zm9vYmFy", // "foobar"
|
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
|
|
|
RedirectURIs: []url.URL{
|
|
|
|
url.URL{Scheme: "http", Host: "127.0.0.1:5556", Path: "/cb"},
|
|
|
|
url.URL{Scheme: "https", Host: "example.com", Path: "/callback"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Admin: true,
|
|
|
|
},
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
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) {
|
2016-04-27 00:05:34 +05:30
|
|
|
id := []byte(fmt.Sprintf("client_%v", cliCount))
|
|
|
|
cliCount++
|
|
|
|
return id, nil
|
2016-04-20 06:57:45 +05:30
|
|
|
}
|
2016-05-12 22:23:01 +05:30
|
|
|
cr := db.NewClientRepo(dbMap)
|
|
|
|
clientIDGenerator := func(hostport string) (string, error) {
|
|
|
|
return fmt.Sprintf("client_%v", hostport), nil
|
|
|
|
}
|
2016-06-29 04:39:20 +05:30
|
|
|
for _, client := range clients {
|
|
|
|
_, err := cr.New(nil, client)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 22:23:01 +05:30
|
|
|
cm := manager.NewClientManager(cr, db.TransactionFactory(dbMap), manager.ManagerOptions{SecretGenerator: secGen, ClientIDGenerator: clientIDGenerator})
|
2016-05-27 03:12:15 +05:30
|
|
|
ccr := db.NewConnectorConfigRepo(dbMap)
|
2016-04-20 06:57:45 +05:30
|
|
|
|
|
|
|
f.cr = cr
|
2015-08-18 05:57:27 +05:30
|
|
|
f.ur = ur
|
|
|
|
f.pwr = pwr
|
2016-05-27 03:12:15 +05:30
|
|
|
f.adAPI = admin.NewAdminAPI(ur, pwr, cr, ccr, um, cm, "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",
|
|
|
|
},
|
2016-06-29 02:12:20 +05:30
|
|
|
errCode: http.StatusConflict,
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
// 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-05-27 03:12:15 +05:30
|
|
|
func TestConnectors(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
req adminschema.ConnectorsSetRequest
|
|
|
|
want adminschema.ConnectorsGetResponse
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
req: adminschema.ConnectorsSetRequest{
|
|
|
|
Connectors: []interface{}{
|
|
|
|
map[string]string{
|
|
|
|
"type": "local",
|
|
|
|
"id": "local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: adminschema.ConnectorsGetResponse{
|
|
|
|
Connectors: []interface{}{
|
|
|
|
map[string]string{
|
|
|
|
"id": "local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: adminschema.ConnectorsSetRequest{
|
|
|
|
Connectors: []interface{}{
|
|
|
|
map[string]string{
|
|
|
|
"type": "github",
|
|
|
|
"id": "github",
|
|
|
|
"clientID": "foo",
|
|
|
|
"clientSecret": "bar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "oidc",
|
|
|
|
"id": "oidc",
|
|
|
|
"issuerURL": "https://auth.example.com",
|
|
|
|
"clientID": "foo",
|
|
|
|
"clientSecret": "bar",
|
|
|
|
"trustedEmailProvider": true,
|
2016-08-12 07:15:47 +05:30
|
|
|
"emailClaim": "",
|
2016-05-27 03:12:15 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: adminschema.ConnectorsGetResponse{
|
|
|
|
Connectors: []interface{}{
|
|
|
|
map[string]string{
|
|
|
|
"id": "github",
|
|
|
|
"clientID": "foo",
|
|
|
|
"clientSecret": "bar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"id": "oidc",
|
|
|
|
"issuerURL": "https://auth.example.com",
|
|
|
|
"clientID": "foo",
|
|
|
|
"clientSecret": "bar",
|
|
|
|
"trustedEmailProvider": true,
|
2016-08-12 07:15:47 +05:30
|
|
|
"emailClaim": "",
|
2016-05-27 03:12:15 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Missing "type" argument
|
|
|
|
req: adminschema.ConnectorsSetRequest{
|
|
|
|
Connectors: []interface{}{
|
|
|
|
map[string]string{
|
|
|
|
"id": "local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := makeAdminAPITestFixtures()
|
|
|
|
if err := f.adClient.Connectors.Set(&tt.req).Do(); err != nil {
|
|
|
|
if !tt.wantErr {
|
|
|
|
t.Errorf("case %d: failed to set connectors: %v", i, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tt.wantErr {
|
|
|
|
t.Errorf("case %d: expected error setting connectors", i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := f.adClient.Connectors.Get().Do()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: failed toget connectors: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(tt.want, resp); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got) = %s", i, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:07:26 +05:30
|
|
|
func TestCreateClient(t *testing.T) {
|
2016-04-20 06:57:45 +05:30
|
|
|
mustParseURL := func(s string) *url.URL {
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't parse URL: %v", err)
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
2016-04-27 00:05:34 +05:30
|
|
|
|
2016-04-27 00:35:41 +05:30
|
|
|
addIDAndSecret := func(cli adminschema.Client) *adminschema.Client {
|
2016-06-22 00:33:47 +05:30
|
|
|
if cli.Id == "" {
|
|
|
|
if cli.Public {
|
|
|
|
cli.Id = "client_" + cli.ClientName
|
|
|
|
} else {
|
|
|
|
cli.Id = "client_auth.example.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli.Secret == "" {
|
|
|
|
cli.Secret = base64.URLEncoding.EncodeToString([]byte("client_0"))
|
2016-06-18 00:03:41 +05:30
|
|
|
}
|
2016-04-20 06:57:45 +05:30
|
|
|
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/")},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-18 00:03:41 +05:30
|
|
|
clientPublicGood := clientGood
|
|
|
|
clientPublicGood.Public = true
|
|
|
|
clientPublicGood.Metadata.ClientName = "PublicName"
|
|
|
|
clientPublicGood.Metadata.RedirectURIs = []url.URL{}
|
|
|
|
clientPublicGood.Credentials.ID = "client_PublicName"
|
|
|
|
|
|
|
|
adminPublicClientGood := adminClientGood
|
|
|
|
adminPublicClientGood.Public = true
|
|
|
|
adminPublicClientGood.ClientName = "PublicName"
|
|
|
|
adminPublicClientGood.RedirectURIs = []string{}
|
|
|
|
|
|
|
|
adminPublicClientMissingName := adminPublicClientGood
|
|
|
|
adminPublicClientMissingName.ClientName = ""
|
|
|
|
|
|
|
|
adminPublicClientHasARedirect := adminPublicClientGood
|
|
|
|
adminPublicClientHasARedirect.RedirectURIs = []string{"https://auth.example.com/"}
|
|
|
|
|
2016-04-20 06:57:45 +05:30
|
|
|
adminAdminClient := adminClientGood
|
|
|
|
adminAdminClient.IsAdmin = true
|
|
|
|
clientGoodAdmin := clientGood
|
|
|
|
clientGoodAdmin.Admin = true
|
|
|
|
|
|
|
|
adminMultiRedirect := adminClientGood
|
|
|
|
adminMultiRedirect.RedirectURIs = []string{"https://auth.example.com/", "https://auth2.example.com/"}
|
2016-04-27 00:05:34 +05:30
|
|
|
clientMultiRedirect := clientGood
|
2016-04-20 06:57:45 +05:30
|
|
|
clientMultiRedirect.Metadata.RedirectURIs = append(
|
|
|
|
clientMultiRedirect.Metadata.RedirectURIs,
|
|
|
|
*mustParseURL("https://auth2.example.com/"))
|
|
|
|
|
2016-04-27 00:05:34 +05:30
|
|
|
adminClientWithPeers := adminClientGood
|
|
|
|
adminClientWithPeers.TrustedPeers = []string{"test_client_0"}
|
|
|
|
|
2016-06-22 00:33:47 +05:30
|
|
|
adminClientOwnID := adminClientGood
|
|
|
|
adminClientOwnID.Id = "my_own_id"
|
|
|
|
|
|
|
|
clientGoodOwnID := clientGood
|
|
|
|
clientGoodOwnID.Credentials.ID = "my_own_id"
|
|
|
|
|
|
|
|
adminClientOwnSecret := adminClientGood
|
|
|
|
adminClientOwnSecret.Secret = base64.URLEncoding.EncodeToString([]byte("my_own_secret"))
|
|
|
|
clientGoodOwnSecret := clientGood
|
|
|
|
|
|
|
|
adminClientOwnIDAndSecret := adminClientGood
|
|
|
|
adminClientOwnIDAndSecret.Id = "my_own_id"
|
|
|
|
adminClientOwnIDAndSecret.Secret = base64.URLEncoding.EncodeToString([]byte("my_own_secret"))
|
|
|
|
clientGoodOwnIDAndSecret := clientGoodOwnID
|
|
|
|
|
|
|
|
adminClientBadSecret := adminClientGood
|
|
|
|
adminClientBadSecret.Secret = "not_base64_encoded"
|
|
|
|
|
2016-04-06 00:07:26 +05:30
|
|
|
tests := []struct {
|
2016-04-27 00:05:34 +05:30
|
|
|
req adminschema.ClientCreateRequest
|
|
|
|
want adminschema.ClientCreateResponse
|
|
|
|
wantClient client.Client
|
|
|
|
wantError int
|
|
|
|
wantTrustedPeers []string
|
2016-04-06 00:07:26 +05:30
|
|
|
}{
|
|
|
|
{
|
2016-04-20 06:57:45 +05:30
|
|
|
req: adminschema.ClientCreateRequest{},
|
|
|
|
wantError: http.StatusBadRequest,
|
2016-06-22 00:33:47 +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,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-04-20 06:57:45 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminschema.Client{
|
|
|
|
RedirectURIs: []string{"909090"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantError: http.StatusBadRequest,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-04-20 06:57:45 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminClientGood,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
2016-04-27 00:35:41 +05:30
|
|
|
Client: addIDAndSecret(adminClientGood),
|
2016-04-20 06:57:45 +05:30
|
|
|
},
|
|
|
|
wantClient: clientGood,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-04-20 06:57:45 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminAdminClient,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
2016-04-27 00:35:41 +05:30
|
|
|
Client: addIDAndSecret(adminAdminClient),
|
2016-04-20 06:57:45 +05:30
|
|
|
},
|
|
|
|
wantClient: clientGoodAdmin,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-04-20 06:57:45 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminMultiRedirect,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
2016-04-27 00:35:41 +05:30
|
|
|
Client: addIDAndSecret(adminMultiRedirect),
|
2016-04-20 06:57:45 +05:30
|
|
|
},
|
|
|
|
wantClient: clientMultiRedirect,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-04-27 00:05:34 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminClientWithPeers,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
2016-04-27 00:35:41 +05:30
|
|
|
Client: addIDAndSecret(adminClientWithPeers),
|
2016-04-27 00:05:34 +05:30
|
|
|
},
|
|
|
|
wantClient: clientGood,
|
|
|
|
wantTrustedPeers: []string{"test_client_0"},
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-06-18 00:03:41 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminPublicClientGood,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
|
|
|
Client: addIDAndSecret(adminPublicClientGood),
|
|
|
|
},
|
|
|
|
wantClient: clientPublicGood,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-06-18 00:03:41 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminPublicClientMissingName,
|
|
|
|
},
|
|
|
|
wantError: http.StatusBadRequest,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
2016-06-18 00:03:41 +05:30
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminPublicClientHasARedirect,
|
|
|
|
},
|
|
|
|
wantError: http.StatusBadRequest,
|
2016-06-22 00:33:47 +05:30
|
|
|
}, {
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminClientOwnID,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
|
|
|
Client: addIDAndSecret(adminClientOwnID),
|
|
|
|
},
|
|
|
|
wantClient: clientGoodOwnID,
|
|
|
|
}, {
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminClientOwnSecret,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
|
|
|
Client: addIDAndSecret(adminClientOwnSecret),
|
|
|
|
},
|
|
|
|
wantClient: clientGoodOwnSecret,
|
|
|
|
}, {
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminClientOwnIDAndSecret,
|
|
|
|
},
|
|
|
|
want: adminschema.ClientCreateResponse{
|
|
|
|
Client: addIDAndSecret(adminClientOwnIDAndSecret),
|
|
|
|
},
|
|
|
|
wantClient: clientGoodOwnIDAndSecret,
|
|
|
|
}, {
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminClientBadSecret,
|
|
|
|
},
|
|
|
|
wantError: http.StatusBadRequest,
|
2016-06-29 04:39:20 +05:30
|
|
|
}, {
|
|
|
|
// Client ID already exists
|
|
|
|
req: adminschema.ClientCreateRequest{
|
|
|
|
Client: &adminschema.Client{
|
|
|
|
Id: "client-1",
|
|
|
|
Secret: "Zm9vYmFy",
|
|
|
|
RedirectURIs: []string{"https://auth.example.com/"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantError: http.StatusConflict,
|
2016-06-18 00:03:41 +05:30
|
|
|
},
|
2016-04-06 00:07:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2016-04-20 06:57:45 +05:30
|
|
|
f := makeAdminAPITestFixtures()
|
2016-04-27 00:05:34 +05:30
|
|
|
for j, r := range []string{"https://client0.example.com",
|
|
|
|
"https://client1.example.com"} {
|
|
|
|
_, err := f.cr.New(nil, client.Client{
|
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: fmt.Sprintf("test_client_%d", j),
|
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
|
|
|
RedirectURIs: []url.URL{*mustParseURL(r)},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d, client %d: unexpected error creating client: %v", i, j, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2016-06-29 04:39:20 +05:30
|
|
|
t.Errorf("case %d: want aErr.Code=%v, got %v: %v", i, tt.wantError, aErr.Code, aErr)
|
2016-04-20 06:57:45 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
repoClient, err := f.cr.Get(nil, resp.Client.Id)
|
2016-04-20 06:57:45 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: Unexpected error getting client: %v", i, err)
|
2016-06-18 00:03:41 +05:30
|
|
|
continue
|
2016-04-20 06:57:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|