@@ -1,6 +1,7 @@
11package providers
22
33import (
4+ "context"
45 "encoding/base64"
56 "encoding/json"
67 "fmt"
@@ -12,6 +13,7 @@ import (
1213 "github.com/stretchr/testify/assert"
1314
1415 admin "google.golang.org/api/admin/directory/v1"
16+ option "google.golang.org/api/option"
1517)
1618
1719func newRedeemServer(body []byte) (*url.URL, *httptest.Server) {
@@ -185,34 +187,53 @@ func TestGoogleProviderGetEmailAddressEmailMissing(t *testing.T) {
185187
186188func TestGoogleProviderUserInGroup(t *testing.T) {
187189 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
188− if r.URL.Path == "/users/member-by-email@example.com" {
189− fmt.Fprintln(w, "{}")
190− } else if r.URL.Path == "/users/non-member-by-email@example.com" {
191− fmt.Fprintln(w, "{}")
192− } else if r.URL.Path == "/users/member-by-id@example.com" {
193− fmt.Fprintln(w, "{\"id\": \"member-id\"}")
194− } else if r.URL.Path == "/users/non-member-by-id@example.com" {
195− fmt.Fprintln(w, "{\"id\": \"non-member-id\"}")
196− } else if r.URL.Path == "/groups/group@example.com/members" {
197− fmt.Fprintln(w, "{\"members\": [{\"email\": \"member-by-email@example.com\"}, {\"id\": \"member-id\", \"type\": \"USER\"}]}")
190+ if r.URL.Path == "/groups/group@example.com/hasMember/member-in-domain@example.com" {
191+ fmt.Fprintln(w, `{"isMember": true}`)
192+ } else if r.URL.Path == "/groups/group@example.com/hasMember/non-member-in-domain@example.com" {
193+ fmt.Fprintln(w, `{"isMember": false}`)
194+ } else if r.URL.Path == "/groups/group@example.com/hasMember/member-out-of-domain@otherexample.com" {
195+ http.Error(
196+ w,
197+ `{"error": {"errors": [{"domain": "global","reason": "invalid","message": "Invalid Input: memberKey"}],"code": 400,"message": "Invalid Input: memberKey"}}`,
198+ http.StatusBadRequest,
199+ )
200+ } else if r.URL.Path == "/groups/group@example.com/hasMember/non-member-out-of-domain@otherexample.com" {
201+ http.Error(
202+ w,
203+ `{"error": {"errors": [{"domain": "global","reason": "invalid","message": "Invalid Input: memberKey"}],"code": 400,"message": "Invalid Input: memberKey"}}`,
204+ http.StatusBadRequest,
205+ )
206+ } else if r.URL.Path == "/groups/group@example.com/members/non-member-out-of-domain@otherexample.com" {
207+ // note that the client currently doesn't care what this response text or code is - any error here results in failure to match the group
208+ http.Error(
209+ w,
210+ `{"error": {"errors": [{"domain": "global","reason": "notFound","message": "Resource Not Found: memberKey"}],"code": 404,"message": "Resource Not Found: memberKey"}}`,
211+ http.StatusNotFound,
212+ )
213+ } else if r.URL.Path == "/groups/group@example.com/members/member-out-of-domain@otherexample.com" {
214+ fmt.Fprintln(w,
215+ `{"kind": "admin#directory#member","etag":"12345","id":"1234567890","email": "member-out-of-domain@otherexample.com","role": "MEMBER","type": "USER","status": "ACTIVE","delivery_settings": "ALL_MAIL"}}`,
216+ )
198217 }
199218 }))
200219 defer ts.Close()
201220
202221 client := ts.Client()
203− service, err := admin.New(client)
222+ ctx := context.Background()
223+
224+ service, err := admin.NewService(ctx, option.WithHTTPClient(client))
204225 service.BasePath = ts.URL
205226 assert.Equal(t, nil, err)
206227
207− result := userInGroup(service, []string{"group@example.com"}, "member-by-email@example.com")
228+ result := userInGroup(service, []string{"group@example.com"}, "member-in-domain@example.com")
208229 assert.True(t, result)
209230
210− result = userInGroup(service, []string{"group@example.com"}, "member-by-id@example.com")
231+ result = userInGroup(service, []string{"group@example.com"}, "member-out-of-domain@otherexample.com")
211232 assert.True(t, result)
212233
213− result = userInGroup(service, []string{"group@example.com"}, "non-member-by-id@example.com")
234+ result = userInGroup(service, []string{"group@example.com"}, "non-member-in-domain@example.com")
214235 assert.False(t, result)
215236
216− result = userInGroup(service, []string{"group@example.com"}, "non-member-by-email@example.com")
237+ result = userInGroup(service, []string{"group@example.com"}, "non-member-out-of-domain@otherexample.com")
217238 assert.False(t, result)
218239}