Add Github OAuth2 principal email/org tests
parent
9a0b4d6251
commit
06d314598d
|
@ -0,0 +1,113 @@
|
|||
package oauth2_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
clog "github.com/influxdata/chronograf/log"
|
||||
"github.com/influxdata/chronograf/oauth2"
|
||||
)
|
||||
|
||||
func TestGithubPrincipalID(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expected := []struct {
|
||||
Email string `json:"email"`
|
||||
Primary bool `json:"primary"`
|
||||
Verified bool `json:"verified"`
|
||||
}{
|
||||
{"martymcfly@example.com", true, false},
|
||||
}
|
||||
mockAPI := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/user/emails" {
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
enc := json.NewEncoder(rw)
|
||||
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
_ = enc.Encode(expected)
|
||||
}))
|
||||
defer mockAPI.Close()
|
||||
|
||||
logger := clog.New(clog.ParseLevel("debug"))
|
||||
prov := oauth2.Github{
|
||||
Logger: logger,
|
||||
}
|
||||
tt, err := NewTestTripper(logger, mockAPI, http.DefaultTransport)
|
||||
if err != nil {
|
||||
t.Fatal("Error initializing TestTripper: err:", err)
|
||||
}
|
||||
|
||||
tc := &http.Client{
|
||||
Transport: tt,
|
||||
}
|
||||
|
||||
email, err := prov.PrincipalID(tc)
|
||||
if err != nil {
|
||||
t.Fatal("Unexpected error while retrieiving PrincipalID: err:", err)
|
||||
}
|
||||
|
||||
if email != expected[0].Email {
|
||||
t.Fatal("Retrieved email was not as expected. Want:", expected[0].Email, "Got:", email)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGithubPrincipalIDOrganization(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expectedUser := []struct {
|
||||
Email string `json:"email"`
|
||||
Primary bool `json:"primary"`
|
||||
Verified bool `json:"verified"`
|
||||
}{
|
||||
{"martymcfly@example.com", true, false},
|
||||
}
|
||||
expectedOrg := []struct {
|
||||
Login string `json:"login"`
|
||||
}{
|
||||
{"Hill Valley Preservation Society"},
|
||||
}
|
||||
|
||||
mockAPI := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/user/emails" {
|
||||
enc := json.NewEncoder(rw)
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
_ = enc.Encode(expectedUser)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/user/orgs" {
|
||||
enc := json.NewEncoder(rw)
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
_ = enc.Encode(expectedOrg)
|
||||
return
|
||||
}
|
||||
rw.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer mockAPI.Close()
|
||||
|
||||
logger := clog.New(clog.ParseLevel("debug"))
|
||||
prov := oauth2.Github{
|
||||
Logger: logger,
|
||||
Orgs: []string{"Hill Valley Preservation Society"},
|
||||
}
|
||||
tt, err := NewTestTripper(logger, mockAPI, http.DefaultTransport)
|
||||
if err != nil {
|
||||
t.Fatal("Error initializing TestTripper: err:", err)
|
||||
}
|
||||
|
||||
tc := &http.Client{
|
||||
Transport: tt,
|
||||
}
|
||||
|
||||
email, err := prov.PrincipalID(tc)
|
||||
if err != nil {
|
||||
t.Fatal("Unexpected error while retrieiving PrincipalID: err:", err)
|
||||
}
|
||||
|
||||
if email != expectedUser[0].Email {
|
||||
t.Fatal("Retrieved email was not as expected. Want:", expectedUser[0].Email, "Got:", email)
|
||||
}
|
||||
}
|
|
@ -4,34 +4,12 @@ import (
|
|||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
clog "github.com/influxdata/chronograf/log"
|
||||
"github.com/influxdata/chronograf/oauth2"
|
||||
)
|
||||
|
||||
func NewTestTripper(ts *httptest.Server, rt http.RoundTripper) (*TestTripper, error) {
|
||||
url, err := url.Parse(ts.URL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TestTripper{rt, url}, nil
|
||||
}
|
||||
|
||||
type TestTripper struct {
|
||||
rt http.RoundTripper
|
||||
tsURL *url.URL
|
||||
}
|
||||
|
||||
// RoundTrip modifies the Hostname of the incoming request to be directed to the
|
||||
// test server.
|
||||
func (tt *TestTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
r.URL.Host = tt.tsURL.Host
|
||||
r.URL.Scheme = tt.tsURL.Scheme
|
||||
|
||||
return tt.rt.RoundTrip(r)
|
||||
}
|
||||
|
||||
func Test_Heroku_PrincipalID_ExtractsEmailAddress(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -53,8 +31,11 @@ func Test_Heroku_PrincipalID_ExtractsEmailAddress(t *testing.T) {
|
|||
}))
|
||||
defer mockAPI.Close()
|
||||
|
||||
prov := oauth2.Heroku{}
|
||||
tt, err := NewTestTripper(mockAPI, http.DefaultTransport)
|
||||
logger := clog.New(clog.ParseLevel("debug"))
|
||||
prov := oauth2.Heroku{
|
||||
Logger: logger,
|
||||
}
|
||||
tt, err := NewTestTripper(logger, mockAPI, http.DefaultTransport)
|
||||
if err != nil {
|
||||
t.Fatal("Error initializing TestTripper: err:", err)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package oauth2_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
|
||||
"github.com/influxdata/chronograf"
|
||||
)
|
||||
|
||||
func NewTestTripper(log chronograf.Logger, ts *httptest.Server, rt http.RoundTripper) (*TestTripper, error) {
|
||||
url, err := url.Parse(ts.URL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TestTripper{log, rt, url}, nil
|
||||
}
|
||||
|
||||
type TestTripper struct {
|
||||
Log chronograf.Logger
|
||||
|
||||
rt http.RoundTripper
|
||||
tsURL *url.URL
|
||||
}
|
||||
|
||||
// RoundTrip modifies the Hostname of the incoming request to be directed to the
|
||||
// test server.
|
||||
func (tt *TestTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
tt.Log.
|
||||
WithField("component", "test").
|
||||
WithField("remote_addr", r.RemoteAddr).
|
||||
WithField("method", r.Method).
|
||||
WithField("url", r.URL).
|
||||
Info("Request")
|
||||
|
||||
r.URL.Host = tt.tsURL.Host
|
||||
r.URL.Scheme = tt.tsURL.Scheme
|
||||
|
||||
return tt.rt.RoundTrip(r)
|
||||
}
|
Loading…
Reference in New Issue