Skip to content

Commit

Permalink
Merge pull request #10 from sergioifg94/MGDAPI-2663
Browse files Browse the repository at this point in the history
Add ability to find group by path
  • Loading branch information
briangallagher authored Oct 11, 2021
2 parents be8cb25 + fa3426e commit 8e862bf
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,48 @@ func (c *Client) FindGroupByName(groupName string, realmName string) (*Group, er
return findInList(groups.([]*Group)), nil
}

func (c *Client) FindGroupByPath(groupPath, realmName string) (*Group, error) {
// Given a path "root/sub1/sub2", convert into ["root", "sub1", "sub2"]
paths := strings.Split(groupPath, "/")

// Find the "root" group
rootGroup, err := c.FindGroupByName(paths[0], realmName)
if err != nil {
return nil, err
}

// If the path is "root", there's no need to keep looking, return the
// group
if len(paths) == 1 {
return rootGroup, nil
}

// Iterate through the subpaths ["sub1", "sub2"] and look for the next
// path in the current group level
currentGroup := rootGroup
for level := 1; level < len(paths); level++ {
currentPath := paths[level]
foundInLevel := false
for _, subGroup := range currentGroup.SubGroups {
if subGroup.Name != currentPath {
continue
}

currentGroup = subGroup
foundInLevel = true
}

// We iterated through all the subgroups and didn't find
// the subpath, return nil as the group is not in the expected
// hierarchy
if !foundInLevel {
return nil, nil
}
}

return currentGroup, nil
}

func (c *Client) CreateGroup(groupName string, realmName string) (string, error) {
group := Group{
Name: groupName,
Expand Down Expand Up @@ -1132,6 +1174,7 @@ type KeycloakInterface interface {
DeleteUserFromGroup(realmName, userID, groupID string) error

FindGroupByName(groupName string, realmName string) (*Group, error)
FindGroupByPath(groupPath, realmName string) (*Group, error)
CreateGroup(group string, realmName string) (string, error)
MakeGroupDefault(groupID string, realmName string) error
ListDefaultGroups(realmName string) ([]*Group, error)
Expand Down
54 changes: 54 additions & 0 deletions pkg/common/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,60 @@ func TestClient_FindGroupByName(t *testing.T) {
testClientHTTPRequest(handle, request)
}

func TestClient_FindGroupByPath(t *testing.T) {
const (
existingGroupName string = "gotcha"
existingGroupID string = "12348"
)
realm := getDummyRealm()

handle := withPathAssertionBody(
t,
200,
fmt.Sprintf(GroupListPath, realm.Spec.Realm.Realm),
[]*Group{
{
ID: "12345",
Name: "root",
SubGroups: []*Group{
{
Name: "skip-me",
ID: "12346",
},
{
Name: "almost-there",
ID: "12347",
SubGroups: []*Group{
{
Name: "gotcha",
ID: "12348",
},
},
},
},
},
},
)

request := func(c *Client) {
// when the group exists
foundGroup, err := c.FindGroupByPath("root/almost-there/gotcha", "dummy")
// then return the group instance
assert.NoError(t, err)
assert.NotNil(t, foundGroup)
assert.Equal(t, existingGroupID, foundGroup.ID)
assert.Equal(t, existingGroupName, foundGroup.Name)

// when the group doesn't exist
notFoundGroup, err := c.FindGroupByName("root/oops/gotcha", "dummy")
// then return `nil`
assert.NoError(t, err)
assert.Nil(t, notFoundGroup)
}

testClientHTTPRequest(handle, request)
}

func TestClient_CreateGroup(t *testing.T) {
realm := getDummyRealm()
const (
Expand Down
49 changes: 49 additions & 0 deletions pkg/common/keycloakClient_moq.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e862bf

Please sign in to comment.