Skip to content

Commit

Permalink
Set user roles
Browse files Browse the repository at this point in the history
On top of the existing add, remove
+ test
related to descope/etc#4886
  • Loading branch information
aviadl committed Dec 18, 2023
1 parent 7f19cae commit d8984dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/management/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
updateDisplayName: '/v1/mgmt/user/update/name',
updatePicture: '/v1/mgmt/user/update/picture',
updateCustomAttribute: '/v1/mgmt/user/update/customAttribute',
setRole: '/v1/mgmt/user/update/role/set',
addRole: '/v1/mgmt/user/update/role/add',
removeRole: '/v1/mgmt/user/update/role/remove',
addTenant: '/v1/mgmt/user/update/tenant/add',
Expand Down
29 changes: 29 additions & 0 deletions lib/management/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,35 @@ describe('Management User', () => {
});
});

describe('setRoles', () => {
it('should send the correct request and receive correct response', async () => {
const httpResponse = {
ok: true,
json: () => mockMgmtUserResponse,
clone: () => ({
json: () => Promise.resolve(mockMgmtUserResponse),
}),
status: 200,
};
mockHttpClient.post.mockResolvedValue(httpResponse);

const resp: SdkResponse<UserResponse> = await management.user.setRoles('lid', ['foo', 'bar']);

expect(mockHttpClient.post).toHaveBeenCalledWith(
apiPaths.user.setRole,
{ loginId: 'lid', roleNames: ['foo', 'bar'] },
{ token: 'key' },
);

expect(resp).toEqual({
code: 200,
data: mockUserResponse,
ok: true,
response: httpResponse,
});
});
});

describe('addRoles', () => {
it('should send the correct request and receive correct response', async () => {
const httpResponse = {
Expand Down
9 changes: 9 additions & 0 deletions lib/management/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ const withUser = (sdk: CoreSdk, managementKey?: string) => ({
),
(data) => data.user,
),
setRoles: (loginId: string, roles: string[]): Promise<SdkResponse<UserResponse>> =>
transformResponse<SingleUserResponse, UserResponse>(
sdk.httpClient.post(
apiPaths.user.setRole,
{ loginId, roleNames: roles },
{ token: managementKey },
),
(data) => data.user,
),
addRoles: (loginId: string, roles: string[]): Promise<SdkResponse<UserResponse>> =>
transformResponse<SingleUserResponse, UserResponse>(
sdk.httpClient.post(
Expand Down

0 comments on commit d8984dc

Please sign in to comment.