Skip to content

Commit

Permalink
Merge pull request #113 from agorman/create-child-group
Browse files Browse the repository at this point in the history
Adding CreateChildGroup method
  • Loading branch information
Nerzal authored Dec 10, 2019
2 parents bf0d593 + f74ffa1 commit 91a456f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type GoCloak interface {

CreateUser(token string, realm string, user User) (string, error)
CreateGroup(accessToken string, realm string, group Group) error
CreateChildGroup(token string, realm string, groupID string, group Group) (string, error)
CreateClientRole(accessToken string, realm string, clientID string, role Role) error
CreateClient(accessToken string, realm string, clientID Client) error
CreateClientScope(accessToken string, realm string, scope ClientScope) error
Expand Down
12 changes: 12 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,18 @@ func (client *gocloak) CreateGroup(token, realm string, group Group) (string, er
return getID(resp), nil
}

// CreateChildGroup creates a new child group
func (client *gocloak) CreateChildGroup(token string, realm string, groupID string, group Group) (string, error) {
resp, err := client.getRequestWithBearerAuth(token).
SetBody(group).
Post(client.getAdminRealmURL(realm, "groups", groupID, "children"))

if err := checkForError(resp, err); err != nil {
return "", err
}
return getID(resp), nil
}

func (client *gocloak) CreateComponent(token, realm string, component Component) (string, error) {
resp, err := client.getRequestWithBearerAuth(token).
SetBody(component).
Expand Down
19 changes: 18 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func TestGocloak_SetPassword(t *testing.T) {
FailIfErr(t, err, "Failed to set password")
}

func TestGocloak_CreateListGetUpdateDeleteGroup(t *testing.T) {
func TestGocloak_CreateListGetUpdateDeleteGetChildGroup(t *testing.T) {
t.Parallel()
cfg := GetConfig(t)
client := NewClientWithDebug(t)
Expand Down Expand Up @@ -687,6 +687,23 @@ func TestGocloak_CreateListGetUpdateDeleteGroup(t *testing.T) {
)
assert.NoError(t, err, "GetGroup failed")
assert.Equal(t, *(createdGroup.Name), *(updatedGroup.Name))

childGroupID, err := client.CreateChildGroup(
token.AccessToken,
cfg.GoCloak.Realm,
groupID,
Group{
Name: GetRandomNameP("GroupName"),
},
)
assert.NoError(t, err, "CreateChildGroup failed")

_, err = client.GetGroup(
token.AccessToken,
cfg.GoCloak.Realm,
childGroupID,
)
assert.NoError(t, err, "GetGroup failed")
}

func CreateClientRole(t *testing.T, client GoCloak) (func(), string) {
Expand Down
2 changes: 2 additions & 0 deletions gocloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type GoCloak interface {

// CreateGroup creates a new group
CreateGroup(accessToken, realm string, group Group) (string, error)
// CreateChildGroup creates a new child group
CreateChildGroup(token string, realm string, groupID string, group Group) (string, error)
// CreateClient creates a new client
CreateClient(accessToken, realm string, clientID Client) (string, error)
// CreateClientScope creates a new clientScope
Expand Down

0 comments on commit 91a456f

Please sign in to comment.