Some checks failed
farmcontrol/farmcontrol-api/pipeline/head There was a failure building this commit
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
import { jest } from '@jest/globals';
|
|
|
|
jest.unstable_mockModule('../../../database/database.js', () => ({
|
|
getModelStats: jest.fn(),
|
|
getModelHistory: jest.fn(),
|
|
}));
|
|
|
|
jest.unstable_mockModule('../../../database/schemas/management/auditlog.schema.js', () => ({
|
|
auditLogModel: {
|
|
modelName: 'AuditLog',
|
|
find: jest.fn(),
|
|
findOne: jest.fn(),
|
|
},
|
|
}));
|
|
|
|
jest.unstable_mockModule('log4js', () => ({
|
|
default: {
|
|
getLogger: () => ({
|
|
level: 'info',
|
|
debug: jest.fn(),
|
|
error: jest.fn(),
|
|
warn: jest.fn(),
|
|
trace: jest.fn(),
|
|
}),
|
|
},
|
|
}));
|
|
|
|
const { listAuditLogsRouteHandler, getAuditLogRouteHandler } = await import('../auditlogs.js');
|
|
|
|
const { auditLogModel } = await import('../../../database/schemas/management/auditlog.schema.js');
|
|
|
|
describe('Audit Log Service Route Handlers', () => {
|
|
let req, res;
|
|
|
|
beforeEach(() => {
|
|
req = {
|
|
params: {},
|
|
query: {},
|
|
body: {},
|
|
user: { id: 'test-user-id' },
|
|
};
|
|
res = {
|
|
send: jest.fn(),
|
|
status: jest.fn().mockReturnThis(),
|
|
};
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
describe('listAuditLogsRouteHandler', () => {
|
|
it('should list audit logs', async () => {
|
|
const mockResult = [
|
|
{ _id: '1', operation: 'edit', parent: 'parent123', _doc: { parent: 'parent123' } },
|
|
];
|
|
auditLogModel.find.mockReturnValue({
|
|
sort: jest.fn().mockReturnThis(),
|
|
skip: jest.fn().mockReturnThis(),
|
|
limit: jest.fn().mockReturnThis(),
|
|
populate: jest.fn().mockResolvedValue(mockResult),
|
|
});
|
|
|
|
await listAuditLogsRouteHandler(req, res);
|
|
|
|
expect(auditLogModel.find).toHaveBeenCalled();
|
|
expect(res.send).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('getAuditLogRouteHandler', () => {
|
|
it('should get an audit log by ID', async () => {
|
|
req.params.id = '507f1f77bcf86cd799439011';
|
|
const mockLog = { _id: '507f1f77bcf86cd799439011', operation: 'edit' };
|
|
auditLogModel.findOne.mockReturnValue({
|
|
populate: jest.fn().mockReturnValue({
|
|
populate: jest.fn().mockReturnValue({
|
|
populate: jest.fn().mockResolvedValue(mockLog),
|
|
}),
|
|
}),
|
|
});
|
|
|
|
await getAuditLogRouteHandler(req, res);
|
|
|
|
expect(auditLogModel.findOne).toHaveBeenCalled();
|
|
expect(res.send).toHaveBeenCalledWith(mockLog);
|
|
});
|
|
});
|
|
});
|
|
|