-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revert google.go prior to adding my own tests and add better ones
Signed-off-by: Matt Hoey <[email protected]>
- Loading branch information
Showing
2 changed files
with
131 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package google | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
@@ -10,17 +11,39 @@ import ( | |
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
|
||
admin "google.golang.org/api/admin/directory/v1" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
var ( | ||
// groups_0 | ||
// ┌───────┤ | ||
// groups_2 groups_1 | ||
// │ ├────────┐ | ||
// └── user_1 user_2 | ||
testGroups = map[string][]*admin.Group{ | ||
"[email protected]": {{Email: "[email protected]"}, {Email: "[email protected]"}}, | ||
"[email protected]": {{Email: "[email protected]"}}, | ||
"[email protected]": {{Email: "[email protected]"}}, | ||
"[email protected]": {{Email: "[email protected]"}}, | ||
"[email protected]": {}, | ||
} | ||
callCounter = make(map[string]int) | ||
) | ||
|
||
func testSetup(t *testing.T) *httptest.Server { | ||
mux := http.NewServeMux() | ||
// TODO: mock calls | ||
// mux.HandleFunc("/admin/directory/v1/groups", func(w http.ResponseWriter, r *http.Request) { | ||
// w.Header().Add("Content-Type", "application/json") | ||
// json.NewEncoder(w).Encode(&admin.Groups{ | ||
// Groups: []*admin.Group{}, | ||
// }) | ||
// }) | ||
|
||
mux.HandleFunc("/admin/directory/v1/groups/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Content-Type", "application/json") | ||
userKey := r.URL.Query().Get("userKey") | ||
if groups, ok := testGroups[userKey]; ok { | ||
json.NewEncoder(w).Encode(admin.Groups{Groups: groups}) | ||
callCounter[userKey]++ | ||
} | ||
}) | ||
|
||
return httptest.NewServer(mux) | ||
} | ||
|
||
|
@@ -143,3 +166,73 @@ func TestOpen(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestGetGroups(t *testing.T) { | ||
ts := testSetup(t) | ||
defer ts.Close() | ||
|
||
serviceAccountFilePath, err := tempServiceAccountKey() | ||
assert.Nil(t, err) | ||
|
||
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", serviceAccountFilePath) | ||
conn, err := newConnector(&Config{ | ||
ClientID: "testClient", | ||
ClientSecret: "testSecret", | ||
RedirectURI: ts.URL + "/callback", | ||
Scopes: []string{"openid", "groups"}, | ||
AdminEmail: "[email protected]", | ||
}, ts.URL) | ||
assert.Nil(t, err) | ||
|
||
conn.adminSrv, err = admin.NewService(context.Background(), option.WithoutAuthentication(), option.WithEndpoint(ts.URL)) | ||
assert.Nil(t, err) | ||
type testCase struct { | ||
userKey string | ||
fetchTransitiveGroupMembership bool | ||
shouldErr bool | ||
expectedGroups []string | ||
} | ||
|
||
for name, testCase := range map[string]testCase{ | ||
"user1_non_transitive_lookup": { | ||
userKey: "[email protected]", | ||
fetchTransitiveGroupMembership: false, | ||
shouldErr: false, | ||
expectedGroups: []string{"[email protected]", "[email protected]"}, | ||
}, | ||
"user1_transitive_lookup": { | ||
userKey: "[email protected]", | ||
fetchTransitiveGroupMembership: true, | ||
shouldErr: false, | ||
expectedGroups: []string{"[email protected]", "[email protected]", "[email protected]"}, | ||
}, | ||
"user2_non_transitive_lookup": { | ||
userKey: "[email protected]", | ||
fetchTransitiveGroupMembership: false, | ||
shouldErr: false, | ||
expectedGroups: []string{"[email protected]"}, | ||
}, | ||
"user2_transitive_lookup": { | ||
userKey: "[email protected]", | ||
fetchTransitiveGroupMembership: true, | ||
shouldErr: false, | ||
expectedGroups: []string{"[email protected]", "[email protected]"}, | ||
}, | ||
} { | ||
testCase := testCase | ||
callCounter = map[string]int{} | ||
t.Run(name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
lookup := make(map[string]struct{}) | ||
|
||
groups, err := conn.getGroups(testCase.userKey, testCase.fetchTransitiveGroupMembership, lookup) | ||
if testCase.shouldErr { | ||
assert.NotNil(err) | ||
} else { | ||
assert.Nil(err) | ||
} | ||
assert.ElementsMatch(testCase.expectedGroups, groups) | ||
t.Logf("[%s] Amount of API calls per userKey: %+v\n", t.Name(), callCounter) | ||
}) | ||
} | ||
} |