Skip to content

Commit

Permalink
Groupにメンバーが存在するかを確認するサンプルを追加 refs #75
Browse files Browse the repository at this point in the history
  • Loading branch information
sinmetal committed Jan 24, 2024
1 parent 62e2972 commit 7d8dd14
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
25 changes: 25 additions & 0 deletions group/v0/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package group

import (
"context"

admin "google.golang.org/api/admin/directory/v1"
)

type Service struct {
svc *admin.Service
}

func NewService(ctx context.Context, svc *admin.Service) (*Service, error) {
return &Service{
svc: svc,
}, nil
}

func (s *Service) HasMember(groupKey string, memberKey string) (bool, error) {
resp, err := s.svc.Members.HasMember(groupKey, memberKey).Do()
if err != nil {
return false, err
}
return resp.IsMember, nil
}
38 changes: 38 additions & 0 deletions group/v0/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package group_test

import (
"context"
"testing"

adts "github.com/apstndb/adcplus/tokensource"
groupbox "github.com/sinmetalcraft/gcpbox/group/v0"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)

func TestService_HasMember(t *testing.T) {
ctx := context.Background()

ts, err := adts.SmartAccessTokenSource(ctx)
if err != nil {
t.Fatal(err)
}

directoryService, err := admin.NewService(ctx, option.WithTokenSource(ts), option.WithQuotaProject("sinmetal-ci"))
if err != nil {
t.Fatal(err)
}

s, err := groupbox.NewService(ctx, directoryService)
if err != nil {
t.Fatal(err)
}

ok, err := s.HasMember("group-test-tier1@sinmetalcraft.jp", "sinmetal@sinmetalcraft.jp")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Error("has not member")
}
}

0 comments on commit 7d8dd14

Please sign in to comment.