Skip to content

Commit

Permalink
MG-1981 - Fix bug on list groups wth subject returning subject group. (
Browse files Browse the repository at this point in the history
…#2148)

Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru authored Apr 16, 2024
1 parent f334ee2 commit 853dab3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
95 changes: 94 additions & 1 deletion internal/groups/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,41 @@ func TestUpdateGroupEndpoint(t *testing.T) {

func TestListGroupsEndpoint(t *testing.T) {
svc := new(mocks.Service)
childGroup := groups.Group{
ID: testsutil.GenerateUUID(t),
Name: valid,
Description: valid,
Domain: testsutil.GenerateUUID(t),
Parent: validGroupResp.ID,
Metadata: clients.Metadata{
"name": "test",
},
Level: -1,
Children: []*groups.Group{},
CreatedAt: time.Now().Add(-1 * time.Second),
UpdatedAt: time.Now(),
UpdatedBy: testsutil.GenerateUUID(t),
Status: clients.EnabledStatus,
}
parentGroup := groups.Group{
ID: testsutil.GenerateUUID(t),
Name: valid,
Description: valid,
Domain: testsutil.GenerateUUID(t),
Metadata: clients.Metadata{
"name": "test",
},
Level: 1,
Children: []*groups.Group{},
CreatedAt: time.Now().Add(-1 * time.Second),
UpdatedAt: time.Now(),
UpdatedBy: testsutil.GenerateUUID(t),
Status: clients.EnabledStatus,
}

validGroupResp.Children = append(validGroupResp.Children, &childGroup)
parentGroup.Children = append(parentGroup.Children, &validGroupResp)

cases := []struct {
desc string
memberKind string
Expand Down Expand Up @@ -542,7 +577,7 @@ func TestListGroupsEndpoint(t *testing.T) {
memberID: testsutil.GenerateUUID(t),
},
svcResp: groups.Page{
Groups: []groups.Group{validGroupResp},
Groups: []groups.Group{validGroupResp, childGroup},
},
svcErr: nil,
resp: groupPageRes{
Expand All @@ -554,6 +589,64 @@ func TestListGroupsEndpoint(t *testing.T) {
},
err: nil,
},
{
desc: "list children groups successfully without tree",
memberKind: auth.UsersKind,
req: listGroupsReq{
Page: groups.Page{
PageMeta: groups.PageMeta{
Limit: 10,
},
ID: validGroupResp.ID,
Direction: -1,
},
tree: false,
token: valid,
memberKind: auth.UsersKind,
memberID: testsutil.GenerateUUID(t),
},
svcResp: groups.Page{
Groups: []groups.Group{validGroupResp, childGroup},
},
svcErr: nil,
resp: groupPageRes{
Groups: []viewGroupRes{
{
Group: childGroup,
},
},
},
err: nil,
},
{
desc: "list parent group successfully without tree",
memberKind: auth.UsersKind,
req: listGroupsReq{
Page: groups.Page{
PageMeta: groups.PageMeta{
Limit: 10,
},
ID: validGroupResp.ID,
Direction: 1,
},
tree: false,
token: valid,
memberKind: auth.UsersKind,
memberID: testsutil.GenerateUUID(t),
},
svcResp: groups.Page{
Groups: []groups.Group{parentGroup, validGroupResp},
},
svcErr: nil,
resp: groupPageRes{
Groups: []viewGroupRes{
{
Group: parentGroup,
},
},
},
err: nil,
},
{
desc: "unsuccessfully with invalid request",
memberKind: auth.ThingsKind,
Expand Down
8 changes: 6 additions & 2 deletions internal/groups/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ func ListGroupsEndpoint(svc groups.Service, memberKind string) endpoint.Endpoint
if req.tree {
return buildGroupsResponseTree(page), nil
}
filterByID := req.Page.ID != ""

return buildGroupsResponse(page), nil
return buildGroupsResponse(page, filterByID), nil
}
}

Expand Down Expand Up @@ -262,7 +263,7 @@ func toViewGroupRes(group groups.Group) viewGroupRes {
return view
}

func buildGroupsResponse(gp groups.Page) groupPageRes {
func buildGroupsResponse(gp groups.Page, filterByID bool) groupPageRes {
res := groupPageRes{
pageRes: pageRes{
Total: gp.Total,
Expand All @@ -275,6 +276,9 @@ func buildGroupsResponse(gp groups.Page) groupPageRes {
view := viewGroupRes{
Group: group,
}
if filterByID && group.Level == 0 {
continue
}
res.Groups = append(res.Groups, view)
}

Expand Down

0 comments on commit 853dab3

Please sign in to comment.