61 lines
9.5 KiB
JavaScript
61 lines
9.5 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 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),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);
|
|
const admin=await login('admin'),operator=await login('operator'),viewer=await login('viewer');
|
|
assert.equal((await call('/api/storage/servers',config,operator)).status,403);assert.equal((await call('/api/storage/servers',config,admin,'https://evil.test')).status,403);
|
|
const quick=await call('/api/storage/servers',{address:String.raw`\\storage.company.test\orders`,authMode:'windows',enabled:true},admin);assert.equal(quick.status,201);const quickServer=await quick.json();assert.equal(quickServer.authMode,'windows');assert.equal(quickServer.hasPassword,false);assert.equal(quickServer.username,'');assert.equal((await call(`/api/storage/servers/${quickServer.id}/check`,{},admin)).status,200);
|
|
const invalid=await call('/api/storage/servers',{address:String.raw`\\storage.company.test\orders\..\outside`,authMode:'windows',enabled:true},admin);assert.equal(invalid.status,400);
|
|
const saved=await call('/api/storage/servers',config,admin);assert.equal(saved.status,201);const server=await saved.json();assert.equal(server.password,undefined);
|
|
assert.doesNotMatch(JSON.stringify(await(await call('/api/storage',undefined,viewer)).json()),/Private-storage/);
|
|
assert.equal((await call(`/api/storage/servers/${server.id}/files`,undefined,viewer)).status,403);
|
|
assert.equal((await call(`/api/storage/servers/${server.id}/check`,{},admin)).status,200);
|
|
const overview=await(await call('/api/overview',undefined,admin)).json();assert.equal(overview.connections.find(c=>c.name==='存储服务器').ready,true);
|
|
await fs.writeFile(path.join(outputDir,'order.pdf'),'%PDF-1.7\nHTTP document');store.recordJob({jobId:'job-1',status:'success',path:path.join(outputDir,'order.pdf'),filename:'order.pdf',metadata:{order:'001234'}});
|
|
assert.equal((await call(`/api/storage/servers/${server.id}/archive`,{jobId:'job-1',directory:'产业A'},viewer)).status,403);
|
|
const archived=await call(`/api/storage/servers/${server.id}/archive`,{jobId:'job-1',directory:'产业A'},operator);assert.equal(archived.status,200);assert.equal((await archived.json()).status,'success');
|
|
assert.equal(storageStore.archives().length,1);
|
|
const downloaded=await call(`/api/storage/servers/${server.id}/file?path=${encodeURIComponent('产业A/001234/order.pdf')}`,undefined,operator);assert.equal(downloaded.status,200);assert.equal(await downloaded.text(),'%PDF-1.7\nHTTP document');
|
|
store.recordJob({jobId:'outside',status:'success',path:path.join(dir,'outside.pdf'),filename:'outside.pdf'});await fs.writeFile(path.join(dir,'outside.pdf'),'%PDF-1.7\noutside');
|
|
assert.equal((await call(`/api/storage/servers/${server.id}/archive`,{jobId:'outside',directory:''},admin)).status,400);
|
|
});
|
|
|
|
test('automatic storage is opt-in, starts with new jobs and never queues mail',async t=>{
|
|
const {outputDir,remote,store,storage,storageStore}=await fixture(t);const server=await storage.saveServer(config);await fs.writeFile(path.join(outputDir,'new.pdf'),'%PDF-1.7\nnew');
|
|
const old=store.recordJob({jobId:'old-job',status:'success',path:path.join(outputDir,'new.pdf'),filename:'new.pdf',createdAt:'2000-01-01T00:00:00Z'});
|
|
await storage.automatic(old);assert.deepEqual(await fs.readdir(remote),[]);
|
|
await assert.rejects(Promise.resolve().then(()=>storage.saveSettings({serverId:server.id,enabled:true})),e=>e.status===409);
|
|
await storage.check(server.id);storage.saveSettings({serverId:server.id,enabled:true});await storage.automatic(old);assert.deepEqual(await fs.readdir(remote),[]);
|
|
const job=store.recordJob({jobId:'new-job',status:'success',path:path.join(outputDir,'new.pdf'),filename:'new.pdf',metadata:{order:'7654321'},createdAt:new Date(Date.now()+1).toISOString()});await storage.automatic(job);
|
|
assert.deepEqual(await fs.readdir(remote),['7654321']);assert.deepEqual(await fs.readdir(path.join(remote,'7654321')),['new.pdf']);assert.equal(storageStore.archives()[0].status,'success');
|
|
});
|
|
|
|
|
|
test('draft connection check does not save, and unreadable storage cannot be added',async t=>{
|
|
const {remote,storageStore,call,login}=await fixture(t),admin=await login('admin'),operator=await login('operator');
|
|
const draft={address:String.fromCharCode(92).repeat(2)+'storage.company.test'+String.fromCharCode(92)+'orders',authMode:'windows',enabled:true};
|
|
assert.equal((await call('/api/storage/servers/check',draft,operator)).status,403);
|
|
assert.equal((await call('/api/storage/servers/check',draft,admin,'https://evil.test')).status,403);
|
|
const checked=await call('/api/storage/servers/check',draft,admin);assert.equal(checked.status,200);assert.equal((await checked.json()).readable,true);assert.equal(storageStore.servers().length,0);assert.deepEqual(await fs.readdir(remote),[]);
|
|
await fs.rmdir(remote);
|
|
assert.equal((await call('/api/storage/servers/check',draft,admin)).status,502);
|
|
assert.equal((await call('/api/storage/servers',draft,admin)).status,502);assert.equal(storageStore.servers().length,0);
|
|
await fs.mkdir(remote);const saved=await call('/api/storage/servers',draft,admin);assert.equal(saved.status,201);const server=await saved.json();assert.equal(server.state.readable,true);assert.equal(server.state.writable,false);
|
|
assert.deepEqual(await fs.readdir(remote),[]);
|
|
const unavailable={...server,address:String.fromCharCode(92).repeat(2)+'storage.company.test'+String.fromCharCode(92)+'orders'+String.fromCharCode(92)+'new',name:'changed',password:''};await fs.rmdir(remote);
|
|
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);
|
|
});
|