0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

feat(core): get /dashboard/users/total (#936)

* feat(core): get /dashboard/users/total

* test(core): get /dashboard/users/total
This commit is contained in:
IceHe.xyz 2022-05-25 16:54:21 +08:00 committed by GitHub
parent 4abcda6820
commit c4bb0de7d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,30 @@
import dashboardRoutes from '@/routes/dashboard';
import { createRequester } from '@/utils/test-utils';
const totalUserCount = 1000;
const countUsers = jest.fn(async () => ({ count: totalUserCount }));
jest.mock('@/queries/user', () => ({
countUsers: async () => countUsers(),
}));
describe('dashboardRoutes', () => {
const logRequest = createRequester({ authedRoutes: dashboardRoutes });
afterEach(() => {
jest.clearAllMocks();
});
describe('GET /dashboard/users/total', () => {
it('should call countUsers with no parameters', async () => {
await logRequest.get('/dashboard/users/total');
expect(countUsers).toHaveBeenCalledWith();
});
it('/dashboard/users/total should return correct response', async () => {
const response = await logRequest.get('/dashboard/users/total');
expect(response.status).toEqual(200);
expect(response.body).toEqual({ totalUserCount });
});
});
});

View file

@ -0,0 +1,12 @@
import { countUsers } from '@/queries/user';
import { AuthedRouter } from './types';
export default function dashboardRoutes<T extends AuthedRouter>(router: T) {
router.get('/dashboard/users/total', async (ctx, next) => {
const { count: totalUserCount } = await countUsers();
ctx.body = { totalUserCount };
return next();
});
}

View file

@ -7,6 +7,7 @@ import koaAuth from '@/middleware/koa-auth';
import koaLogSession from '@/middleware/koa-log-session';
import applicationRoutes from '@/routes/application';
import connectorRoutes from '@/routes/connector';
import dashboardRoutes from '@/routes/dashboard';
import resourceRoutes from '@/routes/resource';
import sessionRoutes from '@/routes/session';
import settingRoutes from '@/routes/setting';
@ -40,6 +41,7 @@ const createRouters = (provider: Provider) => {
adminUserRoutes(authedRouter);
logRoutes(authedRouter);
roleRoutes(authedRouter);
dashboardRoutes(authedRouter);
return [sessionRouter, anonymousRouter, authedRouter];
};