Skip to content

Commit

Permalink
Search roles (#343)
Browse files Browse the repository at this point in the history
* search roles

* readme
  • Loading branch information
slavikm authored Mar 6, 2024
1 parent e391c4e commit 358f259
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,15 @@ const rolesRes = await descopeClient.management.role.loadAll();
rolesRes.data.forEach((role) => {
// do something
});

// Search roles
const rolesRes = await descopeClient.management.role.search({
tenantIds: ['t1', 't2'],
roleNames: ['role1'],
});
rolesRes.data.forEach((role) => {
// do something
});
```

### Query SSO Groups
Expand Down
1 change: 1 addition & 0 deletions lib/management/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default {
update: '/v1/mgmt/role/update',
delete: '/v1/mgmt/role/delete',
loadAll: '/v1/mgmt/role/all',
search: '/v1/mgmt/role/search',
},
flow: {
list: '/v1/mgmt/flow/list',
Expand Down
34 changes: 33 additions & 1 deletion lib/management/role.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SdkResponse } from '@descope/core-js-sdk';
import withManagement from '.';
import apiPaths from './paths';
import { mockCoreSdk, mockHttpClient } from './testutils';
import { Role } from './types';
import { Role, RoleSearchOptions } from './types';

const management = withManagement(mockCoreSdk, 'key');

Expand Down Expand Up @@ -157,4 +157,36 @@ describe('Management Role', () => {
});
});
});

describe('search', () => {
it('should send the correct request and receive correct response', async () => {
const httpResponse = {
ok: true,
json: () => mockAllRolesResponse,
clone: () => ({
json: () => Promise.resolve(mockAllRolesResponse),
}),
status: 200,
};
mockHttpClient.post.mockResolvedValue(httpResponse);
const req: RoleSearchOptions = {
tenantIds: ['t'],
roleNames: ['r'],
roleNameLike: 'x',
permissionNames: ['p'],
};
const resp: SdkResponse<Role[]> = await management.role.search(req);

expect(mockHttpClient.post).toHaveBeenCalledWith(apiPaths.role.search, req, {
token: 'key',
});

expect(resp).toEqual({
code: 200,
data: mockRoles,
ok: true,
response: httpResponse,
});
});
});
});
9 changes: 8 additions & 1 deletion lib/management/role.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SdkResponse, transformResponse } from '@descope/core-js-sdk';
import { CoreSdk } from '../types';
import apiPaths from './paths';
import { Role } from './types';
import { Role, RoleSearchOptions } from './types';

type MultipleRoleResponse = {
roles: Role[];
Expand Down Expand Up @@ -46,6 +46,13 @@ const withRole = (sdk: CoreSdk, managementKey?: string) => ({
}),
(data) => data.roles,
),
search: (options: RoleSearchOptions): Promise<SdkResponse<Role[]>> =>
transformResponse<MultipleRoleResponse, Role[]>(
sdk.httpClient.post(apiPaths.role.search, options, {
token: managementKey,
}),
(data) => data.roles,
),
});

export default withRole;
8 changes: 8 additions & 0 deletions lib/management/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ export type Role = {
tenantId?: string;
};

/** Search roles based on the parameters */
export type RoleSearchOptions = {
tenantIds?: string[];
roleNames?: string[];
roleNameLike?: string; // Search roles where name contains this - case insensitive
permissionNames?: string[];
};

/** Represents a group in a project. It has an id and display name and a list of group members. */
export type Group = {
id: string;
Expand Down

0 comments on commit 358f259

Please sign in to comment.