Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test the dashboard admins property #8303

Merged
merged 7 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/lib/db/account-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import User from '../types/user';
import NotFoundError from '../error/notfound-error';
import type { IUserLookup } from '../types/stores/user-store';
import type { IAdminCount } from '../types/stores/account-store';
import type { IAccountStore, MinimalUser } from '../types';
import type { IAccountStore, MinimalUiUser } from '../types';
import type { Db } from './db';

const TABLE = 'users';
Expand Down Expand Up @@ -199,14 +199,15 @@ export class AccountStore implements IAccountStore {
};
}

async getAdmins(): Promise<MinimalUser[]> {
async getAdmins(): Promise<MinimalUiUser[]> {
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,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,52 @@ test('should return personal dashboard project details', async () => {
],
});
});

test('should return Unleash admins', async () => {
await loginUser('[email protected]');
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
kwasniew marked this conversation as resolved.
Show resolved Hide resolved
.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`);

const admins = body.admins;
admins.sort((a, b) => a.id - b.id);
thomasheartman marked this conversation as resolved.
Show resolved Hide resolved
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/),
},
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
IAccountStore,
IEventStore,
IOnboardingReadModel,
MinimalUser,
MinimalUiUser,
} from '../../types';
import type { FeatureEventFormatter } from '../../addons/feature-event-formatter-md';
import { generateImageUrl } from '../../util';
Expand Down Expand Up @@ -126,7 +126,7 @@ export class PersonalDashboardService {
return { latestEvents: formattedEvents, onboardingStatus };
}

async getAdmins(): Promise<MinimalUser[]> {
async getAdmins(): Promise<MinimalUiUser[]> {
return this.accountStore.getAdmins();
}
}
4 changes: 2 additions & 2 deletions src/lib/types/stores/account-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IUser, MinimalUser } from '../user';
import type { IUser, MinimalUiUser } from '../user';
import type { Store } from './store';

export interface IUserLookup {
Expand All @@ -22,5 +22,5 @@ export interface IAccountStore extends Store<IUser, number> {
getAccountByPersonalAccessToken(secret: string): Promise<IUser>;
markSeenAt(secrets: string[]): Promise<void>;
getAdminCount(): Promise<IAdminCount>;
getAdmins(): Promise<MinimalUser[]>;
getAdmins(): Promise<MinimalUiUser[]>;
}
2 changes: 1 addition & 1 deletion src/lib/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IUser {
scimId?: string;
}

export type MinimalUser = Pick<
export type MinimalUiUser = Pick<
thomasheartman marked this conversation as resolved.
Show resolved Hide resolved
IUser,
'id' | 'name' | 'username' | 'email' | 'imageUrl'
>;
Expand Down