The schema test compares the declared variables against `.env.example`, so the two cannot drift apart. The server tests drive the real handler over a loopback socket rather than a mock, which is what caught the health probe missing from bootstrap mode and the deployments recorded under the wrong guild. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
180 lines
6.2 KiB
JavaScript
180 lines
6.2 KiB
JavaScript
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, /<div id="app"/);
|
|
});
|
|
|
|
it('falls back to the shell so a hard refresh works on any route', async () => {
|
|
const response = await app.fetch('/repos/FWS/FWS-Arch');
|
|
assert.equal(response.status, 200);
|
|
assert.match(await response.text(), /<div id="app"/);
|
|
});
|
|
|
|
it('serves the front-end modules with the right type', async () => {
|
|
const response = await app.fetch('/core/dom.js');
|
|
assert.equal(response.status, 200);
|
|
assert.match(response.headers.get('content-type'), /javascript/);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|