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
@@ -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);
|
||||
});
|
||||
Reference in new issue
Block a user