feat(storage): require readable connections before adding servers

This commit is contained in:
阿白叔 committed 2026-10-17 14:12:27 +08:00
1 parent b34d51432d
commit 8417b1dff8
11 files changed
+100 -20

No files matched your search

+2 -2
View File
@@ -5,7 +5,7 @@ const {Store}=require('../src/dashboard-store.cjs');const {StorageStore}=require
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=storage.saveServer({name:'托管服务器',host:'storage.company.test',share:'orders',directory:'',username:'archive',password:'Fixture-storage-password',enabled:true});
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];
@@ -21,7 +21,7 @@ test('preview endpoints reuse authentication and root restrictions, with correct
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);storage.saveServer({...saved,password:'',enabled:false});assert.equal((await request(url('preview-info'),operator)).status,409);
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=>{