2018-11-14 23:31:31 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2018-11-15 16:12:28 +00:00
|
|
|
"fmt"
|
2018-11-14 23:31:31 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
2018-11-15 16:12:28 +00:00
|
|
|
"strings"
|
2018-11-14 23:31:31 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/dexidp/dex/connector"
|
|
|
|
)
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
type testResponse struct {
|
|
|
|
data interface{}
|
|
|
|
nextLink string
|
|
|
|
lastLink string
|
|
|
|
}
|
2018-11-14 23:31:31 +00:00
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
func TestUserGroups(t *testing.T) {
|
|
|
|
s := newTestServer(map[string]testResponse{
|
|
|
|
"/user/orgs": {
|
|
|
|
data: []org{{Login: "org-1"}, {Login: "org-2"}},
|
|
|
|
nextLink: "/user/orgs?since=2",
|
|
|
|
lastLink: "/user/orgs?since=2",
|
|
|
|
},
|
|
|
|
"/user/orgs?since=2": {data: []org{{Login: "org-3"}}},
|
|
|
|
"/user/teams": {
|
|
|
|
data: []team{
|
|
|
|
{Name: "team-1", Org: org{Login: "org-1"}},
|
|
|
|
{Name: "team-2", Org: org{Login: "org-1"}},
|
|
|
|
},
|
|
|
|
nextLink: "/user/teams?since=2",
|
|
|
|
lastLink: "/user/teams?since=2",
|
|
|
|
},
|
|
|
|
"/user/teams?since=2": {
|
|
|
|
data: []team{
|
|
|
|
{Name: "team-3", Org: org{Login: "org-1"}},
|
|
|
|
{Name: "team-4", Org: org{Login: "org-2"}},
|
|
|
|
},
|
|
|
|
nextLink: "/user/teams?since=2",
|
|
|
|
lastLink: "/user/teams?since=2",
|
|
|
|
},
|
2018-11-14 23:31:31 +00:00
|
|
|
})
|
2018-11-15 16:12:28 +00:00
|
|
|
defer s.Close()
|
2018-11-14 23:31:31 +00:00
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
c := githubConnector{apiURL: s.URL}
|
|
|
|
groups, err := c.userGroups(context.Background(), newClient())
|
2018-11-14 23:31:31 +00:00
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, groups, []string{
|
2018-11-15 22:08:15 +00:00
|
|
|
"org-1",
|
2018-11-14 23:31:31 +00:00
|
|
|
"org-1:team-1",
|
|
|
|
"org-1:team-2",
|
|
|
|
"org-1:team-3",
|
2018-11-15 22:08:15 +00:00
|
|
|
"org-2",
|
2018-11-14 23:31:31 +00:00
|
|
|
"org-2:team-4",
|
|
|
|
"org-3",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserGroupsWithoutOrgs(t *testing.T) {
|
2018-11-15 16:12:28 +00:00
|
|
|
s := newTestServer(map[string]testResponse{
|
|
|
|
"/user/orgs": {data: []org{}},
|
|
|
|
"/user/teams": {data: []team{}},
|
2018-11-14 23:31:31 +00:00
|
|
|
})
|
2018-11-15 16:12:28 +00:00
|
|
|
defer s.Close()
|
2018-11-14 23:31:31 +00:00
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
c := githubConnector{apiURL: s.URL}
|
|
|
|
groups, err := c.userGroups(context.Background(), newClient())
|
2018-11-14 23:31:31 +00:00
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, len(groups), 0)
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:23:57 +00:00
|
|
|
func TestUserGroupsWithTeamNameFieldConfig(t *testing.T) {
|
|
|
|
s := newTestServer(map[string]testResponse{
|
|
|
|
"/user/orgs": {
|
|
|
|
data: []org{{Login: "org-1"}},
|
|
|
|
},
|
|
|
|
"/user/teams": {
|
|
|
|
data: []team{
|
|
|
|
{Name: "Team 1", Slug: "team-1", Org: org{Login: "org-1"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
c := githubConnector{apiURL: s.URL, teamNameField: "slug"}
|
|
|
|
groups, err := c.userGroups(context.Background(), newClient())
|
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, groups, []string{
|
2018-11-15 22:08:15 +00:00
|
|
|
"org-1",
|
2018-11-15 17:23:57 +00:00
|
|
|
"org-1:team-1",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-04 21:22:45 +00:00
|
|
|
func TestUserGroupsWithTeamNameAndSlugFieldConfig(t *testing.T) {
|
|
|
|
s := newTestServer(map[string]testResponse{
|
|
|
|
"/user/orgs": {
|
|
|
|
data: []org{{Login: "org-1"}},
|
|
|
|
},
|
|
|
|
"/user/teams": {
|
|
|
|
data: []team{
|
|
|
|
{Name: "Team 1", Slug: "team-1", Org: org{Login: "org-1"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
c := githubConnector{apiURL: s.URL, teamNameField: "both"}
|
|
|
|
groups, err := c.userGroups(context.Background(), newClient())
|
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, groups, []string{
|
|
|
|
"org-1",
|
|
|
|
"org-1:Team 1",
|
|
|
|
"org-1:team-1",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-14 23:31:31 +00:00
|
|
|
func TestUsernameIncludedInFederatedIdentity(t *testing.T) {
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
s := newTestServer(map[string]testResponse{
|
|
|
|
"/user": {data: user{Login: "some-login"}},
|
|
|
|
"/user/emails": {data: []userEmail{{
|
2018-11-14 23:31:31 +00:00
|
|
|
Email: "some@email.com",
|
|
|
|
Verified: true,
|
|
|
|
Primary: true,
|
2018-11-15 16:12:28 +00:00
|
|
|
}}},
|
|
|
|
"/login/oauth/access_token": {data: map[string]interface{}{
|
2018-11-14 23:31:31 +00:00
|
|
|
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9",
|
|
|
|
"expires_in": "30",
|
2018-11-15 16:12:28 +00:00
|
|
|
}},
|
2018-11-19 18:14:38 +00:00
|
|
|
"/user/orgs": {
|
|
|
|
data: []org{{Login: "org-1"}},
|
|
|
|
},
|
2018-11-14 23:31:31 +00:00
|
|
|
})
|
2018-11-15 16:12:28 +00:00
|
|
|
defer s.Close()
|
2018-11-14 23:31:31 +00:00
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL)
|
|
|
|
expectNil(t, err)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", hostURL.String(), nil)
|
|
|
|
expectNil(t, err)
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
c := githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: newClient()}
|
2018-11-19 18:14:38 +00:00
|
|
|
identity, err := c.HandleCallback(connector.Scopes{Groups: true}, req)
|
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, identity.Username, "some-login")
|
|
|
|
expectEquals(t, 0, len(identity.Groups))
|
|
|
|
|
|
|
|
c = githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: newClient(), loadAllGroups: true}
|
|
|
|
identity, err = c.HandleCallback(connector.Scopes{Groups: true}, req)
|
2018-11-14 23:31:31 +00:00
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, identity.Username, "some-login")
|
2018-11-19 18:14:38 +00:00
|
|
|
expectEquals(t, identity.Groups, []string{"org-1"})
|
2018-11-14 23:31:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
func newTestServer(responses map[string]testResponse) *httptest.Server {
|
|
|
|
var s *httptest.Server
|
|
|
|
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
response := responses[r.RequestURI]
|
|
|
|
linkParts := make([]string, 0)
|
|
|
|
if response.nextLink != "" {
|
|
|
|
linkParts = append(linkParts, fmt.Sprintf("<%s%s>; rel=\"next\"", s.URL, response.nextLink))
|
|
|
|
}
|
|
|
|
if response.lastLink != "" {
|
|
|
|
linkParts = append(linkParts, fmt.Sprintf("<%s%s>; rel=\"last\"", s.URL, response.lastLink))
|
|
|
|
}
|
|
|
|
if len(linkParts) > 0 {
|
|
|
|
w.Header().Add("Link", strings.Join(linkParts, ", "))
|
|
|
|
}
|
2018-11-14 23:31:31 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-11-15 16:12:28 +00:00
|
|
|
json.NewEncoder(w).Encode(response.data)
|
2018-11-14 23:31:31 +00:00
|
|
|
}))
|
2018-11-15 16:12:28 +00:00
|
|
|
return s
|
2018-11-14 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newClient() *http.Client {
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
return &http.Client{Transport: tr}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectNil(t *testing.T, a interface{}) {
|
|
|
|
if a != nil {
|
|
|
|
t.Errorf("Expected %+v to equal nil", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectEquals(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
if !reflect.DeepEqual(a, b) {
|
|
|
|
t.Errorf("Expected %+v to equal %+v", a, b)
|
|
|
|
}
|
|
|
|
}
|