2015-08-18 05:57:27 +05:30
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2016-02-10 04:36:07 +05:30
|
|
|
"encoding/base64"
|
2015-08-18 05:57:27 +05:30
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/coreos/go-oidc/oidc"
|
2016-02-10 04:36:07 +05:30
|
|
|
"github.com/go-gorp/gorp"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
"github.com/coreos/dex/client"
|
|
|
|
"github.com/coreos/dex/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testClients = []oidc.ClientIdentity{
|
|
|
|
oidc.ClientIdentity{
|
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: "client1",
|
2016-02-10 04:36:07 +05:30
|
|
|
Secret: base64.URLEncoding.EncodeToString([]byte("secret-1")),
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
2016-01-13 06:46:28 +05:30
|
|
|
RedirectURIs: []url.URL{
|
2015-08-18 05:57:27 +05:30
|
|
|
url.URL{
|
|
|
|
Scheme: "https",
|
2016-04-06 23:55:50 +05:30
|
|
|
Host: "client1.example.com",
|
|
|
|
Path: "/callback",
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
oidc.ClientIdentity{
|
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: "client2",
|
2016-02-10 04:36:07 +05:30
|
|
|
Secret: base64.URLEncoding.EncodeToString([]byte("secret-2")),
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
2016-01-13 06:46:28 +05:30
|
|
|
RedirectURIs: []url.URL{
|
2015-08-18 05:57:27 +05:30
|
|
|
url.URL{
|
|
|
|
Scheme: "https",
|
2016-04-06 23:55:50 +05:30
|
|
|
Host: "client2.example.com",
|
|
|
|
Path: "/callback",
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-09 05:31:44 +05:30
|
|
|
func newClientIdentityRepo(t *testing.T) client.ClientIdentityRepo {
|
2015-08-18 05:57:27 +05:30
|
|
|
dsn := os.Getenv("DEX_TEST_DSN")
|
2016-02-10 04:36:07 +05:30
|
|
|
var dbMap *gorp.DbMap
|
2015-08-18 05:57:27 +05:30
|
|
|
if dsn == "" {
|
2016-02-10 04:36:07 +05:30
|
|
|
dbMap = db.NewMemDB()
|
|
|
|
} else {
|
|
|
|
dbMap = connect(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2016-02-09 05:31:44 +05:30
|
|
|
repo, err := db.NewClientIdentityRepoFromClients(dbMap, testClients)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create client repo from clients: %v", err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2016-02-09 05:31:44 +05:30
|
|
|
return repo
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSetAdminClient(t *testing.T) {
|
|
|
|
startAdmins := []string{"client2"}
|
|
|
|
tests := []struct {
|
|
|
|
// client ID
|
|
|
|
cid string
|
|
|
|
|
|
|
|
// initial state of client
|
|
|
|
wantAdmin bool
|
|
|
|
|
|
|
|
// final state of client
|
|
|
|
setAdmin bool
|
|
|
|
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cid: "client1",
|
|
|
|
wantAdmin: false,
|
|
|
|
setAdmin: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cid: "client1",
|
|
|
|
wantAdmin: false,
|
|
|
|
setAdmin: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cid: "client2",
|
|
|
|
wantAdmin: true,
|
|
|
|
setAdmin: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cid: "client2",
|
|
|
|
wantAdmin: true,
|
|
|
|
setAdmin: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-09 05:31:44 +05:30
|
|
|
Tests:
|
2015-08-18 05:57:27 +05:30
|
|
|
for i, tt := range tests {
|
2016-02-09 05:31:44 +05:30
|
|
|
repo := newClientIdentityRepo(t)
|
2015-08-18 05:57:27 +05:30
|
|
|
for _, cid := range startAdmins {
|
|
|
|
err := repo.SetDexAdmin(cid, true)
|
|
|
|
if err != nil {
|
2016-02-09 05:31:44 +05:30
|
|
|
t.Errorf("case %d: failed to set dex admin: %v", i, err)
|
|
|
|
continue Tests
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gotAdmin, err := repo.IsDexAdmin(tt.cid)
|
|
|
|
if tt.wantErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: want non-nil err", i)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-02-09 05:31:44 +05:30
|
|
|
t.Errorf("case %d: unexpected error: %v", i, err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
if gotAdmin != tt.wantAdmin {
|
|
|
|
t.Errorf("case %d: want=%v, got=%v", i, tt.wantAdmin, gotAdmin)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.SetDexAdmin(tt.cid, tt.setAdmin)
|
|
|
|
if err != nil {
|
2016-02-09 05:31:44 +05:30
|
|
|
t.Errorf("case %d: unexpected error: %v", i, err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
gotAdmin, err = repo.IsDexAdmin(tt.cid)
|
|
|
|
if err != nil {
|
2016-02-09 05:31:44 +05:30
|
|
|
t.Errorf("case %d: unexpected error: %v", i, err)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
if gotAdmin != tt.setAdmin {
|
|
|
|
t.Errorf("case %d: want=%v, got=%v", i, tt.setAdmin, gotAdmin)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|