|
|
|
|
@ -9,6 +9,7 @@ import (
|
|
|
|
|
"net/url" |
|
|
|
|
"reflect" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus" |
|
|
|
|
"golang.org/x/oauth2" |
|
|
|
|
@ -184,6 +185,78 @@ func TestCallbackIdentity(t *testing.T) {
|
|
|
|
|
expectEquals(t, identity.Groups[0], "users") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRefreshIdentity(t *testing.T) { |
|
|
|
|
s := newTestServer(map[string]interface{}{ |
|
|
|
|
usersURLPath: user{ |
|
|
|
|
ObjectMeta: k8sapi.ObjectMeta{ |
|
|
|
|
Name: "jdoe", |
|
|
|
|
UID: "12345", |
|
|
|
|
}, |
|
|
|
|
FullName: "John Doe", |
|
|
|
|
Groups: []string{"users"}, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
defer s.Close() |
|
|
|
|
|
|
|
|
|
h, err := newHTTPClient(true, "") |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
oc := openshiftConnector{apiURL: s.URL, httpClient: h, oauth2Config: &oauth2.Config{ |
|
|
|
|
Endpoint: oauth2.Endpoint{ |
|
|
|
|
AuthURL: fmt.Sprintf("%s/oauth/authorize", s.URL), |
|
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", s.URL), |
|
|
|
|
}, |
|
|
|
|
}} |
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(oauth2.Token{AccessToken: "fFAGRNJru1FTz70BzhT3Zg"}) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
oldID := connector.Identity{ConnectorData: data} |
|
|
|
|
|
|
|
|
|
identity, err := oc.Refresh(context.Background(), connector.Scopes{Groups: true}, oldID) |
|
|
|
|
|
|
|
|
|
expectNil(t, err) |
|
|
|
|
expectEquals(t, identity.UserID, "12345") |
|
|
|
|
expectEquals(t, identity.Username, "jdoe") |
|
|
|
|
expectEquals(t, identity.PreferredUsername, "jdoe") |
|
|
|
|
expectEquals(t, identity.Email, "jdoe") |
|
|
|
|
expectEquals(t, len(identity.Groups), 1) |
|
|
|
|
expectEquals(t, identity.Groups[0], "users") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestRefreshIdentityFailure(t *testing.T) { |
|
|
|
|
s := newTestServer(map[string]interface{}{ |
|
|
|
|
usersURLPath: user{ |
|
|
|
|
ObjectMeta: k8sapi.ObjectMeta{ |
|
|
|
|
Name: "jdoe", |
|
|
|
|
UID: "12345", |
|
|
|
|
}, |
|
|
|
|
FullName: "John Doe", |
|
|
|
|
Groups: []string{"users"}, |
|
|
|
|
}, |
|
|
|
|
}) |
|
|
|
|
defer s.Close() |
|
|
|
|
|
|
|
|
|
h, err := newHTTPClient(true, "") |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
oc := openshiftConnector{apiURL: s.URL, httpClient: h, oauth2Config: &oauth2.Config{ |
|
|
|
|
Endpoint: oauth2.Endpoint{ |
|
|
|
|
AuthURL: fmt.Sprintf("%s/oauth/authorize", s.URL), |
|
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", s.URL), |
|
|
|
|
}, |
|
|
|
|
}} |
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(oauth2.Token{AccessToken: "oRzxVjCnohYRHEYEhZshkmakKmoyVoTjfUGC", Expiry: time.Now().Add(-time.Hour)}) |
|
|
|
|
expectNil(t, err) |
|
|
|
|
|
|
|
|
|
oldID := connector.Identity{ConnectorData: data} |
|
|
|
|
|
|
|
|
|
identity, err := oc.Refresh(context.Background(), connector.Scopes{Groups: true}, oldID) |
|
|
|
|
expectNotNil(t, err) |
|
|
|
|
expectEquals(t, connector.Identity{}, identity) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newTestServer(responses map[string]interface{}) *httptest.Server { |
|
|
|
|
var s *httptest.Server |
|
|
|
|
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
@ -216,3 +289,9 @@ func expectEquals(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
|
t.Errorf("Expected %+v to equal %+v", a, b) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func expectNotNil(t *testing.T, a interface{}) { |
|
|
|
|
if a == nil { |
|
|
|
|
t.Errorf("Expected %+v to not equal nil", a) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|