From e4cec6629dba7a990261d690da92f6bfe1a216af Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 30 Sep 2024 15:38:00 +0200 Subject: [PATCH] test: test the dashboard admins property (#8303) This PR adds tests for the new admins property of the personal dashboard API payload. It checks that only user admins are added and that their image URL is not an empty string. In doing this, also fixes an issue where the image URL wouldn't be generated correctly. ## Discussion points Some of the test feels like it might be better testing on a deeper level (i.e. the account store). However, from an initial glance, I think that would require more setup and work, so I'm leaving it in the dashboard test for now as that's where it's ultimately useful. But we can discuss if we should move it. --- src/lib/db/account-store.ts | 11 +++-- .../personal-dashboard-controller.e2e.test.ts | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/lib/db/account-store.ts b/src/lib/db/account-store.ts index c5389a0cdc..4c87717d67 100644 --- a/src/lib/db/account-store.ts +++ b/src/lib/db/account-store.ts @@ -201,12 +201,13 @@ export class AccountStore implements IAccountStore { async getAdmins(): Promise { const rowToAdminUser = (row) => { + const user = rowToUser(row); return { - id: row.id, - name: emptify(row.name), - username: emptify(row.username), - email: emptify(row.email), - imageUrl: emptify(row.image_url), + id: user.id, + name: user.name, + username: user.username, + email: user.email, + imageUrl: user.imageUrl, }; }; diff --git a/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts b/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts index 55a7f9ac5e..6304c7eb59 100644 --- a/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts +++ b/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts @@ -217,3 +217,50 @@ test('should return personal dashboard project details', async () => { ], }); }); + +test('should return Unleash admins', async () => { + await loginUser('new_user@test.com'); + const adminRoleId = 1; + const userService = app.services.userService; + + const admin = await userService.createUser({ + username: 'admin', + rootRole: adminRoleId, + }); + const admin2 = await userService.createUser({ + username: 'John', + name: 'John Admin', + rootRole: adminRoleId, + }); + + // service account that shouldn't be listed in the output. Service + // accounts are enterprise functionality, so there's no service to + // call here + const [{ id: serviceAdminId }] = await db + .rawDatabase('users') + .insert({ + username: 'service_admin', + is_service: true, + }) + .returning('*'); + await app.services.accessService.setUserRootRole( + serviceAdminId, + adminRoleId, + ); + + const { body } = await app.request.get(`/api/admin/personal-dashboard`); + + expect(body.admins).toMatchObject([ + { + id: admin.id, + username: admin.username, + imageUrl: expect.stringMatching(/^https:\/\/gravatar.com/), + }, + { + id: admin2.id, + name: admin2.name, + username: admin2.username, + imageUrl: expect.stringMatching(/^https:\/\/gravatar.com/), + }, + ]); +});