mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
feat(core): get /logs/:id (#934)
This commit is contained in:
parent
d60748b20c
commit
bddf47bf90
3 changed files with 41 additions and 2 deletions
|
@ -44,3 +44,10 @@ export const findLogs = async (limit: number, offset: number, logCondition: LogC
|
|||
limit ${limit}
|
||||
offset ${offset}
|
||||
`);
|
||||
|
||||
export const findLogById = async (id: string) =>
|
||||
envSet.pool.one<Log>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id}=${id}
|
||||
`);
|
||||
|
|
|
@ -2,7 +2,8 @@ import { LogCondition } from '@/queries/log';
|
|||
import logRoutes from '@/routes/log';
|
||||
import { createRequester } from '@/utils/test-utils';
|
||||
|
||||
const mockLogs = [{ id: 1 }, { id: 2 }];
|
||||
const mockLog = { id: 1 };
|
||||
const mockLogs = [mockLog, { id: 2 }];
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
const countLogs = jest.fn(async (condition: LogCondition) => ({
|
||||
|
@ -11,12 +12,14 @@ const countLogs = jest.fn(async (condition: LogCondition) => ({
|
|||
const findLogs = jest.fn(
|
||||
async (limit: number, offset: number, condition: LogCondition) => mockLogs
|
||||
);
|
||||
const findLogById = jest.fn(async (id: string) => mockLog);
|
||||
/* eslint-enable @typescript-eslint/no-unused-vars */
|
||||
|
||||
jest.mock('@/queries/log', () => ({
|
||||
countLogs: async (condition: LogCondition) => countLogs(condition),
|
||||
findLogs: async (limit: number, offset: number, condition: LogCondition) =>
|
||||
findLogs(limit, offset, condition),
|
||||
findLogById: async (id: string) => findLogById(id),
|
||||
}));
|
||||
|
||||
describe('logRoutes', () => {
|
||||
|
@ -48,4 +51,19 @@ describe('logRoutes', () => {
|
|||
expect(response.header).toHaveProperty('total-number', `${mockLogs.length}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /logs/:id', () => {
|
||||
const logId = 'logIdValue';
|
||||
|
||||
it('should call findLogById with correct parameters', async () => {
|
||||
await logRequest.get(`/logs/${logId}`);
|
||||
expect(findLogById).toHaveBeenCalledWith(logId);
|
||||
});
|
||||
|
||||
it('should return correct response', async () => {
|
||||
const response = await logRequest.get(`/logs/${logId}`);
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual(mockLog);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -2,7 +2,7 @@ import { object, string } from 'zod';
|
|||
|
||||
import koaGuard from '@/middleware/koa-guard';
|
||||
import koaPagination from '@/middleware/koa-pagination';
|
||||
import { countLogs, findLogs } from '@/queries/log';
|
||||
import { countLogs, findLogById, findLogs } from '@/queries/log';
|
||||
|
||||
import { AuthedRouter } from './types';
|
||||
|
||||
|
@ -35,4 +35,18 @@ export default function logRoutes<T extends AuthedRouter>(router: T) {
|
|||
return next();
|
||||
}
|
||||
);
|
||||
|
||||
router.get(
|
||||
'/logs/:id',
|
||||
koaGuard({ params: object({ id: string().min(1) }) }),
|
||||
async (ctx, next) => {
|
||||
const {
|
||||
params: { id },
|
||||
} = ctx.guard;
|
||||
|
||||
ctx.body = await findLogById(id);
|
||||
|
||||
return next();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue