1
1
package google
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"net/http"
@@ -10,17 +11,38 @@ import (
10
11
11
12
"github.com/sirupsen/logrus"
12
13
"github.com/stretchr/testify/assert"
14
+ admin "google.golang.org/api/admin/directory/v1"
15
+ "google.golang.org/api/option"
16
+ )
17
+
18
+ var (
19
+ // groups_0
20
+ // ┌───────┤
21
+ // groups_2 groups_1
22
+ // │ ├────────┐
23
+ // └── user_1 user_2
24
+ testGroups = map [string ][]* admin.Group {
25
+
26
+
27
+
28
+
29
+
30
+ }
31
+ callCounter = make (map [string ]int )
13
32
)
14
33
15
34
func testSetup (t * testing.T ) * httptest.Server {
16
35
mux := http .NewServeMux ()
17
- // TODO: mock calls
18
- // mux.HandleFunc("/admin/directory/v1/groups", func(w http.ResponseWriter, r *http.Request) {
19
- // w.Header().Add("Content-Type", "application/json")
20
- // json.NewEncoder(w).Encode(&admin.Groups{
21
- // Groups: []*admin.Group{},
22
- // })
23
- // })
36
+
37
+ mux .HandleFunc ("/admin/directory/v1/groups/" , func (w http.ResponseWriter , r * http.Request ) {
38
+ w .Header ().Add ("Content-Type" , "application/json" )
39
+ userKey := r .URL .Query ().Get ("userKey" )
40
+ if groups , ok := testGroups [userKey ]; ok {
41
+ json .NewEncoder (w ).Encode (admin.Groups {Groups : groups })
42
+ callCounter [userKey ]++
43
+ }
44
+ })
45
+
24
46
return httptest .NewServer (mux )
25
47
}
26
48
@@ -144,3 +166,73 @@ func TestOpen(t *testing.T) {
144
166
})
145
167
}
146
168
}
169
+
170
+ func TestGetGroups (t * testing.T ) {
171
+ ts := testSetup (t )
172
+ defer ts .Close ()
173
+
174
+ serviceAccountFilePath , err := tempServiceAccountKey ()
175
+ assert .Nil (t , err )
176
+
177
+ os .Setenv ("GOOGLE_APPLICATION_CREDENTIALS" , serviceAccountFilePath )
178
+ conn , err := newConnector (& Config {
179
+ ClientID : "testClient" ,
180
+ ClientSecret : "testSecret" ,
181
+ RedirectURI : ts .URL + "/callback" ,
182
+ Scopes : []string {"openid" , "groups" },
183
+
184
+ }, ts .URL )
185
+ assert .Nil (t , err )
186
+
187
+ conn .adminSrv , err = admin .NewService (context .Background (), option .WithoutAuthentication (), option .WithEndpoint (ts .URL ))
188
+ assert .Nil (t , err )
189
+ type testCase struct {
190
+ userKey string
191
+ fetchTransitiveGroupMembership bool
192
+ shouldErr bool
193
+ expectedGroups []string
194
+ }
195
+
196
+ for name , testCase := range map [string ]testCase {
197
+ "user1_non_transitive_lookup" : {
198
+
199
+ fetchTransitiveGroupMembership : false ,
200
+ shouldErr : false ,
201
+
202
+ },
203
+ "user1_transitive_lookup" : {
204
+
205
+ fetchTransitiveGroupMembership : true ,
206
+ shouldErr : false ,
207
+
208
+ },
209
+ "user2_non_transitive_lookup" : {
210
+
211
+ fetchTransitiveGroupMembership : false ,
212
+ shouldErr : false ,
213
+ expectedGroups : []
string {
"[email protected] " },
214
+ },
215
+ "user2_transitive_lookup" : {
216
+
217
+ fetchTransitiveGroupMembership : true ,
218
+ shouldErr : false ,
219
+
220
+ },
221
+ } {
222
+ testCase := testCase
223
+ callCounter = map [string ]int {}
224
+ t .Run (name , func (t * testing.T ) {
225
+ assert := assert .New (t )
226
+ lookup := make (map [string ]struct {})
227
+
228
+ groups , err := conn .getGroups (testCase .userKey , testCase .fetchTransitiveGroupMembership , lookup )
229
+ if testCase .shouldErr {
230
+ assert .NotNil (err )
231
+ } else {
232
+ assert .Nil (err )
233
+ }
234
+ assert .ElementsMatch (testCase .expectedGroups , groups )
235
+ t .Logf ("[%s] Amount of API calls per userKey: %+v\n " , t .Name (), callCounter )
236
+ })
237
+ }
238
+ }
0 commit comments