test: add admin routes tests

Made-with: Cursor
This commit is contained in:
Anton
2026-03-04 16:07:44 +03:00
parent 39be02e1ca
commit cacb9e0c9b
3 changed files with 207 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import Fastify, { FastifyInstance } from 'fastify';
import { AppError } from '../../src/utils/errors.js';
import { adminQuestionsRoutes } from '../../src/routes/admin/questions.js';
import type { MockDb } from '../test-utils.js';
import { createMockDb } from '../test-utils.js';
const mockAdminUser = { id: 'admin-1', email: 'admin@test.com' };
/**
* Build a minimal Fastify app for admin route integration tests.
* Bypasses real auth - preHandlers set req.user to mock admin.
*/
export async function buildAdminTestApp(mockDb?: MockDb): Promise<FastifyInstance> {
const db = mockDb ?? createMockDb();
const app = Fastify({
logger: false,
requestIdHeader: 'x-request-id',
requestIdLogLabel: 'requestId',
});
app.setErrorHandler((err: unknown, _request, reply) => {
const error = err as Error & { statusCode?: number; validation?: unknown };
if (err instanceof AppError) {
return reply.status(err.statusCode).send(err.toJSON());
}
if (error.validation) {
return reply.status(422).send({
error: { code: 'VALIDATION_ERROR', message: 'Validation failed', details: error.validation },
});
}
return reply.status(500).send({ error: { code: 'INTERNAL_ERROR', message: error.message } });
});
app.decorate('db', db);
app.decorate('rateLimitOptions', {
apiAuthed: { max: 100, timeWindow: '1 minute' },
});
app.decorateRequest('user', undefined);
app.decorate('authenticate', async (req: { user?: { id: string; email: string } }) => {
req.user = mockAdminUser;
});
app.decorate('authenticateAdmin', async (req: { user?: { id: string; email: string } }) => {
if (!req.user) req.user = mockAdminUser;
(req.user as { role?: string }).role = 'admin';
});
await app.register(adminQuestionsRoutes, { prefix: '/admin' });
return app;
}