Files
order-notification-automation/tests/storage/storage-preview-http.test.cjs
T

32 lines
5.0 KiB
JavaScript

'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');
const {Store}=require('../../src/modules/dashboard/store.cjs');const {StorageStore}=require('../../src/modules/storage/store.cjs');const {StorageService}=require('../../src/modules/storage/service.cjs');const {FileArea}=require('../../src/modules/storage/files.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');const {makePreviewFiles}=require('../fixtures/storage-preview-files.cjs');
async function setup(t){
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-preview-http-')),remote=path.join(dir,'remote');await fs.mkdir(remote);await makePreviewFiles(remote);
const store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');for(const role of ['operator','viewer'])store.addUser({username:role,password:'Test-password-123',role},'admin');
const storageStore=new StorageStore(path.join(dir,'storage')),storage=new StorageService({store:storageStore,jobs:store,outputDir:dir,client:{area:async()=>new FileArea(remote)}}),managed=await storage.saveServer({name:'托管服务器',host:'storage.company.test',share:'orders',directory:'',username:'archive',password:'Fixture-storage-password',enabled:true});
const server=createDashboard({store,storage,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));const base='http://127.0.0.1:'+server.address().port;
t.after(async()=>{await storage.stop();server.closeAllConnections();await new Promise(r=>server.close(r));storageStore.close();await fs.rm(dir,{recursive:true,force:true});});
const request=(route,cookie,extra={})=>fetch(base+route,{headers:{...(cookie?{Cookie:cookie}:{}),...extra}}),login=async username=>(await fetch(base+'/api/login',{method:'POST',headers:{Origin:base,'Content-Type':'application/json'},body:JSON.stringify({username,password:'Test-password-123'})})).headers.get('set-cookie').split(';')[0];
return {request,login,storage,managed,remote};
}
test('preview endpoints reuse authentication and root restrictions, with correct content and byte ranges',async t=>{
const {request,login,managed,storage,remote}=await setup(t),prefix='/api/storage/servers/'+managed.id;
const url=action=>prefix+'/'+action+'?path='+encodeURIComponent('说明.txt');assert.equal((await request(url('preview-info'))).status,401);const operator=await login('operator'),viewer=await login('viewer');assert.equal((await request(url('preview-info'),viewer)).status,403);
const info=await request(url('preview-info'),operator);assert.equal(info.status,200);assert.equal((await info.json()).kind,'text');
const text=await request(url('content'),operator);assert.equal(text.status,200);assert.match(text.headers.get('content-type'),/^text\/plain/);assert.match(await text.text(),/^<script>/);
const imageURL=prefix+'/content?path=sample.png',image=await request(imageURL,operator,{Range:'bytes=0-7'});assert.equal(image.status,206);assert.match(image.headers.get('content-range'),/^bytes 0-7\//);assert.deepEqual([...new Uint8Array(await image.arrayBuffer())],[137,80,78,71,13,10,26,10]);
assert.equal((await request(imageURL,operator,{Range:'bytes=999999-'})).status,416);
assert.equal((await request(prefix+'/content?path=bad.png',operator)).status,415);assert.equal((await request(prefix+'/preview-info?path=..%2Foutside.txt',operator)).status,400);
const outside=path.join(remote,'..','outside');await fs.mkdir(outside);await fs.writeFile(path.join(outside,'secret.txt'),'secret');await fs.symlink(outside,path.join(remote,'redirect'),'junction');assert.equal((await request(prefix+'/content?path=redirect%2Fsecret.txt',operator)).status,400);
const file=await request(prefix+'/file?path=unknown.bin',operator);assert.equal(file.status,200);assert.match(file.headers.get('content-disposition'),/^attachment/);assert.equal((await file.arrayBuffer()).byteLength,4);
const saved=storage.store.publicServer(managed.id);await storage.saveServer({...saved,password:'',enabled:false});assert.equal((await request(url('preview-info'),operator)).status,409);
});
test('preview shell isolates renderer permissions and serves all open-source assets locally',async t=>{
const {request}=await setup(t),page=await request('/storage-preview.html');assert.equal(page.status,200);const csp=page.headers.get('content-security-policy');assert.match(csp,/frame-ancestors 'self'/);assert.match(csp,/connect-src 'self'/);assert.doesNotMatch(csp,/https:/);
for(const route of ['/storage-preview.js','/storage-preview-office.js','/vendor/embedpdf/embedpdf.js','/vendor/embedpdf/pdfium.wasm','/vendor/docx/docx-preview.js','/vendor/jszip/jszip.min.js','/vendor/exceljs/exceljs.min.js','/vendor/photoswipe/photoswipe.esm.js','/vendor/photoswipe/photoswipe.css']){const asset=await request(route);assert.equal(asset.status,200,route);assert.ok((await asset.arrayBuffer()).byteLength>0);}
assert.equal((await request('/vendor/embedpdf/package.json')).status,404);assert.ok([401,404].includes((await request('/vendor/docx/../../package.json')).status));
});