2018-10-04 21:59:36 +05:30
|
|
|
package bitbucketcloud
|
2018-10-01 00:38:07 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/dexidp/dex/connector"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserGroups(t *testing.T) {
|
2022-01-27 14:08:48 +05:30
|
|
|
teamsResponse := userWorkspacesResponse{
|
2018-10-01 00:38:07 +05:30
|
|
|
pagedResponse: pagedResponse{
|
|
|
|
Size: 3,
|
|
|
|
Page: 1,
|
|
|
|
PageLen: 10,
|
|
|
|
},
|
2022-01-27 14:08:48 +05:30
|
|
|
Values: []workspace{
|
|
|
|
{Workspace: workspaceSlug{Slug: "team-1"}},
|
|
|
|
{Workspace: workspaceSlug{Slug: "team-2"}},
|
|
|
|
{Workspace: workspaceSlug{Slug: "team-3"}},
|
2018-10-01 00:38:07 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s := newTestServer(map[string]interface{}{
|
2022-01-27 14:08:48 +05:30
|
|
|
"/user/permissions/workspaces": teamsResponse,
|
|
|
|
"/groups/team-1": []group{{Slug: "administrators"}, {Slug: "members"}},
|
|
|
|
"/groups/team-2": []group{{Slug: "everyone"}},
|
|
|
|
"/groups/team-3": []group{},
|
2018-10-01 00:38:07 +05:30
|
|
|
})
|
|
|
|
|
2020-04-16 17:44:43 +05:30
|
|
|
connector := bitbucketConnector{apiURL: s.URL, legacyAPIURL: s.URL}
|
2022-01-27 14:08:48 +05:30
|
|
|
groups, err := connector.userWorkspaces(context.Background(), newClient())
|
2018-10-01 00:38:07 +05:30
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, groups, []string{
|
|
|
|
"team-1",
|
|
|
|
"team-2",
|
|
|
|
"team-3",
|
|
|
|
})
|
|
|
|
|
2020-04-16 17:44:43 +05:30
|
|
|
connector.includeTeamGroups = true
|
2022-01-27 14:08:48 +05:30
|
|
|
groups, err = connector.userWorkspaces(context.Background(), newClient())
|
2020-04-16 17:44:43 +05:30
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, groups, []string{
|
|
|
|
"team-1",
|
|
|
|
"team-2",
|
|
|
|
"team-3",
|
|
|
|
"team-1/administrators",
|
|
|
|
"team-1/members",
|
|
|
|
"team-2/everyone",
|
|
|
|
})
|
|
|
|
|
2018-10-01 00:38:07 +05:30
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserWithoutTeams(t *testing.T) {
|
|
|
|
s := newTestServer(map[string]interface{}{
|
2022-01-27 14:08:48 +05:30
|
|
|
"/user/permissions/workspaces": userWorkspacesResponse{},
|
2018-10-01 00:38:07 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
connector := bitbucketConnector{apiURL: s.URL}
|
2022-01-27 14:08:48 +05:30
|
|
|
groups, err := connector.userWorkspaces(context.Background(), newClient())
|
2018-10-01 00:38:07 +05:30
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, len(groups), 0)
|
|
|
|
|
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUsernameIncludedInFederatedIdentity(t *testing.T) {
|
|
|
|
s := newTestServer(map[string]interface{}{
|
|
|
|
"/user": user{Username: "some-login"},
|
|
|
|
"/user/emails": userEmailResponse{
|
|
|
|
pagedResponse: pagedResponse{
|
|
|
|
Size: 1,
|
|
|
|
Page: 1,
|
|
|
|
PageLen: 10,
|
|
|
|
},
|
|
|
|
Values: []userEmail{{
|
|
|
|
Email: "some@email.com",
|
|
|
|
IsConfirmed: true,
|
|
|
|
IsPrimary: true,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"/site/oauth2/access_token": map[string]interface{}{
|
|
|
|
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9",
|
|
|
|
"expires_in": "30",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
hostURL, err := url.Parse(s.URL)
|
|
|
|
expectNil(t, err)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", hostURL.String(), nil)
|
|
|
|
expectNil(t, err)
|
|
|
|
|
|
|
|
bitbucketConnector := bitbucketConnector{apiURL: s.URL, hostName: hostURL.Host, httpClient: newClient()}
|
|
|
|
identity, err := bitbucketConnector.HandleCallback(connector.Scopes{}, req)
|
|
|
|
|
|
|
|
expectNil(t, err)
|
|
|
|
expectEquals(t, identity.Username, "some-login")
|
|
|
|
|
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestServer(responses map[string]interface{}) *httptest.Server {
|
|
|
|
return httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(responses[r.URL.String()])
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-10-04 21:59:36 +05:30
|
|
|
t.Fatalf("Expected %+v to equal nil", a)
|
2018-10-01 00:38:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectEquals(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
if !reflect.DeepEqual(a, b) {
|
2018-10-04 21:59:36 +05:30
|
|
|
t.Fatalf("Expected %+v to equal %+v", a, b)
|
2018-10-01 00:38:07 +05:30
|
|
|
}
|
|
|
|
}
|