feat(wiki): add the reading site and its editor
The renderer escapes HTML before any transformation and drops `javascript:` links. Pages are written by the team, but a renderer that relies on that is one mistake away from being a hole. The editor previews as you type: legal pages are long, and writing Markdown blind is how broken tables end up published. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
7 files changed
+1031
No files matched your search
@@ -0,0 +1,215 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createServer } from 'node:http';
|
||||
import { after, before, beforeEach, describe, it } from 'node:test';
|
||||
import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js';
|
||||
import {
|
||||
createPage,
|
||||
getPageBySlug,
|
||||
listPublished,
|
||||
removePage,
|
||||
searchPublished,
|
||||
updatePage,
|
||||
} from '../src/database/repositories/wiki-repository.js';
|
||||
import { render } from '../src/web/public/core/markdown.js';
|
||||
import { createHandler } from '../src/web/server.js';
|
||||
import { seedWiki } from '../src/web/services/wiki-seed.js';
|
||||
|
||||
/** Public wiki and its Markdown renderer (M17). */
|
||||
|
||||
describe('markdown renderer', () => {
|
||||
it('escapes HTML before doing anything else', () => {
|
||||
const { html } = render('<script>alert(1)</script>');
|
||||
assert.doesNotMatch(html, /<script>/);
|
||||
assert.match(html, /<script>/);
|
||||
});
|
||||
|
||||
it('escapes HTML inside a heading and a table cell too', () => {
|
||||
assert.doesNotMatch(render('## <img src=x onerror=1>').html, /<img src=x/);
|
||||
assert.doesNotMatch(render('| a |\n| --- |\n| <b>x</b> |').html, /<b>x<\/b>/);
|
||||
});
|
||||
|
||||
it('refuses a javascript: link', () => {
|
||||
const { html } = render('[clique](javascript:alert(1))');
|
||||
assert.doesNotMatch(html, /javascript:/);
|
||||
assert.match(html, /href="#"/);
|
||||
});
|
||||
|
||||
it('keeps http links and opens them in a new tab', () => {
|
||||
const { html } = render('[Discord](https://discord.com/terms)');
|
||||
assert.match(html, /href="https:\/\/discord\.com\/terms"/);
|
||||
assert.match(html, /rel="noreferrer noopener"/);
|
||||
});
|
||||
|
||||
it('renders headings with anchors and collects them', () => {
|
||||
const { html, headings } = render('## Durée de conservation\n### Détail');
|
||||
assert.match(html, /<h2 id="duree-de-conservation">/);
|
||||
assert.deepEqual(
|
||||
headings.map((heading) => heading.id),
|
||||
['duree-de-conservation', 'detail'],
|
||||
);
|
||||
});
|
||||
|
||||
it('renders a table', () => {
|
||||
const { html } = render('| Donnée | Quand |\n| --- | --- |\n| Pseudo | À la connexion |');
|
||||
assert.match(html, /<th>Donnée<\/th>/);
|
||||
assert.match(html, /<td>À la connexion<\/td>/);
|
||||
});
|
||||
|
||||
it('renders lists, quotes and fenced code verbatim', () => {
|
||||
assert.match(render('- un\n- deux').html, /<ul>\n<li>un<\/li>/);
|
||||
assert.match(render('1. un\n2. deux').html, /<ol>\n<li>un<\/li>/);
|
||||
assert.match(render('> attention').html, /<blockquote>attention<\/blockquote>/);
|
||||
assert.match(render('```\nnpm run setup\n```').html, /<pre><code>npm run setup<\/code><\/pre>/);
|
||||
});
|
||||
|
||||
it('renders inline code and emphasis', () => {
|
||||
const { html } = render('Voir `LOG_RETENTION_DAYS` et **le reste**.');
|
||||
assert.match(html, /<code>LOG_RETENTION_DAYS<\/code>/);
|
||||
assert.match(html, /<strong>le reste<\/strong>/);
|
||||
});
|
||||
|
||||
it('survives an empty page', () => {
|
||||
assert.deepEqual(render(''), { html: '', headings: [] });
|
||||
assert.deepEqual(render(undefined), { html: '', headings: [] });
|
||||
});
|
||||
});
|
||||
|
||||
describe('wiki repository', () => {
|
||||
before(() => initDatabase(':memory:'));
|
||||
after(() => closeDatabase());
|
||||
beforeEach(() => getDatabase().exec('DELETE FROM wiki_pages;'));
|
||||
|
||||
const page = (overrides = {}) =>
|
||||
createPage({ slug: 'guide', title: 'Guide', content: 'Bonjour', published: true, ...overrides });
|
||||
|
||||
it('stores and reads a page back', () => {
|
||||
const created = page();
|
||||
assert.equal(created.published, true);
|
||||
assert.equal(created.protected, false);
|
||||
assert.deepEqual(getPageBySlug('guide'), created);
|
||||
});
|
||||
|
||||
it('lists only the published pages, in reading order', () => {
|
||||
page({ slug: 'b', title: 'B', category: 'Général', position: 2 });
|
||||
page({ slug: 'a', title: 'A', category: 'Général', position: 1 });
|
||||
page({ slug: 'draft', title: 'Brouillon', published: false });
|
||||
|
||||
assert.deepEqual(
|
||||
listPublished().map((entry) => entry.slug),
|
||||
['a', 'b'],
|
||||
);
|
||||
});
|
||||
|
||||
it('searches titles and bodies', () => {
|
||||
page({ slug: 'vie-privee', title: 'Confidentialité', content: 'Conservation des données' });
|
||||
assert.equal(searchPublished('conservation').length, 1);
|
||||
assert.equal(searchPublished('introuvable').length, 0);
|
||||
});
|
||||
|
||||
it('refuses to delete a protected page', () => {
|
||||
const locked = page({ slug: 'legal', isProtected: true });
|
||||
assert.equal(removePage(locked.id), 0);
|
||||
assert.ok(getPageBySlug('legal'));
|
||||
});
|
||||
|
||||
it('records who last changed a page', () => {
|
||||
const created = page();
|
||||
const updated = updatePage(created.id, { title: 'Guide revu' }, 'u1');
|
||||
assert.equal(updated.title, 'Guide revu');
|
||||
assert.equal(updated.updated_by, 'u1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('wiki seeding', () => {
|
||||
before(() => initDatabase(':memory:'));
|
||||
after(() => closeDatabase());
|
||||
beforeEach(() => getDatabase().exec('DELETE FROM wiki_pages;'));
|
||||
|
||||
it('creates the welcome page and both legal pages', () => {
|
||||
assert.equal(seedWiki(), 3);
|
||||
assert.ok(getPageBySlug('conditions-utilisation').published);
|
||||
assert.ok(getPageBySlug('politique-confidentialite').published);
|
||||
});
|
||||
|
||||
it('marks the legal pages as protected', () => {
|
||||
seedWiki();
|
||||
assert.equal(getPageBySlug('politique-confidentialite').protected, true);
|
||||
assert.equal(getPageBySlug('accueil').protected, false);
|
||||
});
|
||||
|
||||
it('names the Swiss law it is written against', () => {
|
||||
seedWiki();
|
||||
const privacy = getPageBySlug('politique-confidentialite').content;
|
||||
assert.match(privacy, /LPD/);
|
||||
assert.match(privacy, /PFPDT/);
|
||||
assert.match(privacy, /art\. 25 LPD/);
|
||||
});
|
||||
|
||||
it('never overwrites what the organisation has written', () => {
|
||||
seedWiki();
|
||||
updatePage(getPageBySlug('conditions-utilisation').id, { content: 'Notre propre texte.' });
|
||||
|
||||
assert.equal(seedWiki(), 0);
|
||||
assert.equal(getPageBySlug('conditions-utilisation').content, 'Notre propre texte.');
|
||||
});
|
||||
|
||||
it('brings a deleted legal page back, but not the welcome one', () => {
|
||||
seedWiki();
|
||||
getDatabase().exec("DELETE FROM wiki_pages WHERE slug IN ('politique-confidentialite', 'accueil')");
|
||||
|
||||
assert.equal(seedWiki(), 1);
|
||||
assert.ok(getPageBySlug('politique-confidentialite'));
|
||||
assert.equal(getPageBySlug('accueil'), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('wiki over HTTP', () => {
|
||||
let app;
|
||||
|
||||
before(async () => {
|
||||
initDatabase(':memory:');
|
||||
seedWiki();
|
||||
app = createServer(createHandler({ client: null, setupOnly: false }));
|
||||
await new Promise((resolve) => app.listen(0, '127.0.0.1', resolve));
|
||||
});
|
||||
after(async () => {
|
||||
await new Promise((resolve) => app.close(resolve));
|
||||
closeDatabase();
|
||||
});
|
||||
|
||||
const get = (path) => fetch(`http://127.0.0.1:${app.address().port}${path}`);
|
||||
|
||||
it('serves the page tree without a session', async () => {
|
||||
const response = await get('/api/wiki');
|
||||
const body = await response.json();
|
||||
|
||||
assert.equal(response.status, 200);
|
||||
assert.ok(body.categories.some((category) => category.name === 'Légal'));
|
||||
});
|
||||
|
||||
it('serves a published page without a session', async () => {
|
||||
const response = await get('/api/wiki/pages/conditions-utilisation');
|
||||
assert.equal(response.status, 200);
|
||||
assert.match((await response.json()).content, /droit suisse/);
|
||||
});
|
||||
|
||||
it('hides a draft even when its address is known', async () => {
|
||||
createPage({ slug: 'secret', title: 'Secret', content: 'chut', published: false });
|
||||
assert.equal((await get('/api/wiki/pages/secret')).status, 404);
|
||||
});
|
||||
|
||||
it('searches without a session', async () => {
|
||||
const body = await get('/api/wiki/search?q=confidentialite').then((response) => response.json());
|
||||
assert.ok(Array.isArray(body.results));
|
||||
});
|
||||
|
||||
it('keeps the administration behind the login', async () => {
|
||||
assert.equal((await get('/api/wiki/admin')).status, 401);
|
||||
});
|
||||
|
||||
it('serves its own page rather than the dashboard shell', async () => {
|
||||
const body = await get('/wiki/conditions-utilisation').then((response) => response.text());
|
||||
assert.match(body, /wiki\.js/);
|
||||
assert.doesNotMatch(body, /id="app"/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user