import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { padIdFromUrl } from '../src/core/etherpad.ts'; const BASE = 'https://schulcloud.example.org'; describe('padIdFromUrl', () => { it('takes the pad id out of the url the server hands back', () => { assert.equal(padIdFromUrl(`${BASE}/etherpad/p/g.abc123$65f0e1d2c3b4a5968778695a`, BASE), 'g.abc123$65f0e1d2c3b4a5968778695a'); }); it('keeps the id exactly as encoded', () => { // Group pad ids contain `$`. Decoding or re-encoding the segment produces // an id Etherpad does not recognise. assert.equal(padIdFromUrl(`${BASE}/etherpad/p/g.x%24y`, BASE), 'g.x%24y'); }); it('refuses a url pointing at another host', () => { // The caller sends an Etherpad session cookie to whatever this returns, and // the url comes from server configuration — so a mismatch must not be // followed rather than trusted. assert.equal(padIdFromUrl('https://evil.test/etherpad/p/g.abc$123', BASE), undefined); }); it('accepts a differing port only when it matches', () => { assert.equal(padIdFromUrl('http://localhost:4400/etherpad/p/pad1', 'http://localhost:4400'), 'pad1'); assert.equal(padIdFromUrl('http://localhost:9001/etherpad/p/pad1', 'http://localhost:4400'), undefined); }); it('gives up on a url that is not a pad url', () => { assert.equal(padIdFromUrl(`${BASE}/dashboard`, BASE), undefined); assert.equal(padIdFromUrl(`${BASE}/etherpad/p/`, BASE), undefined); }); it('gives up rather than throwing on something that is not a url', () => { assert.equal(padIdFromUrl('not a url', BASE), undefined); assert.equal(padIdFromUrl('', BASE), undefined); }); });