Skip to content

Commit

Permalink
search roles
Browse files Browse the repository at this point in the history
  • Loading branch information
slavikm committed Mar 5, 2024
1 parent 11ba389 commit cead1a8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions descope/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ var (
roleUpdate: "mgmt/role/update",
roleDelete: "mgmt/role/delete",
roleLoadAll: "mgmt/role/all",
roleSearch: "mgmt/role/search",
groupLoadAllGroups: "mgmt/group/all",
groupLoadAllGroupsForMember: "mgmt/group/member/all",
groupLoadAllGroupMembers: "mgmt/group/members",
Expand Down Expand Up @@ -345,6 +346,7 @@ type mgmtEndpoints struct {
roleUpdate string
roleDelete string
roleLoadAll string
roleSearch string

groupLoadAllGroups string
groupLoadAllGroupsForMember string
Expand Down Expand Up @@ -885,6 +887,10 @@ func (e *endpoints) ManagementRoleLoadAll() string {
return path.Join(e.version, e.mgmt.roleLoadAll)
}

func (e *endpoints) ManagementRoleSearch() string {
return path.Join(e.version, e.mgmt.roleSearch)
}

func (e *endpoints) ManagementGroupLoadAllGroups() string {
return path.Join(e.version, e.mgmt.groupLoadAllGroups)
}
Expand Down
8 changes: 8 additions & 0 deletions descope/internal/mgmt/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func (r *role) LoadAll(ctx context.Context) ([]*descope.Role, error) {
return unmarshalRolesLoadAllResponse(res)
}

func (r *role) Search(ctx context.Context, options *descope.RoleSearchOptions) ([]*descope.Role, error) {
res, err := r.client.DoPostRequest(ctx, api.Routes.ManagementRoleSearch(), options, nil, r.conf.ManagementKey)
if err != nil {
return nil, err
}
return unmarshalRolesLoadAllResponse(res)
}

func unmarshalRolesLoadAllResponse(res *api.HTTPResponse) ([]*descope.Role, error) {
pres := struct {
Roles []*descope.Role
Expand Down
34 changes: 34 additions & 0 deletions descope/internal/mgmt/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"testing"

"github.com/descope/go-sdk/descope"
"github.com/descope/go-sdk/descope/tests/helpers"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -95,3 +96,36 @@ func TestRoleLoadError(t *testing.T) {
require.Error(t, err)
require.Nil(t, res)
}

func TestRoleSearchSuccess(t *testing.T) {
response := map[string]any{
"roles": []map[string]any{{
"name": "abc",
}}}
mgmt := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
require.Equal(t, r.Header.Get("Authorization"), "Bearer a:key")
req := map[string]any{}
require.NoError(t, helpers.ReadBody(r, &req))
require.ElementsMatch(t, []string{"t1"}, req["tenantIds"])
require.ElementsMatch(t, []string{"r1"}, req["roleNames"])
require.Equal(t, "abc", req["roleNameLike"])
require.ElementsMatch(t, []string{"p1"}, req["permissionNames"])
}, response))
res, err := mgmt.Role().Search(context.Background(), &descope.RoleSearchOptions{
TenantIDs: []string{"t1"},
RoleNames: []string{"r1"},
RoleNameLike: "abc",
PermissionNames: []string{"p1"},
})
require.NoError(t, err)
require.NotNil(t, res)
require.Len(t, res, 1)
require.Equal(t, "abc", res[0].Name)
}

func TestRoleSearchError(t *testing.T) {
mgmt := newTestMgmt(nil, helpers.DoBadRequest(nil))
res, err := mgmt.Role().Search(context.Background(), &descope.RoleSearchOptions{})
require.Error(t, err)
require.Nil(t, res)
}
2 changes: 2 additions & 0 deletions descope/sdk/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ type Role interface {

// Load all roles.
LoadAll(ctx context.Context) ([]*descope.Role, error)

Search(ctx context.Context, options *descope.RoleSearchOptions) ([]*descope.Role, error)
}

// Provides functions for querying SSO groups in a project's tenant.
Expand Down
7 changes: 7 additions & 0 deletions descope/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,13 @@ func (r *Role) GetCreatedTime() time.Time {
return time.Unix(int64(r.CreatedTime), 0)
}

type RoleSearchOptions struct {
TenantIDs []string `json:"tenantIds,omitempty"`
RoleNames []string `json:"roleNames,omitempty"`
RoleNameLike string `json:"roleNameLike,omitempty"`
PermissionNames []string `json:"permissionNames,omitempty"`
}

// Options for searching and filtering users
//
// Limit - limits the number of returned users. Leave at 0 to return the default amount.
Expand Down

0 comments on commit cead1a8

Please sign in to comment.