Isolate CMS sites, transfer publication settings and qualify Git releases
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, mkdir, readFile, writeFile, rm, stat } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join, resolve } from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { once } from 'node:events';
|
||||
|
||||
test('independent sites, rendering, secrets and publication ownership', async t => {
|
||||
const root = await mkdtemp(join(tmpdir(), 'dc-cms-test-'));
|
||||
const projects = join(root, 'projects'); const original = join(root, 'original');
|
||||
await mkdir(projects);
|
||||
const fixture = {
|
||||
'content/pages/home.json': JSON.stringify({title:'Original',regions:[]}),
|
||||
'content/knowledge.json': JSON.stringify({title:'Original knowledge',tree:[]}),
|
||||
'content/block-templates/home.json':'[]',
|
||||
'templates/pages/home/shell.html':'<html></html>',
|
||||
'tools/render-home.mjs': 'import {readFile,writeFile} from "node:fs/promises"; const page=JSON.parse(await readFile("content/pages/home.json"));await writeFile("index.html",`<h1>${page.title}</h1>`);',
|
||||
'tools/render-knowledge.mjs':'console.log("knowledge rendered")',
|
||||
'assets/proof.txt':'original-asset',
|
||||
'.git/private':'excluded git', '.cms-backups/old.zip':'excluded backup',
|
||||
'index.html':'<h1>Original</h1>',
|
||||
'knowledge/index.html':'<a href="/knowledge/fleet/">Fleet</a><img src="/assets/test.png"><a href="https://nodedc.ru/">Public</a><script type="application/json">{"url":"/knowledge/fleet/"}</script>',
|
||||
};
|
||||
for(const [path,content] of Object.entries(fixture)){await mkdir(join(original,path,'..'),{recursive:true});await writeFile(join(original,path),content);}
|
||||
await writeFile(join(projects,'nodedc.json'),JSON.stringify({id:'nodedc',name:'NODE.DC — Agent',siteRoot:original,publicUrl:'https://agent.nodedc.ru/',deploy:{enabled:false}}));
|
||||
let child,base;
|
||||
async function start(auth=false){
|
||||
child=spawn(process.execPath,['server/admin-server.mjs'],{cwd:resolve('.'),env:{...process.env,HOST:'127.0.0.1',PORT:'0',CMS_PROJECT:'nodedc',CMS_PROJECTS_ROOT:projects,CMS_AUTH_ENABLED:String(auth),CMS_SESSION_SECRET:'test-key',CMS_BASE_URL:''},stdio:['ignore','pipe','pipe']});
|
||||
let err='';child.stderr.on('data',b=>err+=b);
|
||||
const address=await new Promise((done,reject)=>{
|
||||
const timer=setTimeout(()=>reject(new Error('server timeout: '+err)),5000);
|
||||
child.stdout.on('data',data=>{const line=data.toString().match(/DC CMS multisite: (\{.*\})/);if(line){clearTimeout(timer);done(JSON.parse(line[1]));}});
|
||||
child.once('exit',code=>{clearTimeout(timer);reject(new Error(`server exit ${code}: ${err}`));});
|
||||
});base=`http://127.0.0.1:${address.port}`;
|
||||
}
|
||||
async function stop(){if(child?.exitCode===null){child.kill();await once(child,'exit');}}
|
||||
t.after(async()=>{await stop();await rm(root,{recursive:true,force:true});});
|
||||
await start();
|
||||
async function api(path,method='GET',payload,expected=200){
|
||||
const response=await fetch(base+path,{method,headers:payload?{'content-type':'application/json'}:{},body:payload?JSON.stringify(payload):undefined});
|
||||
const data=await response.json(); assert.equal(response.status,expected,JSON.stringify(data));return data;
|
||||
}
|
||||
await t.test('create clone without credentials or backups',async()=>{
|
||||
const config=await api('/s/nodedc/api/project/config');
|
||||
config.deploy={enabled:false,method:'sftp',host:'host.invalid',username:'user',port:22,remotePath:'httpdocs',password:'test-password'};
|
||||
await api('/s/nodedc/api/project/config','PUT',config);
|
||||
const created=await api('/api/sites','POST',{id:'mission-core',name:'NODE.DC — Mission Core',copyFrom:'nodedc',publicUrl:'https://nodedc.ru/'},201);
|
||||
assert.equal(created.project.deploy.enabled,false); assert.equal(created.project.deploy.host,'');assert.equal(created.project.deploy.passwordConfigured,false);
|
||||
await assert.rejects(stat(join(projects,'sites/mission-core/.git')));await assert.rejects(stat(join(projects,'sites/mission-core/.cms-backups')));
|
||||
assert.equal((await api('/api/sites')).sites.length,2);
|
||||
});
|
||||
await t.test('concurrent edits and media remain site-specific',async()=>{
|
||||
await Promise.all([api('/s/nodedc/api/page/home','PUT',{title:'Agent edited',regions:[]}),api('/s/mission-core/api/page/home','PUT',{title:'Mission edited',regions:[]})]);
|
||||
assert.equal((await api('/s/nodedc/api/page/home')).title,'Agent edited');assert.equal((await api('/s/mission-core/api/page/home')).title,'Mission edited');
|
||||
await writeFile(join(projects,'sites/mission-core/assets/proof.txt'),'mission-asset');
|
||||
assert.equal(await (await fetch(base+'/s/nodedc/assets/proof.txt')).text(),'original-asset');
|
||||
assert.equal(await (await fetch(base+'/s/mission-core/assets/proof.txt')).text(),'mission-asset');
|
||||
await api('/s/mission-core/api/knowledge','PUT',{title:'Mission knowledge',tree:[]});
|
||||
assert.equal((await api('/s/nodedc/api/knowledge')).title,'Original knowledge');
|
||||
});
|
||||
await t.test('render is isolated and never implicitly publishes',async()=>{
|
||||
await api('/s/mission-core/api/render/home','POST');
|
||||
assert.equal(await readFile(join(original,'index.html'),'utf8'),'<h1>Original</h1>');
|
||||
assert.equal(await (await fetch(base+'/s/mission-core/index.html')).text(),'<h1>Mission edited</h1>');
|
||||
});
|
||||
await t.test('preview root links stay within their selected site',async()=>{
|
||||
const page=await (await fetch(base+'/s/mission-core/knowledge/')).text();
|
||||
assert.match(page,/href="\/s\/mission-core\/knowledge\/fleet\/"/);
|
||||
assert.match(page,/src="\/s\/mission-core\/assets\/test.png"/);
|
||||
assert.match(page,/"url":"\/s\/mission-core\/knowledge\/fleet\/"/);
|
||||
assert.match(page,/href="https:\/\/nodedc.ru\/"/);
|
||||
assert.ok(!(await readFile(join(projects,'sites/mission-core/knowledge/index.html'),'utf8')).includes('/s/mission-core'));
|
||||
});
|
||||
await t.test('identity and workspace paths cannot be changed by settings',async()=>{
|
||||
const config=await api('/s/mission-core/api/project/config');
|
||||
const result=await api('/s/mission-core/api/project/config','PUT',{...config,id:'nodedc',siteRoot:original});
|
||||
assert.equal(result.project.id,'mission-core'); assert.equal(result.project.runtime.siteRoot,join(projects,'sites/mission-core'));
|
||||
await api('/api/sites','POST',{id:'../escape',copyFrom:'nodedc'},400);
|
||||
await api('/api/sites','POST',{id:'mission-core',copyFrom:'nodedc'},409);
|
||||
assert.equal((await fetch(base+'/s/missing/assets/proof.txt')).status,404);
|
||||
assert.equal((await fetch(base+'/s/nodedc/.git/private')).status,403);
|
||||
});
|
||||
await t.test('encrypted credentials transfer with source disabled',async()=>{
|
||||
const config=await api('/s/nodedc/api/project/config');
|
||||
await api('/s/nodedc/api/project/config','PUT',{...config,deploy:{...config.deploy,enabled:true}});
|
||||
const result=await api('/api/sites/mission-core/deploy-transfer','POST',{fromSiteId:'nodedc'});
|
||||
assert.equal(result.project.deploy.host,'host.invalid'); assert.equal(result.project.deploy.enabled,false);assert.equal(result.project.deploy.passwordConfigured,true);
|
||||
assert.equal((await api('/s/nodedc/api/project/config')).deploy.enabled,false);
|
||||
assert.ok(!JSON.stringify(result).includes('test-password'));
|
||||
const secret=await readFile(join(projects,'mission-core.secrets.json'),'utf8');assert.ok(!secret.includes('test-password'));
|
||||
assert.equal((await stat(join(projects,'mission-core.secrets.json'))).mode & 0o777,0o600);
|
||||
});
|
||||
await t.test('same or nested publication targets cannot both be enabled',async()=>{
|
||||
const target=await api('/s/mission-core/api/project/config');
|
||||
await api('/s/mission-core/api/project/config','PUT',{...target,deploy:{...target.deploy,enabled:true}});
|
||||
const source=await api('/s/nodedc/api/project/config');
|
||||
await api('/s/nodedc/api/project/config','PUT',{...source,deploy:{...source.deploy,enabled:true,remotePath:'httpdocs/nested'}},409);
|
||||
assert.equal((await api('/s/nodedc/api/project/config')).deploy.enabled,false);
|
||||
// An enabled target without deployOnRender only renders, no network call.
|
||||
await api('/s/mission-core/api/render/home','POST');
|
||||
await api('/s/nodedc/api/project/publish','POST',undefined,500);
|
||||
});
|
||||
await t.test('cross-origin registry mutations are refused',async()=>{
|
||||
const response=await fetch(base+'/api/sites',{method:'POST',headers:{origin:'https://untrusted.invalid','content-type':'application/json'},body:JSON.stringify({id:'attack',copyFrom:'nodedc'})});assert.equal(response.status,403);
|
||||
});
|
||||
await stop(); await start();
|
||||
await t.test('registry and edits survive server restart',async()=>{
|
||||
assert.equal((await api('/api/sites')).sites.length,2);assert.equal((await api('/s/mission-core/api/page/home')).title,'Mission edited');assert.equal((await api('/s/nodedc/api/page/home')).title,'Agent edited');
|
||||
});
|
||||
await stop();await start(true);
|
||||
await t.test('registry, scoped content and preview retain authentication',async()=>{
|
||||
for(const path of ['/api/sites','/s/mission-core/api/page/home']){const response=await fetch(base+path,{redirect:'manual'});assert.equal(response.status,401,path);assert.match(response.headers.get('content-type'),/json/);}
|
||||
assert.equal((await fetch(base+'/s/mission-core/index.html',{redirect:'manual'})).status,302);
|
||||
assert.equal((await fetch(base+'/healthz')).status,200);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user