2018-11-15 05:01:31 +05:30
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2018-11-15 21:42:28 +05:30
|
|
|
"fmt"
|
2018-11-15 05:01:31 +05:30
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
2018-11-15 21:42:28 +05:30
|
|
|
"strings"
|
2018-11-15 05:01:31 +05:30
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/dexidp/dex/connector"
|
|
|
|
)
|
|
|
|
|
2018-11-15 21:42:28 +05:30
|
|
|
type testResponse struct {
|
|
|
|
data interface{}
|
|
|
|
nextLink string
|
|
|
|
lastLink string
|
|
|
|
}
|
2018-11-15 05:01:31 +05:30
|
|
|
|
2018-11-15 21:42:28 +05:30
|
|
|
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-15 05:01:31 +05:30
|
|
|
})
|
2018-11-15 21:42:28 +05:30
|
|
|
defer s.Close()
|
2018-11-15 05:01:31 +05:30
|
|
|
|
2018-11-15 21:42:28 +05:30
|
|
|
c := githubConnector{apiURL: s.URL}
|
|
|
|
groups, err := c.userGroups(context.Background(), newClient())
|
2018-11-15 05:01:31 +05:30
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, groups, []string{
|
2018-11-16 03:38:15 +05:30
|
|
|
"org-1",
|
2018-11-15 05:01:31 +05:30
|
|
|
"org-1:team-1",
|
|
|
|
"org-1:team-2",
|
|
|
|
"org-1:team-3",
|
2018-11-16 03:38:15 +05:30
|
|
|
"org-2",
|
2018-11-15 05:01:31 +05:30
|
|
|
"org-2:team-4",
|
|
|
|
"org-3",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserGroupsWithoutOrgs(t *testing.T) {
|
2018-11-15 21:42:28 +05:30
|
|
|
s := newTestServer(map[string]testResponse{
|
|
|
|
"/user/orgs": {data: []org{}},
|
|
|
|
"/user/teams": {data: []team{}},
|
2018-11-15 05:01:31 +05:30
|
|
|
})
|
2018-11-15 21:42:28 +05:30
|
|
|
defer s.Close()
|
2018-11-15 05:01:31 +05:30
|
|
|
|
2018-11-15 21:42:28 +05:30
|
|
|
c := githubConnector{apiURL: s.URL}
|
|
|
|
groups, err := c.userGroups(context.Background(), newClient())
|
2018-11-15 05:01:31 +05:30
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, len(groups), 0)
|
|
|
|
}
|
|
|
|
|
2018-11-15 22:53:57 +05:30
|
|
|
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-16 03:38:15 +05:30
|
|
|
"org-1",
|
2018-11-15 22:53:57 +05:30
|
|
|
"org-1:team-1",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-05 02:52:45 +05:30
|
|
|
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-15 05:01:31 +05:30
|
|
|
func TestUsernameIncludedInFederatedIdentity(t *testing.T) {
|
|
|
|
|
2018-11-15 21:42:28 +05:30
|
|
|
s := newTestServer(map[string]testResponse{
|
|
|
|
"/user": {data: user{Login: "some-login"}},
|
|
|
|
"/user/emails": {data: []userEmail{{
|
2018-11-15 05:01:31 +05:30
|
|
|
Email: "some@email.com",
|
|
|
|
Verified: true,
|
|
|
|
Primary: true,
|
2018-11-15 21:42:28 +05:30
|
|
|
}}},
|
|
|
|
"/login/oauth/access_token": {data: map[string]interface{}{
|
2018-11-15 05:01:31 +05:30
|
|
|
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9",
|
|
|
|
"expires_in": "30",
|
2018-11-15 21:42:28 +05:30
|
|
|
}},
|
2018-11-19 23:44:38 +05:30
|
|
|
"/user/orgs": {
|
|
|
|
data: []org{{Login: "org-1"}},
|
|
|
|
},
|
2018-11-15 05:01:31 +05:30
|
|
|
})
|
2018-11-15 21:42:28 +05:30
|
|
|
defer s.Close()
|
2018-11-15 05:01:31 +05:30
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL)
|
|
|
|
expectNil(t, err)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", hostURL.String(), nil)
|
|
|
|
expectNil(t, err)
|
|
|
|
|
2018-11-15 21:42:28 +05:30
|
|
|
c := githubConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: newClient()}
|
2018-11-19 23:44:38 +05:30
|
|
|
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-15 05:01:31 +05:30
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, identity.Username, "some-login")
|
2018-11-19 23:44:38 +05:30
|
|
|
expectEquals(t, identity.Groups, []string{"org-1"})
|
2018-11-15 05:01:31 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:42:28 +05:30
|
|
|
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-15 05:01:31 +05:30
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-11-15 21:42:28 +05:30
|
|
|
json.NewEncoder(w).Encode(response.data)
|
2018-11-15 05:01:31 +05:30
|
|
|
}))
|
2018-11-15 21:42:28 +05:30
|
|
|
return s
|
2018-11-15 05:01:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|