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

+18 -1
View File
@@ -25,7 +25,7 @@ test('storage HTTP enforces roles, redacts passwords and archives only successfu
});
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=storage.saveServer(config);await fs.writeFile(path.join(outputDir,'new.pdf'),'%PDF-1.7\nnew');
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);
@@ -33,3 +33,20 @@ test('automatic storage is opt-in, starts with new jobs and never queues mail',a
const job=store.recordJob({jobId:'new-job',status:'success',path:path.join(outputDir,'new.pdf'),filename:'new.pdf',createdAt:new Date(Date.now()+1).toISOString()});await storage.automatic(job);
assert.deepEqual(await fs.readdir(remote),['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);
});