From 97363e01dcff44e36d3bc17ce1d13e364a701641 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Mon, 8 May 2023 11:00:58 +0800 Subject: [PATCH] test(core): add api response guard and error case tests to log api (#3807) --- packages/core/src/routes/log.ts | 4 ++- .../src/tests/api/log.test.ts | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 packages/integration-tests/src/tests/api/log.test.ts diff --git a/packages/core/src/routes/log.ts b/packages/core/src/routes/log.ts index e2db2aff3..e4c584504 100644 --- a/packages/core/src/routes/log.ts +++ b/packages/core/src/routes/log.ts @@ -20,6 +20,8 @@ export default function logRoutes( applicationId: string().optional(), logKey: string().optional(), }), + response: Logs.guard.omit({ tenantId: true }).array(), + status: 200, }), async (ctx, next) => { const { limit, offset } = ctx.pagination; @@ -43,7 +45,7 @@ export default function logRoutes( router.get( '/logs/:id', - koaGuard({ params: object({ id: string().min(1) }), response: Logs.guard }), + koaGuard({ params: object({ id: string().min(1) }), response: Logs.guard, status: [200, 404] }), async (ctx, next) => { const { params: { id }, diff --git a/packages/integration-tests/src/tests/api/log.test.ts b/packages/integration-tests/src/tests/api/log.test.ts new file mode 100644 index 000000000..d589f9497 --- /dev/null +++ b/packages/integration-tests/src/tests/api/log.test.ts @@ -0,0 +1,25 @@ +import { getLog, getLogs } from '#src/api/index.js'; +import { createResponseWithCode } from '#src/helpers/admin-tenant.js'; + +describe('logs', () => { + it('should get logs successfully', async () => { + const logs = await getLogs(); + + expect(logs.length).toBeGreaterThan(0); + }); + + it('should get log detail successfully', async () => { + const logs = await getLogs(); + const logId = logs[0]?.id; + + expect(logId).not.toBeUndefined(); + + if (logId) { + await expect(getLog(logId)).resolves.toHaveProperty('id', logId); + } + }); + + it('should throw on getting non-exist log detail', async () => { + await expect(getLog('non-exist-log-id')).rejects.toMatchObject(createResponseWithCode(404)); + }); +});