feat(storage): browse SMB shares and bound connection checks
This commit is contained in:
1 parent
8417b1dff8
commit
a785fa73d4
15 files changed
+277
-39
No files matched your search
@@ -0,0 +1,29 @@
|
||||
const {test}=require('node:test'),assert=require('node:assert/strict');
|
||||
const {StorageService}=require('../src/storage-service.cjs');
|
||||
test('a stalled connection check times out and releases pending state',async()=>{
|
||||
const service=new StorageService({store:{prepareServer:()=>({saved:{host:'server',share:'orders'}})},client:{area:async()=>({list:()=>new Promise(()=>{})})},checkTimeoutMs:40});
|
||||
await assert.rejects(service.checkConnection({}),e=>e.status===504);assert.equal(service.pendingChecks.size,0);await service.stop();
|
||||
});
|
||||
test('cancelled connection checks settle without saving',async()=>{
|
||||
const controller=new AbortController(),service=new StorageService({store:{prepareServer:()=>({saved:{host:'server',share:'orders'}})},client:{area:async()=>({list:()=>new Promise(()=>{})})},checkTimeoutMs:100});
|
||||
const promise=service.checkConnection({},{signal:controller.signal});controller.abort();await assert.rejects(promise,e=>e.status===499);assert.equal(service.pendingChecks.size,0);await service.stop();
|
||||
});
|
||||
|
||||
test('shutdown aborts a pending read instead of waiting for the network',async()=>{
|
||||
const service=new StorageService({store:{prepareServer:()=>({saved:{host:'server',share:'orders'}})},client:{area:async()=>({list:()=>new Promise(()=>{})})},checkTimeoutMs:10000});
|
||||
const checked=assert.rejects(service.checkConnection({}),e=>e.status===503);await service.stop();await checked;assert.equal(service.pendingChecks.size,0);
|
||||
});
|
||||
|
||||
test('failed Windows tree termination falls back and waits for worker closure',{skip:process.platform!=='win32'},async t=>{
|
||||
const {EventEmitter}=require('node:events'),{PassThrough}=require('node:stream'),cp=require('node:child_process');let kills=0,closed=false;
|
||||
const child=new EventEmitter();child.pid=123;child.stdin=new PassThrough();child.stdout=new PassThrough();child.stderr=new PassThrough();child.kill=()=>{kills++;queueMicrotask(()=>{closed=true;child.emit('close',1);});};
|
||||
t.mock.method(cp,'spawn',(file)=>{if(file===process.execPath)return child;const killer=new EventEmitter();queueMicrotask(()=>killer.emit('exit',1));return killer;});
|
||||
const key=require.resolve('../src/smb-probe.cjs');delete require.cache[key];t.after(()=>delete require.cache[key]);const {probe}=require(key);
|
||||
await assert.rejects(probe({host:'server'},{timeoutMs:5}),e=>e.status===504);assert.equal(kills,1);assert.equal(closed,true);
|
||||
});
|
||||
|
||||
test('share enumeration reserves enough bounded output for hundreds of Unicode names',async t=>{
|
||||
const cp=require('node:child_process'),names=Array.from({length:501},(_,i)=>'共享目录'.repeat(10)+i),payload=JSON.stringify({ok:true,shares:names});
|
||||
t.mock.method(cp,'execFile',(file,args,options,callback)=>{assert.ok(options.maxBuffer>=Buffer.byteLength(payload));return {stdin:{on(){},end(){callback(null,payload);}}};});
|
||||
const key=require.resolve('../src/smb-native.cjs');delete require.cache[key];t.after(()=>delete require.cache[key]);const {listShares}=require(key);assert.deepEqual(await listShares({host:'server',authMode:'windows'}),names);
|
||||
});
|
||||
@@ -2,7 +2,7 @@
|
||||
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/dashboard-store.cjs');const {StorageStore}=require('../src/storage-store.cjs');const {StorageService}=require('../src/storage-service.cjs');const {FileArea}=require('../src/storage-files.cjs');const {createDashboard}=require('../src/dashboard.cjs');
|
||||
const config={name:'210',host:'storage.company.test',share:'orders',directory:'',username:'archive',password:'Private-storage-password',enabled:true};
|
||||
async function fixture(t){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-storage-http-'));const outputDir=path.join(dir,'pdf'),remote=path.join(dir,'remote');await fs.mkdir(outputDir);await fs.mkdir(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'));const storage=new StorageService({store:storageStore,jobs:store,outputDir,client:{area:async()=>new FileArea(remote)}});const server=createDashboard({store,storage,outputDir});await new Promise(r=>server.listen(0,'127.0.0.1',r));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 base='http://127.0.0.1:'+server.address().port;const call=(route,body,cookie,origin=base)=>fetch(base+route,{method:body===undefined?'GET':'POST',headers:{...(body===undefined?{}:{'Content-Type':'application/json',Origin:origin}),...(cookie?{Cookie:cookie}:{})},body:body===undefined?undefined:JSON.stringify(body)});const login=async username=>(await call('/api/login',{username,password:'Test-password-123'})).headers.get('set-cookie').split(';')[0];return {dir,outputDir,remote,store,storage,storageStore,call,login};}
|
||||
async function fixture(t){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-storage-http-'));const outputDir=path.join(dir,'pdf'),remote=path.join(dir,'remote');await fs.mkdir(outputDir);await fs.mkdir(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'));const storage=new StorageService({store:storageStore,jobs:store,outputDir,client:{area:async()=>new FileArea(remote),browse:async server=>server.share?{...await new FileArea(remote).list(''),serverRoot:false,readable:true,address:'shared'}:{serverRoot:true,readable:false,address:String.fromCharCode(92).repeat(2)+server.host,entries:[{name:'orders',type:'directory',address:String.fromCharCode(92).repeat(2)+server.host+String.fromCharCode(92)+'orders'}]}}});const server=createDashboard({store,storage,outputDir});await new Promise(r=>server.listen(0,'127.0.0.1',r));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 base='http://127.0.0.1:'+server.address().port;const call=(route,body,cookie,origin=base)=>fetch(base+route,{method:body===undefined?'GET':'POST',headers:{...(body===undefined?{}:{'Content-Type':'application/json',Origin:origin}),...(cookie?{Cookie:cookie}:{})},body:body===undefined?undefined:JSON.stringify(body)});const login=async username=>(await call('/api/login',{username,password:'Test-password-123'})).headers.get('set-cookie').split(';')[0];return {dir,outputDir,remote,store,storage,storageStore,call,login};}
|
||||
|
||||
test('storage HTTP enforces roles, redacts passwords and archives only successful in-root PDFs',async t=>{
|
||||
const {dir,outputDir,store,storageStore,call,login}=await fixture(t);assert.equal((await call('/api/storage')).status,401);
|
||||
@@ -50,3 +50,11 @@ test('draft connection check does not save, and unreadable storage cannot be add
|
||||
assert.equal((await call('/api/storage/servers',unavailable,admin)).status,502);assert.equal(storageStore.servers()[0].version,server.version);assert.equal(storageStore.servers()[0].name,server.name);
|
||||
const disabled=await call('/api/storage/servers',{...server,password:'',enabled:false},admin);assert.equal(disabled.status,200);const reenable=await call('/api/storage/servers',{...await disabled.json(),password:'',enabled:true},admin);assert.equal(reenable.status,502);assert.equal(storageStore.servers()[0].enabled,false);
|
||||
});
|
||||
|
||||
test('server-only input lists shares without allowing server-root saves',async t=>{
|
||||
const {call,login,storageStore}=await fixture(t),admin=await login('admin'),operator=await login('operator'),input={address:'storage.company.test',authMode:'windows',enabled:true};
|
||||
assert.equal((await call('/api/storage/servers/browse',input,operator)).status,403);assert.equal((await call('/api/storage/servers/browse',input,admin,'https://evil.test')).status,403);
|
||||
const browse=await call('/api/storage/servers/browse',input,admin);assert.equal(browse.status,200);const result=await browse.json();assert.equal(result.serverRoot,true);assert.equal(result.entries[0].name,'orders');assert.equal(result.password,undefined);
|
||||
const check=await call('/api/storage/servers/check',input,admin);assert.equal(check.status,200);assert.equal((await check.json()).readable,false);assert.equal((await call('/api/storage/servers',input,admin)).status,400);assert.equal(storageStore.servers().length,0);
|
||||
const selected={...input,address:result.entries[0].address};const saved=await call('/api/storage/servers',selected,admin);assert.equal(saved.status,201);assert.equal(storageStore.servers().length,1);
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs/promises'),os=require('node:os'),path=require('node:path');
|
||||
const {Store}=require('../src/dashboard-store.cjs'),{StorageStore}=require('../src/storage-store.cjs'),{StorageService}=require('../src/storage-service.cjs'),{FileArea,segments}=require('../src/storage-files.cjs'),{createDashboard}=require('../src/dashboard.cjs'),{loadPlaywright,browserOptions}=require('../src/runtime.cjs');
|
||||
test('directory picker selects shares and folders; cancel and timeout unlock the form',{timeout:150000},async t=>{
|
||||
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'storage-picker-')),remote=path.join(dir,'remote');await fs.mkdir(path.join(remote,'子目录'),{recursive:true});const store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');const storageStore=new StorageStore(path.join(dir,'storage'));let aborted=0;
|
||||
const slash=String.fromCharCode(92),address=(server)=>slash.repeat(2)+server.host+(server.share?slash+[server.share,...segments(server.directory)].join(slash):'');
|
||||
const client={area:async server=>new FileArea(path.join(remote,...segments(server.directory)),{boundary:remote}),browse:async(server,{signal})=>{
|
||||
if(server.host==='slow.test'){signal.addEventListener('abort',()=>aborted++,{once:true});return new Promise(()=>{});}
|
||||
const current=address(server);if(!server.share)return {serverRoot:true,readable:false,address:current,parent:null,entries:[{name:'orders',address:current+slash+'orders'}]};
|
||||
const listing=await(await client.area(server)).list(''),parts=[server.share,...segments(server.directory)];return {serverRoot:false,readable:true,address:current,parent:slash.repeat(2)+server.host+(parts.length>1?slash+parts.slice(0,-1).join(slash):''),entries:listing.entries.filter(e=>e.type==='directory').map(e=>({...e,address:current+slash+e.name}))};
|
||||
}};
|
||||
const storage=new StorageService({store:storageStore,jobs:store,outputDir:dir,client,checkTimeoutMs:10000}),server=createDashboard({store,storage,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));let browser;t.after(async()=>{await browser?.close();await storage.stop();server.closeAllConnections();await new Promise(r=>server.close(r));storageStore.close();await fs.rm(dir,{recursive:true,force:true});});
|
||||
browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage({viewport:{width:1280,height:950}});page.setDefaultTimeout(15000);const errors=[];page.on('pageerror',e=>errors.push(e.message));await page.goto('http://127.0.0.1:'+server.address().port+'/#storage');await page.getByLabel('账号',{exact:true}).fill('admin');await page.getByLabel('密码',{exact:true}).fill('Test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();await page.getByRole('button',{name:'添加服务器',exact:true}).click();
|
||||
const input=page.getByLabel('共享路径',{exact:true}),save=page.getByRole('button',{name:'保存服务器',exact:true});await input.fill('files.test');await page.getByRole('button',{name:'浏览',exact:true}).click();await page.getByRole('button',{name:'orders',exact:true}).waitFor();assert.equal(await save.isDisabled(),true);assert.equal(storageStore.servers().length,0);
|
||||
await page.getByRole('button',{name:'orders',exact:true}).click();await page.getByRole('button',{name:'子目录',exact:true}).click();await page.getByRole('button',{name:'选择此目录',exact:true}).click();assert.equal(await input.inputValue(),slash.repeat(2)+'files.test'+slash+'orders'+slash+'子目录');assert.equal(await save.isDisabled(),false);
|
||||
await input.fill('slow.test');await page.getByRole('button',{name:'检查连接',exact:true}).click();await page.getByRole('button',{name:'取消操作',exact:true}).click();await page.getByText('已取消连接操作,可修改后重试。',{exact:true}).waitFor();assert.equal(await page.getByRole('button',{name:'检查连接',exact:true}).isEnabled(),true);assert.equal(await save.isDisabled(),true);
|
||||
storage.checkTimeoutMs=80;await page.getByRole('button',{name:'检查连接',exact:true}).click();await page.getByText(/连接超时,请检查服务器地址/).waitFor();assert.equal(await page.getByRole('button',{name:'检查连接',exact:true}).isEnabled(),true);assert.equal(storageStore.servers().length,0);assert.equal(aborted>=1,true);
|
||||
storage.checkTimeoutMs=10000;await input.fill(slash.repeat(2)+'files.test'+slash+'orders'+slash+'子目录');await page.getByRole('button',{name:'检查连接',exact:true}).click();await page.getByText('连接成功,目标目录可读取。',{exact:true}).waitFor();await save.click();await page.getByRole('button',{name:'管理服务器',exact:true}).waitFor();assert.equal(storageStore.servers()[0].directory,'子目录');assert.deepEqual(errors,[]);
|
||||
await fs.mkdir(path.resolve('output/verification'),{recursive:true});await page.screenshot({path:path.resolve('output/verification/storage-picker-tested.png'),fullPage:true});
|
||||
});
|
||||
Reference in new issue
Block a user