Skip to content

Commit

Permalink
Add test user apis (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
guyp-descope authored Nov 18, 2024
1 parent c19640e commit 0170f56
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,12 @@ await descopeClient.management.user.createTestUser('[email protected]', {
userTenants: [{ tenantId: 'tenant-ID1', roleNames: ['role-name1'] }],
});

// Search all test users according to various parameters
const searchRes = await descopeClient.management.user.searchTestUsers(['id']);
searchRes.data.forEach((user) => {
// do something
});

// Now test user got created, and this user will be available until you delete it,
// you can use any management operation for test user CRUD.
// You can also delete all test users.
Expand Down
2 changes: 2 additions & 0 deletions lib/management/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export default {
user: {
create: '/v1/mgmt/user/create',
createTestUser: '/v1/mgmt/user/create/test',
createBatch: '/v1/mgmt/user/create/batch',
update: '/v1/mgmt/user/update',
patch: '/v1/mgmt/user/patch',
Expand All @@ -10,6 +11,7 @@ export default {
load: '/v1/mgmt/user',
logout: '/v1/mgmt/user/logout',
search: '/v2/mgmt/user/search',
searchTestUsers: '/v2/mgmt/user/search/test',
getProviderToken: '/v1/mgmt/user/provider/token',
updateStatus: '/v1/mgmt/user/update/status',
updateLoginId: '/v1/mgmt/user/update/loginid',
Expand Down
4 changes: 2 additions & 2 deletions lib/management/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('Management User', () => {
);

expect(mockHttpClient.post).toHaveBeenCalledWith(
apiPaths.user.create,
apiPaths.user.createTestUser,
{
loginId: 'loginId',
email: '[email protected]',
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('Management User', () => {
});

expect(mockHttpClient.post).toHaveBeenCalledWith(
apiPaths.user.create,
apiPaths.user.createTestUser,
{
loginId: 'loginId',
email: '[email protected]',
Expand Down
23 changes: 16 additions & 7 deletions lib/management/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const withUser = (sdk: CoreSdk, managementKey?: string) => {
test: true,
};
return transformResponse<SingleUserResponse, UserResponse>(
sdk.httpClient.post(apiPaths.user.create, body, { token: managementKey }),
sdk.httpClient.post(apiPaths.user.createTestUser, body, { token: managementKey }),
(data) => data.user,
);
}
Expand Down Expand Up @@ -562,12 +562,21 @@ const withUser = (sdk: CoreSdk, managementKey?: string) => {
),
(data) => data.users,
),
/**
* Search all users. Results can be filtered according to tenants roles and more,
* Pagination is also available using the limit and page parameters.
* @param searchReq an object with all the constraints for this search
* @returns An array of UserResponse found by the query
*/
searchTestUsers: (searchReq: SearchRequest): Promise<SdkResponse<UserResponse[]>> =>
transformResponse<MultipleUsersResponse, UserResponse[]>(
sdk.httpClient.post(
apiPaths.user.searchTestUsers,
{
...searchReq,
withTestUser: true,
testUsersOnly: true,
roleNames: searchReq.roles,
roles: undefined,
},
{ token: managementKey },
),
(data) => data.users,
),
search: (searchReq: SearchRequest): Promise<SdkResponse<UserResponse[]>> =>
transformResponse<MultipleUsersResponse, UserResponse[]>(
sdk.httpClient.post(
Expand Down

0 comments on commit 0170f56

Please sign in to comment.