From d0b2ce31700e172849eed315500b59612b97a515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 28 Oct 2021 14:24:09 +0200 Subject: [PATCH] fix: add enpoint for fetching a single user (#1074) * fix: add enpoint for fetching a single user * fix add api docs --- src/lib/routes/admin-api/user-admin.ts | 7 ++++++ src/test/e2e/api/admin/user-admin.e2e.test.ts | 21 +++++++++++++++-- website/docs/api/admin/user-admin.md | 23 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/lib/routes/admin-api/user-admin.ts b/src/lib/routes/admin-api/user-admin.ts index 2555160f22..590e506c78 100644 --- a/src/lib/routes/admin-api/user-admin.ts +++ b/src/lib/routes/admin-api/user-admin.ts @@ -61,6 +61,7 @@ export default class UserAdminController extends Controller { this.get('/search', this.search); this.post('/', this.createUser, ADMIN); this.post('/validate-password', this.validatePassword); + this.get('/:id', this.getUser, ADMIN); this.put('/:id', this.updateUser, ADMIN); this.post('/:id/change-password', this.changePassword, ADMIN); this.delete('/:id', this.deleteUser, ADMIN); @@ -115,6 +116,12 @@ export default class UserAdminController extends Controller { } } + async getUser(req: Request, res: Response): Promise { + const { id } = req.params; + const user = await this.userService.getUser(Number(id)); + res.json(user); + } + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types async createUser( req: IAuthRequest, diff --git a/src/test/e2e/api/admin/user-admin.e2e.test.ts b/src/test/e2e/api/admin/user-admin.e2e.test.ts index 05b2433827..e57b1a254c 100644 --- a/src/test/e2e/api/admin/user-admin.e2e.test.ts +++ b/src/test/e2e/api/admin/user-admin.e2e.test.ts @@ -143,8 +143,6 @@ test('requires known root role', async () => { }); test('update user name', async () => { - expect.assertions(3); - const { body } = await app.request .post('/api/admin/user-admin') .send({ @@ -168,6 +166,25 @@ test('update user name', async () => { }); }); +test('get a single user', async () => { + const { body } = await app.request + .post('/api/admin/user-admin') + .send({ + email: 'some2@getunelash.ai', + name: 'Some Name 2', + rootRole: editorRole.id, + }) + .set('Content-Type', 'application/json'); + + const { body: user } = await app.request + .get(`/api/admin/user-admin/${body.id}`) + .expect(200); + + expect(user.email).toBe('some2@getunelash.ai'); + expect(user.name).toBe('Some Name 2'); + expect(user.id).toBe(body.id); +}); + test('should delete user', async () => { expect.assertions(0); diff --git a/website/docs/api/admin/user-admin.md b/website/docs/api/admin/user-admin.md index 25e7b609ce..937d330634 100644 --- a/website/docs/api/admin/user-admin.md +++ b/website/docs/api/admin/user-admin.md @@ -65,6 +65,29 @@ Will return all users and all available root roles for the Unleash instance. } ``` + +### Get a single users {#get-user} + +`GET https://unleash.host.com/api/admin/user-admin/:id` + +Will return a single user by id. + +**Body** + +```json +{ + "createdAt": "2021-05-14T08:58:07.891Z", + "email": "random-user2@getunleash.ai", + "id": 4, + "imageUrl": "https://gravatar.com/avatar/90047524992cd6ae8f66e249a7630d80?size=42&default=retro", + "inviteLink": "", + "isAPI": false, + "loginAttempts": 0, + "rootRole": 1, + "seenAt": null + } +``` + ### Search for users {#search-for-users} You can also search for users via the search API. It will preform a simple search based on name and email matching the given query. Requires minimum 2 characters.