web/website/tests/dmpaint.test.mjs
2026-11-27 23:43:05 +00:00

34 lines
2.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createDMPaint} from '../control-panel/dmpaint.mjs';
import {accessibleCPMenu} from '../control-panel/public/cp-v2/cp-v2-access.mjs';
const request=(path,method='GET')=>new Request('https://cp.dunerealms.com'+path,{method});
const route=(user,staff=false)=>createDMPaint({}, {authenticate:async()=>user,authorise:async()=>staff});
test('the drawing service and vendor code require a registered session',async()=>{
for(const path of ['/dmpaint/','/api/dmpaint','/dmpaint/vendor/Editor.js'])assert.equal((await route(null)(request(path))).status,401);
assert.equal((await route(null)(request('/dmpaint/icon.png'))).status,200);
});
test('ordinary users can design logos; direct staff modes are rejected server-side',async()=>{
const handle=route({id:'citizen'});
assert.deepEqual((await (await handle(request('/api/dmpaint'))).json()).modes,['clan','company','character']);
assert.equal((await handle(request('/dmpaint/?mode=clan'))).status,200);
for(const mode of ['seal','award'])assert.equal((await handle(request('/dmpaint/?mode='+mode))).status,403);
});
test('existing staff permission enables seals and awards',async()=>{
const handle=route({id:'staff'},true);
assert.deepEqual((await (await handle(request('/api/dmpaint'))).json()).modes,['clan','company','character','seal','award']);
assert.equal((await handle(request('/dmpaint/?mode=seal'))).status,200);
});
test('no design upload or arbitrary file serving is exposed',async()=>{
const handle=route({id:'citizen'});
assert.equal((await handle(request('/api/dmpaint','POST'))).status,405);
for(const path of ['/dmpaint/vendor/%2e%2e/server.mjs','/dmpaint/vendor/index.html','/dmpaint/vendor/secret.env'])assert.equal((await handle(request(path))).status,404);
const response=await handle(request('/dmpaint/'));assert.match(response.headers.get('content-security-policy'),/script-src 'self';/);assert.match(response.headers.get('cache-control'),/private/);
});
test('every logged-in User CP has DMPaint, without widening staff access',()=>{
const source=readFileSync(new URL('../control-panel/public/cp-v2/v2.js',import.meta.url),'utf8'),menu=JSON.parse(source.slice(11,source.indexOf(';')));
const visible=accessibleCPMenu(menu,{permissions:[]});
assert.ok(visible.workspaces.find(w=>w.id==='realm').groups.some(g=>g.items.some(i=>i.id==='dmpaint')));
assert.ok(!visible.workspaces.find(w=>w.id==='bureau')?.groups.some(g=>g.items.some(i=>i.id==='seal-generator')));
});