Skip to content

Commit

Permalink
Prepare service types for testing
Browse files Browse the repository at this point in the history
- Stub out clientErrCheckFunc for testing so that returned errors
  from fake client can be checked easily.
- Add methods to create admin and group services by taking in custom
  clients and clientErrCheckFunc

Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
  • Loading branch information
MadhavJivrajani committed Oct 12, 2021
1 parent 33541d4 commit 94abbfe
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ func NewAdminService(ctx context.Context, clientOption option.ClientOption) (Adm
return nil, err
}

return &adminService{client: client}, nil
return NewAdminServiceWithClient(client)
}

func NewAdminServiceWithClient(client AdminServiceClient) (AdminService, error) {
return &adminService{
client: client,
checkForAPIErr404: errFunc,
}, nil
}

func NewGroupService(ctx context.Context, clientOption option.ClientOption) (GroupService, error) {
Expand All @@ -77,11 +84,31 @@ func NewGroupService(ctx context.Context, clientOption option.ClientOption) (Gro
return nil, err
}

return &groupService{client: client}, nil
return NewGroupServiceWithClient(client)
}

func NewGroupServiceWithClient(client GroupServiceClient) (GroupService, error) {
return &groupService{
client: client,
checkForAPIErr404: errFunc,
}, nil
}

// clientErrCheckFunc is stubbed out here for testing
// purposes in order to implement custome error checking
// logic. This function is used to check errors returned
// by the client. The boolean return signifies if the
// check for the error passed or not.
type clientErrCheckFunc func(error) bool

var errFunc clientErrCheckFunc = func(err error) bool {
apierr, ok := err.(*googleapi.Error)
return ok && apierr.Code == http.StatusNotFound
}

type adminService struct {
client AdminServiceClient
client AdminServiceClient
checkForAPIErr404 clientErrCheckFunc
}

// AddOrUpdateGroupMembers first lists all members that are part of group. Based on this list and the
Expand All @@ -94,7 +121,7 @@ func (as *adminService) AddOrUpdateGroupMembers(group GoogleGroup, role string,

l, err := as.client.ListMembers(group.EmailId)
if err != nil {
if apierr, ok := err.(*googleapi.Error); ok && apierr.Code == http.StatusNotFound {
if as.checkForAPIErr404(err) {
log.Printf("skipping adding members to group %q as it has not yet been created\n", group.EmailId)
return nil
}
Expand Down Expand Up @@ -160,7 +187,7 @@ func (as *adminService) CreateOrUpdateGroupIfNescessary(group GoogleGroup) error

grp, err := as.client.GetGroup(group.EmailId)
if err != nil {
if apierr, ok := err.(*googleapi.Error); ok && apierr.Code == http.StatusNotFound {
if as.checkForAPIErr404(err) {
if !config.ConfirmChanges {
log.Printf("dry-run: would create group %q\n", group.EmailId)
} else {
Expand Down Expand Up @@ -262,7 +289,7 @@ func (as *adminService) RemoveOwnerOrManagersFromGroup(group GoogleGroup, member
}
l, err := as.client.ListMembers(group.EmailId)
if err != nil {
if apierr, ok := err.(*googleapi.Error); ok && apierr.Code == http.StatusNotFound {
if as.checkForAPIErr404(err) {
log.Printf("skipping removing members group %q as group has not yet been created\n", group.EmailId)
return nil
}
Expand Down Expand Up @@ -313,7 +340,7 @@ func (as *adminService) RemoveMembersFromGroup(group GoogleGroup, members []stri
}
l, err := as.client.ListMembers(group.EmailId)
if err != nil {
if apierr, ok := err.(*googleapi.Error); ok && apierr.Code == http.StatusNotFound {
if as.checkForAPIErr404(err) {
log.Printf("skipping removing members group %q as group has not yet been created\n", group.EmailId)
return nil
}
Expand Down Expand Up @@ -363,7 +390,8 @@ func (as *adminService) ListMembers(groupKey string) (*admin.Members, error) {
var _ AdminService = (*adminService)(nil)

type groupService struct {
client GroupServiceClient
client GroupServiceClient
checkForAPIErr404 clientErrCheckFunc
}

// UpdateGroupSettings updates the groupsettings.Groups corresponding to the
Expand All @@ -374,7 +402,7 @@ func (gs *groupService) UpdateGroupSettings(group GoogleGroup) error {
}
g2, err := gs.client.Get(group.EmailId)
if err != nil {
if apierr, ok := err.(*googleapi.Error); ok && apierr.Code == http.StatusNotFound {
if gs.checkForAPIErr404(err) {
log.Printf("skipping updating group settings as group %q has not yet been created\n", group.EmailId)
return nil
}
Expand Down

0 comments on commit 94abbfe

Please sign in to comment.