45 lines
3.8 KiB
JavaScript
45 lines
3.8 KiB
JavaScript
import {execFileSync} from 'node:child_process';
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {mkdtemp,rm,access} from 'node:fs/promises';
|
|
import {tmpdir} from 'node:os';
|
|
import {join} from 'node:path';
|
|
import {Workspace,atomicJSON} from '../dist/workspace/index.js';
|
|
import {Collections} from '../dist/admin/collections.js';
|
|
import {Catalog} from '../dist/catalog/index.js';
|
|
import {investigate} from '../dist/core/engine.js';
|
|
import {writeReport} from '../dist/reports/html.js';
|
|
import {startServer} from '../dist/server/index.js';
|
|
test('local wiki enables management without login and preserves CSRF and origin checks',async t=>{
|
|
const root=await mkdtemp(join(tmpdir(),'phomber-admin-'));t.after(()=>rm(root,{recursive:true,force:true}));const w=new Workspace(root);await w.init();await atomicJSON(join(root,'library.json'),{schema_version:'1.0',articles:[]});
|
|
const v=await investigate({schema_version:'1.0',subject:{name:'Collection Person'},research:{offline:true}},w);await writeReport(v,w);const profile=(await new Catalog(w).read())[0];
|
|
const {server,url}=await startServer(w,0);t.after(()=>new Promise(r=>server.close(r)));
|
|
const management=await fetch(url+'/admin/collections/');assert.equal(management.status,200);assert.equal(management.headers.get('set-cookie'),null);
|
|
const html=await management.text(),csrf=html.match(/name="csrf" value="([a-f0-9]+)"/)[1];
|
|
assert.match(html,/Create collection/);
|
|
const home=await (await fetch(url+'/admin/')).text();assert.match(home,/Local admin mode is always enabled/);assert.ok(!/action="\/admin\/(claim|logout)"/.test(home));
|
|
const post=(path,data,origin=url)=>fetch(url+path,{method:'POST',redirect:'manual',headers:{Origin:origin,'Content-Type':'application/x-www-form-urlencoded'},body:new URLSearchParams({csrf,...data})});
|
|
const edit={action:'rename',profile:profile.id,title:'Edited immediately'};
|
|
assert.equal((await post('/manage/profiles',{...edit,csrf:'bad'})).status,403);
|
|
assert.equal((await post('/manage/profiles',edit,'https://evil.example')).status,403);
|
|
assert.equal((await post('/manage/profiles',edit)).status,303);
|
|
assert.equal((await new Catalog(w).read())[0].title,'Edited immediately');
|
|
assert.equal((await post('/manage/collections',{name:'Denied',csrf:'wrong'})).status,403);
|
|
assert.equal((await post('/manage/collections',{name:'Denied'},'https://evil.example')).status,403);
|
|
assert.equal((await post('/manage/collections',{name:'Public research',description:'Curated profiles',profiles:profile.id})).status,303);
|
|
let saved=(await new Collections(w).read())[0];assert.deepEqual(saved.profile_ids,[profile.id]);
|
|
const publicPage=await (await fetch(url+'/collections/')).text();assert.match(publicPage,/Public research/);
|
|
assert.match(await (await fetch(url+'/collections/'+saved.id+'/')).text(),/Edited immediately/);
|
|
assert.equal((await post('/manage/collections',{id:saved.id,name:'Updated collection',profiles:profile.id,archived:'yes'})).status,303);
|
|
assert.equal((await fetch(url+'/collections/'+saved.id+'/')).status,200);
|
|
assert.ok(!(await (await fetch(url+'/collections/')).text()).includes('Updated collection'));
|
|
assert.equal((await post('/manage/collections',{id:saved.id,name:'Bad',profiles:'missing'})).status,403);
|
|
assert.equal((await fetch(url+'/admin/claim/')).status,404);
|
|
assert.equal((await post('/admin/logout',{})).status,405);
|
|
const shortcut=execFileSync(process.execPath,['dist/cli/index.js','admin','--port',new URL(url).port],{env:{...process.env,PHOMBER_HOME:root},encoding:'utf8'}).trim();
|
|
assert.equal(shortcut,url+'/admin/');
|
|
await assert.rejects(()=>access(join(root,'admin-access.json')));
|
|
assert.equal((await w.load(v.id)).input.subject.name,'Collection Person');
|
|
assert.equal(await (await fetch(url+'/favicon.svg')).text(),await (await fetch(url+'/logo.svg')).text());
|
|
});
|