Analytics: Admin-Routen nur auf internem Listener (R5), Transaktion, Duplikat-Check

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Sonnet 5.5 committed 2026-10-01 09:24:57 +00:00
1 parent f7da79e4b3
commit cd9aebe4b1
4 files changed
+71 -12

No files matched your search

+34 -4
View File
@@ -1,14 +1,17 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { loadConfig } from '../src/config.js';
import { openDb } from '../src/db.js';
import { createServer } from '../src/server.js';
import { createPublicServer, createAdminServer } from '../src/server.js';
const KEY = 'abcdef0123456789abcd';
async function setup(adminToken = 'geheim') {
async function setup(adminToken = 'geheim', make = createAdminServer) {
const db = openDb(':memory:');
const server = createServer({ config: { adminToken }, db });
const server = make({ config: { adminToken }, db });
await new Promise((r) => server.listen(0, '127.0.0.1', r));
const base = `http://127.0.0.1:${server.address().port}`;
const call = async (method, path, body, token = 'geheim', raw) => {
@@ -25,7 +28,7 @@ const valid = { projectId: 'p1', domains: ['abc.project.webklar.com'], active: t
test('loadConfig: Vorgaben und Umgebung', () => {
const c = loadConfig({});
assert.deepEqual(c, { port: 8090, dataDir: '/srv/webklar-analytics', adminToken: '', heatSecret: '', geoFile: '', baseHost: 'project.webklar.com' });
assert.deepEqual(c, { port: 8090, adminPort: 8091, dataDir: '/srv/webklar-analytics', adminToken: '', heatSecret: '', geoFile: '', baseHost: 'project.webklar.com' });
const d = loadConfig({ ANALYTICS_PORT: '9000', ANALYTICS_ADMIN_TOKEN: 't', ANALYTICS_HEAT_SECRET: 'h' });
assert.equal(d.port, 9000);
assert.equal(d.adminToken, 't');
@@ -136,3 +139,30 @@ test('DELETE entfernt Website, unbekannte Route 404', async () => {
assert.equal((await t.call('GET', '/a/nichts', undefined, null)).status, 404);
t.close();
});
test('Admin-Route auf dem öffentlichen Listener: 404 auch mit richtigem Token', async () => {
const t = await setup('geheim', createPublicServer);
assert.equal((await t.call('PUT', `/a/admin/sites/${KEY}`, valid)).status, 404);
assert.equal((await t.call('DELETE', `/a/admin/sites/${KEY}`)).status, 404);
assert.equal(row(t.db), undefined);
assert.equal((await t.call('GET', '/a/health', undefined, null)).status, 200);
t.close();
});
test('Doppelte Domains: 400', async () => {
const t = await setup();
assert.equal((await t.call('PUT', `/a/admin/sites/${KEY}`, { ...valid, domains: ['a.de', 'a.de'] })).status, 400);
t.close();
});
test('Datei-DB: zweimal öffnen (idempotent) und WAL', () => {
const f = join(mkdtempSync(join(tmpdir(), 'ana-')), 'a.db');
const d1 = openDb(f);
d1.prepare("INSERT INTO sites VALUES ('k','p','[]',1,NULL)").run();
assert.equal(d1.prepare('PRAGMA journal_mode').get().journal_mode, 'wal');
d1.close();
const d2 = openDb(f);
assert.equal(d2.prepare('SELECT COUNT(*) c FROM sites').get().c, 1);
assert.equal(d2.prepare('PRAGMA journal_mode').get().journal_mode, 'wal');
d2.close();
});