feat(cms): download portable backups from site settings
DC CMS qualification / qualify (push) Successful in 1m57s
DC CMS qualification / qualify (push) Successful in 1m57s
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, mkdir, writeFile, readFile, rm, symlink, stat } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join, dirname } from 'node:path';
|
||||
import AdmZip from 'adm-zip';
|
||||
import { createSiteBackup, siteBackupFilename } from '../server/site-backup.mjs';
|
||||
|
||||
test('backup is a portable current workspace, with UTF-8 paths and exact media bytes', async t => {
|
||||
const root = await mkdtemp(join(tmpdir(), 'backup-fixture-'));
|
||||
t.after(() => rm(root, { recursive: true, force: true }));
|
||||
const files = {
|
||||
'content/pages/home.json': Buffer.from('{"title":"Current"}'),
|
||||
'templates/pages/home/shell.html': Buffer.from('<html>current</html>'),
|
||||
'assets/фото ровера.bin': Buffer.from(Array.from({ length: 700000 }, (_, i) => i % 251)),
|
||||
'.htaccess': Buffer.from('DirectoryIndex index.html'),
|
||||
'empty.txt': Buffer.alloc(0),
|
||||
};
|
||||
const privateFiles = ['.cms-backups/old.zip', '.git/config', '.env', '.env.local', 'demo.secrets.json', 'assets/.trash/old.png', 'assets/._photo', 'node_modules/package/a.js'];
|
||||
for (const [name, value] of [...Object.entries(files), ...privateFiles.map(name => [name, Buffer.from('private')])]) {
|
||||
await mkdir(dirname(join(root, name)), { recursive: true }); await writeFile(join(root, name), value);
|
||||
}
|
||||
const backup = await createSiteBackup(root, { required: ['content/pages/home.json'] });
|
||||
try {
|
||||
const zip = new AdmZip(await readFile(backup.path));
|
||||
assert.equal(zip.test(), true);
|
||||
assert.deepEqual(zip.getEntries().map(e => e.entryName).sort(), Object.keys(files).sort());
|
||||
for (const [name, value] of Object.entries(files)) assert.deepEqual(zip.readFile(name), value, name);
|
||||
assert.equal(backup.size, (await stat(backup.path)).size);
|
||||
assert.equal((await stat(backup.path)).mode & 0o777, 0o600);
|
||||
} finally { await backup.cleanup(); }
|
||||
await assert.rejects(stat(backup.path));
|
||||
assert.equal(await readFile(join(root, '.env'), 'utf8'), 'private');
|
||||
await assert.rejects(createSiteBackup(root, { required: ['missing.json'] }), /неполный/);
|
||||
await symlink(join(root, '.env'), join(root, 'linked-secret'));
|
||||
await assert.rejects(createSiteBackup(root), /символическими/);
|
||||
await rm(join(root, 'linked-secret'));
|
||||
const abort = new AbortController(); abort.abort();
|
||||
await assert.rejects(createSiteBackup(root, { signal: abort.signal }), { name: 'AbortError' });
|
||||
});
|
||||
|
||||
test('backup filename identifies the selected site and Moscow calendar date', () => {
|
||||
assert.equal(siteBackupFilename('mission-core', new Date('2026-11-05T20:59:00Z')), 'NODEDC_SITE_2026-11-05_MISSION_CORE_TECH.zip');
|
||||
assert.equal(siteBackupFilename('nodedc', new Date('2026-11-05T21:01:00Z')), 'NODEDC_SITE_2026-11-06_NODEDC_TECH.zip');
|
||||
});
|
||||
+27
-1
@@ -110,6 +110,31 @@ test('independent sites, rendering, secrets and publication ownership', async t
|
||||
assert.equal((await fetch(base+'/s/mission-core/api/project/upload-site',{method:'POST',body:invalid})).status,500);
|
||||
assert.equal((await api('/s/mission-core/api/page/home')).title,'Mission edited');
|
||||
});
|
||||
await t.test('downloaded backup is current, isolated and can be imported unchanged',async()=>{
|
||||
const beforePage=await api('/s/mission-core/api/page/home');
|
||||
const prepared=await api('/s/mission-core/api/project/backup','POST');
|
||||
await api('/s/mission-core/api/project/backup','POST',undefined,409);
|
||||
const response=await fetch(base+'/s/mission-core'+prepared.downloadUrl);
|
||||
assert.equal(response.status,200);assert.equal(response.headers.get('content-type'),'application/zip');
|
||||
assert.match(response.headers.get('content-disposition'),/MISSION_CORE_TECH\.zip/);
|
||||
assert.equal(response.headers.get('cache-control'),'no-store');
|
||||
const bytes=Buffer.from(await response.arrayBuffer());const zip=new AdmZip(bytes);
|
||||
assert.equal(bytes.length,Number(response.headers.get('content-length')));assert.equal(zip.test(),true);
|
||||
assert.equal((await fetch(base+'/s/mission-core'+prepared.downloadUrl)).status,404);
|
||||
assert.deepEqual(JSON.parse(zip.readAsText('content/pages/home.json')),beforePage);
|
||||
assert.ok(zip.getEntry('tools/render-home.mjs'));assert.ok(zip.getEntry('templates/pages/home/shell.html'));
|
||||
assert.ok(!zip.getEntries().some(e=>/\.cms-backups|\.git\/|secrets|publish-state/.test(e.entryName)));
|
||||
const preparedAgent=await api('/s/nodedc/api/project/backup','POST');
|
||||
assert.equal((await fetch(base+'/s/mission-core'+preparedAgent.downloadUrl)).status,404);
|
||||
const agentResponse=await fetch(base+'/s/nodedc'+preparedAgent.downloadUrl);assert.equal(agentResponse.status,200);
|
||||
const agentZip=new AdmZip(Buffer.from(await agentResponse.arrayBuffer()));
|
||||
assert.equal(JSON.parse(agentZip.readAsText('content/pages/home.json')).title,'Agent edited');
|
||||
const form=new FormData();form.append('file',new Blob([bytes],{type:'application/zip'}),'restored.zip');
|
||||
const restored=await fetch(base+'/s/mission-core/api/project/upload-site',{method:'POST',body:form});
|
||||
assert.equal(restored.status,200);assert.equal((await restored.json()).site.ready,true);
|
||||
assert.deepEqual(await api('/s/mission-core/api/page/home'),beforePage);
|
||||
assert.equal((await api('/s/nodedc/api/page/home')).title,'Agent edited');
|
||||
});
|
||||
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}});
|
||||
@@ -139,7 +164,8 @@ test('independent sites, rendering, secrets and publication ownership', async t
|
||||
});
|
||||
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/);}
|
||||
for(const path of ['/api/sites','/s/mission-core/api/page/home','/s/mission-core/api/project/backup']){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/api/project/backup',{method:'POST',redirect:'manual'})).status,401);
|
||||
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