import assert from 'node:assert/strict'; import { createServer } from 'node:http'; import { after, before, describe, it } from 'node:test'; import { closeDatabase, initDatabase } from '../src/database/index.js'; import { createHandler } from '../src/web/server.js'; import { generateRuntimeSetupToken } from '../src/web/setup-state.js'; /** Drives the real request handler over a loopback socket (M16-T01). */ function serve(options) { const server = createServer(createHandler(options)); const ready = new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); return { ready, close: () => new Promise((resolve) => server.close(resolve)), url: (path) => `http://127.0.0.1:${server.address().port}${path}`, fetch: (path, init) => fetch(`http://127.0.0.1:${server.address().port}${path}`, init), }; } const WRITE_HEADERS = { 'Content-Type': 'application/json', 'X-Requested-With': 'fws-dashboard' }; describe('dashboard server', () => { let app; before(async () => { initDatabase(':memory:'); app = serve({ client: null, setupOnly: false }); await app.ready; }); after(async () => { await app.close(); closeDatabase(); }); it('answers the health probe without a session', async () => { const response = await app.fetch('/api/health'); const body = await response.json(); assert.equal(response.status, 200); assert.equal(body.status, 'starting'); assert.equal(body.setupOnly, false); }); it('describes how to sign in without a session', async () => { const body = await app.fetch('/api/auth/config').then((response) => response.json()); assert.equal(typeof body.oauthConfigured, 'boolean'); assert.equal(body.loginUrl, '/auth/login'); }); it('reports an anonymous visitor as not authenticated', async () => { const body = await app.fetch('/api/me').then((response) => response.json()); assert.equal(body.authenticated, false); }); it('refuses a protected route with 401', async () => { const response = await app.fetch('/api/overview'); assert.equal(response.status, 401); assert.match((await response.json()).error, /Authentification/); }); it('returns 404 on an unknown API route', async () => { assert.equal((await app.fetch('/api/nope')).status, 404); }); it('returns 405 when the path exists under another method', async () => { const response = await app.fetch('/api/health', { method: 'POST', headers: WRITE_HEADERS, body: '{}' }); assert.equal(response.status, 405); }); it('blocks a write that does not carry the dashboard header', async () => { const response = await app.fetch('/api/auth/logout', { method: 'POST' }); assert.equal(response.status, 403); assert.match((await response.json()).error, /X-Requested-With/); }); it('serves the single page application shell', async () => { const response = await app.fetch('/'); const body = await response.text(); assert.equal(response.status, 200); assert.match(response.headers.get('content-type'), /text\/html/); assert.match(body, /
{ const response = await app.fetch('/repos/FWS/FWS-Arch'); assert.equal(response.status, 200); assert.match(await response.text(), /
{ const response = await app.fetch('/core/dom.js'); assert.equal(response.status, 200); assert.match(response.headers.get('content-type'), /javascript/); }); it('never lets a module be cached, so versions cannot drift apart', async () => { for (const path of ['/app.js', '/core/ui.js', '/views/settings.js', '/styles.css']) { const response = await app.fetch(path); assert.equal(response.headers.get('cache-control'), 'no-store', `${path} peut ĂȘtre mis en cache`); } }); it('gives its assets a validator so a cache can revalidate', async () => { const response = await app.fetch('/core/dom.js'); assert.ok(response.headers.get('etag'), 'no ETag on a static asset'); assert.ok(response.headers.get('last-modified'), 'no Last-Modified on a static asset'); }); it('answers 304 when the cached copy is still current', async () => { const first = await app.fetch('/core/dom.js'); const etag = first.headers.get('etag'); const second = await app.fetch('/core/dom.js', { headers: { 'If-None-Match': etag } }); assert.equal(second.status, 304); assert.equal(await second.text(), ''); }); it('sends the file again when the validator does not match', async () => { const response = await app.fetch('/core/dom.js', { headers: { 'If-None-Match': '"stale"' } }); assert.equal(response.status, 200); assert.ok((await response.text()).length > 0); }); it('sends the security headers on every response', async () => { const response = await app.fetch('/'); assert.equal(response.headers.get('x-frame-options'), 'DENY'); assert.equal(response.headers.get('x-content-type-options'), 'nosniff'); assert.match(response.headers.get('content-security-policy'), /frame-ancestors 'none'/); }); it('never serves a file outside the public directory', async () => { // The path is normalised server side, so the traversal lands on the shell. const response = await app.fetch('/..%2f..%2fpackage.json'); const body = await response.text(); assert.equal(response.status, 200); assert.doesNotMatch(body, /"fws-discordbot"/); }); it('rejects a malformed JSON body', async () => { const response = await app.fetch('/api/auth/setup', { method: 'POST', headers: WRITE_HEADERS, body: 'pas du json', }); assert.equal(response.status, 400); }); }); describe('bootstrap mode', () => { let app; let token; before(async () => { initDatabase(':memory:'); token = generateRuntimeSetupToken(); app = serve({ client: null, setupOnly: true }); await app.ready; }); after(async () => { await app.close(); closeDatabase(); }); it('announces itself as a setup-only server', async () => { const body = await app.fetch('/api/health').then((response) => response.json()); assert.equal(body.setupOnly, true); }); it('does not even register the dashboard routes', async () => { assert.equal((await app.fetch('/api/overview')).status, 404); assert.equal((await app.fetch('/api/settings')).status, 404); }); it('refuses a wrong bootstrap token', async () => { const response = await app.fetch('/api/auth/setup', { method: 'POST', headers: WRITE_HEADERS, body: JSON.stringify({ token: 'mauvais' }), }); assert.equal(response.status, 403); }); it('opens a confined session with the right token', async () => { const response = await app.fetch('/api/auth/setup', { method: 'POST', headers: WRITE_HEADERS, body: JSON.stringify({ token }), }); assert.equal(response.status, 200); const cookie = response.headers.getSetCookie()[0].split(';')[0]; const me = await app.fetch('/api/me', { headers: { cookie } }).then((r) => r.json()); assert.equal(me.source, 'setup'); const configuration = await app.fetch('/api/configuration', { headers: { cookie } }); assert.equal(configuration.status, 200); assert.ok((await configuration.json()).sections.length > 0); }); it('serves the wizard front-end', async () => { const response = await app.fetch('/views/setup.js'); assert.equal(response.status, 200); }); });