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

83 lines
6.9 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 {StorageStore}=require('../src/storage-store.cjs');
const {FileArea}=require('../src/storage-files.cjs');
const config={name:'210 归档',host:'storage.company.test',share:'orders',directory:'通知书',username:'DOMAIN\\archive',password:'Storage-secret-123',enabled:true};
async function fixture(t){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-storage-'));t.after(()=>fs.rm(dir,{recursive:true,force:true}));return dir;}
test('storage persists encrypted credentials, keeps blank passwords and rejects stale or changed identities',async t=>{
let store;t.after(()=>store?.close());const dir=await fixture(t);store=new StorageStore(dir);
const saved=store.saveServer(config);assert.equal(saved.hasPassword,true);assert.equal(saved.password,undefined);
assert.doesNotMatch(JSON.stringify(store.servers()),/Storage-secret/);
assert.doesNotMatch((await fs.readFile(path.join(dir,'storage.sqlite'))).toString('utf8'),/Storage-secret-123/);
assert.equal(store.secret(saved.id).password,'Storage-secret-123');
assert.equal(store.saveServer({...saved,password:'',name:'归档服务器'}).version,2);
assert.throws(()=>store.saveServer({...saved,password:''}),e=>e.status===409);
const current=store.publicServer(saved.id);
assert.throws(()=>store.saveServer({...current,host:'other.company.test',password:''}),e=>e.status===400);
assert.throws(()=>store.saveServer({...current,directory:'../outside',password:''}),e=>e.status===400);
assert.throws(()=>store.saveServer({...current,share:'orders\\other',password:''}),e=>e.status===400);
store.close();store=new StorageStore(dir);assert.equal(store.secret(saved.id).password,'Storage-secret-123');
const record=store.beginArchive(saved.id,'job-1','admin','');assert.equal(record.status,'running');
store.close();store=new StorageStore(dir);assert.equal(store.archives()[0].status,'interrupted');
for(let index=0;index<101;index++)store.beginArchive(saved.id,'later-'+index,'admin','');
assert.equal(store.finishArchive(record.id,'success',{filename:'old.pdf'}).status,'success');
});
test('file area publishes real PDFs idempotently and preserves different same-name content',async t=>{
const dir=await fixture(t),root=path.join(dir,'remote');await fs.mkdir(root);const area=new FileArea(root);
await area.check();assert.deepEqual(await fs.readdir(root),[]);
await area.mkdir('产业A');const file=path.join(dir,'source.pdf');await fs.writeFile(file,'%PDF-1.7\nfirst document\n%%EOF');
const first=await area.archive(file,'订单.pdf','产业A');assert.equal(first.filename,'订单.pdf');assert.equal(first.bytes,29);
assert.equal((await area.readPdf('产业A/订单.pdf')).bytes.toString(),'%PDF-1.7\nfirst document\n%%EOF');
const again=await area.archive(file,'订单.pdf','产业A');assert.equal(again.relativePath,first.relativePath);
await fs.writeFile(file,'%PDF-1.7\nsecond document\n%%EOF');
const second=await area.archive(file,'订单.pdf','产业A');assert.notEqual(second.filename,first.filename);
assert.equal((await fs.readdir(path.join(root,'产业A'))).length,2);
assert.equal((await area.readPdf('产业A/订单.pdf')).bytes.toString(),'%PDF-1.7\nfirst document\n%%EOF');
assert.deepEqual((await area.list('')).entries.map(e=>e.name),['产业A']);
});
test('file area rejects parent paths, alternate streams, symlinks and non-PDF sources',async t=>{
const dir=await fixture(t),root=path.join(dir,'remote'),outside=path.join(dir,'outside');await fs.mkdir(root);await fs.mkdir(outside);const area=new FileArea(root);
for(const relative of ['../outside','C:\\secret','\\\\other\\share','a:stream','CON','folder./secret','a/../b'])await assert.rejects(area.list(relative),e=>e.status===400);
await fs.symlink(outside,path.join(root,'escape'),'junction');await assert.rejects(area.mkdir('escape/nested'),e=>e.status===400);
const file=path.join(dir,'not.pdf');await fs.writeFile(file,'not a PDF');await assert.rejects(area.archive(file,'file.pdf',''),e=>e.status===400);
await assert.rejects(area.readPdf('not.txt'),e=>e.status===400);
});
test('configured archive root cannot escape through an intermediate directory junction',async t=>{
const dir=await fixture(t),share=path.join(dir,'share'),outside=path.join(dir,'outside');await fs.mkdir(share);await fs.mkdir(path.join(outside,'nested'),{recursive:true});await fs.symlink(outside,path.join(share,'redirect'),'junction');
const area=new FileArea(path.join(share,'redirect','nested'),{boundary:share});
await assert.rejects(area.list(''),e=>e.status===400);
assert.deepEqual(await fs.readdir(path.join(outside,'nested')),[]);
});
test('storage accepts a complete UNC path and Windows identity without credentials',async t=>{
let store;t.after(()=>store?.close());const dir=await fixture(t);store=new StorageStore(dir);
const saved=store.saveServer({address:String.raw`\\files.example.test\orders\归档`,authMode:'windows',enabled:true});
assert.equal(saved.host,'files.example.test');assert.equal(saved.share,'orders');assert.equal(saved.directory,'归档');assert.equal(saved.name,'files.example.test / orders');assert.equal(saved.authMode,'windows');assert.equal(saved.hasPassword,false);assert.equal(store.secret(saved.id).password,'');
for(const address of [String.raw`\\server`,String.raw`\\server\orders\..\secret`,'C:/local','https://server/orders'])assert.throws(()=>store.saveServer({address,authMode:'windows',enabled:true}),e=>e.status===400);
const credentials=store.saveServer(config);const switched=store.saveServer({...credentials,authMode:'windows'});assert.equal(store.secret(switched.id).password,'');assert.equal(switched.username,'');
});
test('explicit passwordless account does not accidentally retain a saved password',async t=>{
let store;t.after(()=>store?.close());const dir=await fixture(t);store=new StorageStore(dir);const saved=store.saveServer(config);
const passwordless=store.saveServer({...saved,authMode:'credentials',passwordless:true,password:''});assert.equal(store.secret(saved.id).password,'');assert.equal(passwordless.hasPassword,false);
assert.equal(store.saveServer({...passwordless,password:''}).passwordless,true);
assert.throws(()=>store.saveServer({...store.publicServer(saved.id),passwordless:false,password:''}),e=>e.status===400);
});
test('Windows identity mode uses UNC access without requesting a new SMB login',async t=>{
const native=require('../src/smb-native.cjs');let calls=0;t.mock.method(native,'connect',async()=>{calls++;});
const {SmbClient}=require('../src/storage-files.cjs'),client=new SmbClient();
const area=await client.area({host:'files.example.test',share:'orders',directory:'archive',authMode:'windows'});assert.equal(calls,0);assert.equal(area.root,path.resolve(String.raw`\\files.example.test\orders\archive`));
await client.area({...config,authMode:'credentials'});assert.equal(calls,1);
await client.area(config);assert.equal(calls,2);
});