Skip to content

Commit

Permalink
NOISSUE - Add group service tests (#241)
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo authored Jan 10, 2024
1 parent 2fb14e2 commit e8bf29d
Show file tree
Hide file tree
Showing 12 changed files with 3,314 additions and 156 deletions.
107 changes: 0 additions & 107 deletions internal/groups/mocks/groups.go

This file was deleted.

26 changes: 9 additions & 17 deletions internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (svc service) CreateGroup(ctx context.Context, token, kind string, g groups
})
}
if _, err := svc.auth.AddPolicies(ctx, &policies); err != nil {
return groups.Group{}, err
return g, err
}

return g, nil
Expand All @@ -125,14 +125,7 @@ func (svc service) ViewGroupPerms(ctx context.Context, token, id string) ([]stri
return nil, err
}

permissions, err := svc.listUserGroupPermission(ctx, res.GetId(), id)
if err != nil {
return nil, err
}
if len(permissions) == 0 {
return nil, errors.ErrAuthorization
}
return permissions, nil
return svc.listUserGroupPermission(ctx, res.GetId(), id)
}

func (svc service) ListGroups(ctx context.Context, token, memberKind, memberID string, gm groups.Page) (groups.Page, error) {
Expand Down Expand Up @@ -215,12 +208,8 @@ func (svc service) ListGroups(ctx context.Context, token, memberKind, memberID s
return groups.Page{}, err
}
default:
err := svc.checkSuperAdmin(ctx, res.GetUserId())
switch {
case err == nil:
if res.GetDomainId() == "" {
return groups.Page{}, errors.ErrMalformedEntity
}
switch svc.checkSuperAdmin(ctx, res.GetUserId()) {
case nil:
gm.PageMeta.OwnerID = res.GetDomainId()
default:
// If domain is disabled , then this authorization will fail for all non-admin domain users
Expand Down Expand Up @@ -280,6 +269,9 @@ func (svc service) listUserGroupPermission(ctx context.Context, userID, groupID
if err != nil {
return []string{}, err
}
if len(lp.GetPermissions()) == 0 {
return []string{}, errors.ErrAuthorization
}
return lp.GetPermissions(), nil
}

Expand Down Expand Up @@ -505,7 +497,7 @@ func (svc service) assignParentGroup(ctx context.Context, domain, parentGroupID
return svc.groups.AssignParentGroup(ctx, parentGroupID, groupIDs...)
}

func (svc service) unassignParentGroup(ctx context.Context, domain, parentGroupID string, groupIDs []string) error {
func (svc service) unassignParentGroup(ctx context.Context, domain, parentGroupID string, groupIDs []string) (err error) {
groupsPage, err := svc.groups.RetrieveByIDs(ctx, groups.Page{PageMeta: groups.PageMeta{Limit: 1<<63 - 1}}, groupIDs...)
if err != nil {
return errors.Wrap(errRetrieveGroups, err)
Expand All @@ -517,7 +509,7 @@ func (svc service) unassignParentGroup(ctx context.Context, domain, parentGroupI
var deletePolicies magistrala.DeletePoliciesReq
for _, group := range groupsPage.Groups {
if group.Parent != "" && group.Parent != parentGroupID {
return fmt.Errorf("%s group doesn't have same parent", group.ID)
return errors.Wrap(errors.ErrConflict, fmt.Errorf("%s group doesn't have same parent", group.ID))
}
addPolicies.AddPoliciesReq = append(addPolicies.AddPoliciesReq, &magistrala.AddPolicyReq{
Domain: domain,
Expand Down
Loading

0 comments on commit e8bf29d

Please sign in to comment.