82 lines
2.5 KiB
JavaScript
82 lines
2.5 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
require('../src/common');
|
|
|
|
const registerAuditLogRoutes = require('../src/web/routes/settings/audit-log');
|
|
|
|
test('audit log uses its own read permission and shared pagination partial', async () => {
|
|
const handlers = {};
|
|
const app = {
|
|
get(path, ...routeHandlers) {
|
|
handlers[path] = routeHandlers;
|
|
}
|
|
};
|
|
|
|
registerAuditLogRoutes(app, {
|
|
pool: {
|
|
async query(sql) {
|
|
if (String(sql).includes('FROM o_audit_events')) {
|
|
return [[
|
|
{
|
|
id: 1,
|
|
occurred_at: '2026-08-16T12:00:00.000Z',
|
|
category: 'authentication',
|
|
event_type: 'login.success',
|
|
target_label: 'newer',
|
|
details_json: null
|
|
},
|
|
{
|
|
id: 2,
|
|
occurred_at: '2026-08-15T12:00:00.000Z',
|
|
category: 'authentication',
|
|
event_type: 'login.failed',
|
|
target_label: 'older',
|
|
details_json: null
|
|
}
|
|
]];
|
|
}
|
|
return [[]];
|
|
}
|
|
},
|
|
formatDashboardDate() {
|
|
return 'Aug 16, 2026';
|
|
}
|
|
});
|
|
|
|
assert.ok(handlers['/settings/audit-log']);
|
|
const request = {
|
|
currentUser: { id: 7, permissions: ['audit-log.read', 'audit-log.allow'] },
|
|
query: {}
|
|
};
|
|
let continued = false;
|
|
handlers['/settings/audit-log'][0](request, {}, function () {
|
|
continued = true;
|
|
});
|
|
assert.equal(continued, true);
|
|
|
|
const response = { send(value) { this.body = value; } };
|
|
await handlers['/settings/audit-log'][1](request, response, function (error) {
|
|
throw error;
|
|
});
|
|
assert.match(response.body, /Audit log/);
|
|
assert.match(response.body, /table-pagination/);
|
|
assert.match(response.body, /data-local-datetime/);
|
|
assert.match(response.body, /audit-event-type-options/);
|
|
assert.match(response.body, /login\.success/);
|
|
assert.ok(response.body.indexOf('login.success') < response.body.indexOf('login.failed'));
|
|
assert.match(response.body, />Export</);
|
|
assert.ok(handlers['/settings/audit-log/export']);
|
|
|
|
const exportResponse = {
|
|
headers: {},
|
|
setHeader(name, value) { this.headers[name] = value; },
|
|
send(value) { this.body = value; }
|
|
};
|
|
await handlers['/settings/audit-log/export'][1](request, exportResponse, function (error) {
|
|
throw error;
|
|
});
|
|
assert.match(exportResponse.headers['Content-Type'], /text\/csv/);
|
|
assert.match(exportResponse.body, /Occurred At \(UTC\)/);
|
|
assert.match(exportResponse.body, /login.success/);
|
|
}); |