feat: add category order rules and read-only sent mail history

This commit is contained in:
abaishu0730 committed 2026-10-19 10:36:32 +08:00
1 parent 388805a8a6
commit efa982a8a1
41 files changed
+1828 -28

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
'use strict';
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
const {startPreview}=require('../../scripts/preview.cjs');const {loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');
test('history UI saves IMAP-only credentials locally, explicitly queries sent mail and displays escaped evidence',async t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'history-ui-'));const runtime=await startPreview({stateDir:dir,port:0,mailHistory:true});let browser;
t.after(async()=>{await browser?.close();await runtime.close();fs.rmSync(dir,{recursive:true,force:true});});
let connects=0;
const raw=Buffer.from('From: sender@example.test\r\nTo: orders@example.test\r\nSubject: Test notice\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n<img src="https://tracker.example.test/pixel"> Huizhou evidence');
runtime.mail.imapFactory=()=>({mailbox:{uidValidity:1n},on(){},async connect(){connects++;},async list(){return [{path:'Sent',specialUse:'\\Sent'}];},async getMailboxLock(_path,opts){assert.equal(opts.readOnly,true);return {release(){}};},async search(){return [1];},async fetchOne(uid,q){return q.source?{source:raw}:{uid,size:raw.length,envelope:{subject:'Test notice'}};},async logout(){},close(){}});
browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage();const requests=[];page.on('request',r=>requests.push(r.url()));
await page.goto('http://127.0.0.1:'+runtime.server.address().port+'/#mail');await page.getByRole('button',{name:'进入演示空间',exact:true}).click();
await page.getByRole('button',{name:'添加只读邮箱',exact:true}).click({timeout:5000});
for(const [label,value]of [['邮箱名称','历史核对'],['邮箱地址','orders@example.test'],['IMAP 服务器','imap.example.test'],['IMAP 登录账号','orders@example.test'],['IMAP 授权码','History-123']])await page.getByLabel(label,{exact:true}).fill(value);
await page.getByRole('button',{name:'保存只读邮箱',exact:true}).click();await page.getByText('只读邮箱已保存。',{exact:true}).waitFor();assert.equal(connects,0);
await page.getByRole('button',{name:'连接并列出已发送文件夹',exact:true}).click();await page.getByLabel('已发送文件夹',{exact:true}).selectOption('Sent');
await page.getByRole('button',{name:'查询已发送邮件',exact:true}).click();await page.getByRole('button',{name:'查看邮件',exact:true}).click();
await page.getByText('<img src="https://tracker.example.test/pixel"> Huizhou evidence',{exact:true}).waitFor();
assert.equal(requests.some(r=>r.startsWith('https://tracker.example.test')),false);assert.equal(await page.getByRole('button',{name:/发送测试/}).count(),0);
assert.equal(runtime.mail.store.channels()[0].smtp.hasPassword,false);
await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);
});
+41
View File
@@ -0,0 +1,41 @@
'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');
const {MailService}=require('../../src/modules/mail/service.cjs');
const fs=require('node:fs'),os=require('node:os'),path=require('node:path');
const {MailStore}=require('../../src/modules/mail/store.cjs');
const raw=Buffer.from('From: sender@example.test\r\nTo: orders@example.test\r\nCc: factory@example.test\r\nSubject: Order notice\r\nDate: Sun, 18 Oct 2026 10:00:00 +0800\r\nMessage-ID: <notice@example.test>\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nOrder body');
function fixture(){
const calls=[],store={channelSecret:()=>({version:1,imap:{}}),guard(){}};
const client={mailbox:{uidValidity:9n},on(){},async connect(){},async logout(){},close(){},async list(){return [{path:'Sent',specialUse:'\\Sent',flags:new Set()},{path:'INBOX',flags:new Set()}];},async getMailboxLock(folder,options){calls.push({folder,...options});return {release(){}};},async search(query,options){calls.push({query,...options});return [1,2,3];},async fetchOne(uid,query,options){calls.push({uid,query,...options});return query.source?{source:raw}:{uid,size:raw.length,envelope:{subject:'Order notice',date:new Date('2026-10-18T02:00:00Z'),to:[{address:'orders@example.test'}]}};}};
const service=new MailService({store,imapFactory:()=>client,smtpFactory:()=>{throw Error('SMTP must not be used');}});
return {service,calls,client};
}
test('an IMAP-only history account stores no SMTP credential and rejects all sending or template polling',async t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'history-account-')),store=new MailStore(dir);
const service=new MailService({store,smtpFactory:()=>{throw Error('SMTP should be unreachable');}});
t.after(async()=>{await service.stop();store.close();fs.rmSync(dir,{recursive:true,force:true});});
const account=store.saveChannel({readOnly:true,name:'历史格式核对',email:'orders@example.test',imap:{host:'imap.example.test',port:993,security:'tls',user:'orders@example.test',password:'History-only-123'}});
assert.equal(account.readOnly,true);assert.equal(account.enabled,false);assert.equal(account.smtp.hasPassword,false);
assert.doesNotMatch(JSON.stringify(store.channels()),/History-only-123/);
for(const work of [()=>service.verify(account.id),()=>service.poll(account.id),()=>service.sendTest(account.id,'any')])await assert.rejects(work(),e=>e.status===409&&e.message.includes('只读'));
});
test('sent history is bounded, read-only, does not download bodies during search, and keeps mailbox cursor untouched',async()=>{
const {service,calls}=fixture();
const result=await service.history('channel',{action:'search',folder:'Sent',since:'2026-10-01',before:'2026-10-20',subject:'订单',limit:2});
assert.equal(result.messages.length,2);assert.equal(result.truncated,true);assert.deepEqual(result.messages.map(m=>m.uid),[3,2]);
assert.ok(calls.some(c=>c.folder==='Sent'&&c.readOnly===true));
assert.ok(calls.some(c=>c.query?.sentSince instanceof Date&&c.query.subject==='订单'));
assert.ok(!calls.some(c=>c.query?.source));
const detail=await service.history('channel',{action:'message',folder:'Sent',uid:3,uidValidity:result.uidValidity});
assert.equal(detail.text,'Order body');assert.equal(detail.to[0].address,'orders@example.test');assert.equal(detail.cc[0].address,'factory@example.test');
assert.deepEqual(detail.attachments,[]);
});
test('history rejects wrong folder, stale UID epoch, oversized messages and unbounded dates without leaking provider errors',async()=>{
const {service,client}=fixture();
for(const input of [{action:'search',folder:'INBOX',since:'2026-10-01',before:'2026-10-20'},{action:'search',folder:'Sent',since:'2020-01-01',before:'2026-10-20'},{action:'search',folder:'Sent',since:'2026-02-31',before:'2026-03-05'}])await assert.rejects(service.history('c',input),e=>e.status===400);
await assert.rejects(service.history('c',{action:'message',folder:'Sent',uid:3,uidValidity:'8'}),e=>e.status===409);
client.fetchOne=async()=>({size:20*1024*1024});
await assert.rejects(service.history('c',{action:'message',folder:'Sent',uid:3,uidValidity:'9'}),e=>e.status===413);
client.connect=async()=>{throw Error('secret password');};
await assert.rejects(service.history('c',{action:'folders'}),e=>!e.message.includes('secret password'));
});
+15
View File
@@ -13,3 +13,18 @@ test('template preview is isolated, source attachments downloadable and confirma
assert.equal((await call('/api/mail/templates/'+d.id+'/confirm',{version:1,subject:d.subject,scope:'',useText:false},reader)).status,403);assert.equal((await call('/api/mail/templates/'+d.id+'/confirm',{version:1,subject:d.subject,scope:'',useText:false},admin)).status,200);assert.equal(mailStore.active(c.id,'').id,d.id);
assert.equal((await call('/api/mail/templates/'+d.id+'/source',undefined,reader)).status,403);const source=await call('/api/mail/templates/'+d.id+'/source',undefined,admin);assert.equal(await source.text(),raw.toString());
});
test('history access requires administrator and same origin; isolated history mode rejects SMTP and polling routes',async t=>{
const {mail,mailStore,call,login}=await setup(t);mail.historyOnly=true;
const admin=await login('admin'),reader=await login('reader');
const saved=await call('/api/mail/channels',{readOnly:true,name:'历史核对',email:'orders@example.test',imap:config.imap},admin);
assert.equal(saved.status,201);const account=await saved.json();
const url='/api/mail/channels/'+account.id;
assert.equal((await call(url+'/history',{action:'folders'},reader)).status,403);
assert.equal((await call(url+'/history',{action:'folders'},admin,'https://evil.test')).status,403);
for(const action of ['verify','poll','test'])assert.equal((await call(url+'/'+action,{},admin)).status,409);
assert.equal((await call('/api/mail/channels',config,admin)).status,409);
assert.equal(mailStore.channels().length,1);
mail.imapFactory=()=>({on(){},async connect(){},async list(){return [{path:'Sent',specialUse:'\\Sent'}];},async logout(){},close(){}});
const folders=await call(url+'/history',{action:'folders'},admin);assert.equal(folders.status,200);assert.deepEqual((await folders.json()).folders,[{path:'Sent',name:'Sent'}]);
});