mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-04 10:28:10 +02:00
github: implement github directory provider (#963)
* github: implement github directory provider * fix test
This commit is contained in:
parent
b2f6b42798
commit
dbf020a532
4 changed files with 399 additions and 2 deletions
112
internal/directory/github/github_test.go
Normal file
112
internal/directory/github/github_test.go
Normal file
|
@ -0,0 +1,112 @@
|
|||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/testutil"
|
||||
)
|
||||
|
||||
type M = map[string]interface{}
|
||||
|
||||
func newMockAPI(t *testing.T, srv *httptest.Server) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !assert.Equal(t, "Basic YWJjOnh5eg==", r.Header.Get("Authorization")) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
r.Get("/user/orgs", func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode([]M{
|
||||
{"login": "org1"},
|
||||
{"login": "org2"},
|
||||
})
|
||||
})
|
||||
r.Get("/orgs/{org_id}/teams", func(w http.ResponseWriter, r *http.Request) {
|
||||
teams := map[string][]M{
|
||||
"org1": {
|
||||
{"slug": "team1"},
|
||||
{"slug": "team2"},
|
||||
},
|
||||
"org2": {
|
||||
{"slug": "team3"},
|
||||
{"slug": "team4"},
|
||||
},
|
||||
}
|
||||
orgID := chi.URLParam(r, "org_id")
|
||||
json.NewEncoder(w).Encode(teams[orgID])
|
||||
})
|
||||
r.Get("/orgs/{org_id}/teams/{team_id}/members", func(w http.ResponseWriter, r *http.Request) {
|
||||
members := map[string]map[string][]M{
|
||||
"org1": {
|
||||
"team1": {
|
||||
{"login": "user1"},
|
||||
{"login": "user2"},
|
||||
},
|
||||
"team2": {
|
||||
{"login": "user1"},
|
||||
},
|
||||
},
|
||||
"org2": {
|
||||
"team3": {
|
||||
{"login": "user1"},
|
||||
{"login": "user2"},
|
||||
{"login": "user3"},
|
||||
},
|
||||
"team4": {
|
||||
{"login": "user4"},
|
||||
},
|
||||
},
|
||||
}
|
||||
orgID := chi.URLParam(r, "org_id")
|
||||
teamID := chi.URLParam(r, "team_id")
|
||||
json.NewEncoder(w).Encode(members[orgID][teamID])
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
var mockAPI http.Handler
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mockAPI.ServeHTTP(w, r)
|
||||
}))
|
||||
defer srv.Close()
|
||||
mockAPI = newMockAPI(t, srv)
|
||||
|
||||
p := New(
|
||||
WithURL(mustParseURL(srv.URL)),
|
||||
WithServiceAccount(&ServiceAccount{
|
||||
Username: "abc",
|
||||
PersonalAccessToken: "xyz",
|
||||
}),
|
||||
)
|
||||
users, err := p.UserGroups(context.Background())
|
||||
assert.NoError(t, err)
|
||||
testutil.AssertProtoJSONEqual(t, `[
|
||||
{ "id": "github/user1", "groups": ["team1", "team2", "team3"] },
|
||||
{ "id": "github/user2", "groups": ["team1", "team3"] },
|
||||
{ "id": "github/user3", "groups": ["team3"] },
|
||||
{ "id": "github/user4", "groups": ["team4"] }
|
||||
]`, users)
|
||||
}
|
||||
|
||||
func mustParseURL(rawurl string) *url.URL {
|
||||
u, err := url.Parse(rawurl)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return u
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue