fix: close merge-review mail policy and repair validation gaps
This commit is contained in:
1 parent
dcea4a063f
commit
d744c36365
16 files changed
+168
-31
No files matched your search
@@ -4,11 +4,11 @@ const config={name:'邮箱',email:'orders@company.test',smtp:{host:'smtp.company
|
||||
const raw=Buffer.from('From: author@company.test\r\nTo: orders@company.test\r\nSubject: [模板]Notice\r\nMessage-ID: <a@company.test>\r\n\r\nHello {{订单号}}');
|
||||
async function fixture(t,options={}){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'mail-service-'));const store=new MailStore(dir);const service=new MailService({store,...options});t.after(async()=>{await service.stop();store.close();fs.rmSync(dir,{recursive:true,force:true});});return {store,service};}
|
||||
|
||||
test('self-test renders business template variables with synthetic data and the configured self recipient',async t=>{
|
||||
let sent;const {store,service}=await fixture(t,{smtpFactory:()=>({verify:async()=>{},sendMail:async message=>{sent=message;return {accepted:[config.email]};},close(){}})});
|
||||
test('self-test renders business template variables with synthetic data and the fixed isolated QQ recipient',async t=>{
|
||||
let sent;const {store,service}=await fixture(t,{smtpFactory:()=>({verify:async()=>{},sendMail:async message=>{sent=message;return {accepted:['2639266652@qq.com']};},close(){}})});
|
||||
const c=store.saveChannel({...config,signature:{enabled:true,format:'html',body:'<p>Best Regards! 隔离账户签名</p>'}}),p=require('../../src/modules/mail/parser.cjs'),body='{{类别名称}}\n{{订单明细}}\n存放地址:{{存放地址}}',mail=Buffer.from(raw.toString().replace('Hello {{订单号}}',body));
|
||||
const draft=store.ingest(c.id,'1',1,await p.parseTemplate(mail,config),mail,c.version);store.confirm(draft.id,{version:1,subject:'测试摘要',scope:'',text:body,useText:true},'admin');
|
||||
await service.sendTest(c.id,draft.id);assert.equal(sent.to.address,config.email);assert.match(sent.text,/订单号:TEST/);assert.match(sent.text,/示例归档目录/);assert.match(sent.text,/隔离账户签名/);assert.match(sent.html,/<p>Best Regards!/);assert.doesNotMatch(sent.text,/{{/);assert.equal(store.sends()[0].status,'sent');
|
||||
await service.sendTest(c.id,draft.id);assert.deepEqual(sent.to,['2639266652@qq.com']);assert.deepEqual(sent.cc,[]);assert.deepEqual(sent.attachments,[]);assert.match(sent.text,/订单号:TEST/);assert.match(sent.text,/示例归档目录/);assert.match(sent.text,/隔离账户签名/);assert.match(sent.html,/<p>Best Regards!/);assert.doesNotMatch(sent.text,/{{/);assert.equal(store.sends()[0].status,'sent');
|
||||
});
|
||||
test('read-only poll uses UID epoch, ignores ordinary messages, does not double ingest, and records source denials',async t=>{
|
||||
let downloads=0,locks=0;const client={mailbox:{uidValidity:10n},on(){},async connect(){},async getMailboxLock(box,options){assert.equal(box,'INBOX');assert.equal(options.readOnly,true);locks++;return {release(){locks--;}};},async search(query,options){assert.equal(options.uid,true);return [1,2,3];},async fetchOne(uid,query,options){assert.equal(options.uid,true);if(query.source){downloads++;return {source:uid===3?Buffer.from(raw.toString().replace('author@','unknown@')):raw};}return {size:raw.length,envelope:{subject:uid===1?'Ordinary':'[模板]Notice'}};},async logout(){},close(){}};
|
||||
@@ -22,5 +22,5 @@ test('initial empty seven-day scan retains date boundary on future polls',async
|
||||
test('SMTP verification never sends; explicit self-test uses confirmed template and durable send record',async t=>{
|
||||
let messages=[];const smtp=new SMTPServer({secure:false,hideSTARTTLS:true,authOptional:true,disabledCommands:['AUTH'],onData(stream,session,cb){const chunks=[];stream.on('data',c=>chunks.push(c));stream.on('end',()=>{messages.push(Buffer.concat(chunks));cb();});}});await new Promise(r=>smtp.listen(0,'127.0.0.1',r));t.after(()=>new Promise(r=>smtp.close(r)));
|
||||
const nodemailer=require('nodemailer');const {store,service}=await fixture(t,{smtpFactory:()=>nodemailer.createTransport({host:'127.0.0.1',port:smtp.server.address().port,secure:false,ignoreTLS:true}),imapFactory:()=>({on(){},async connect(){},async getMailboxLock(){return {release(){}};},async logout(){},close(){}})});
|
||||
const c=store.saveChannel(config);await service.verify(c.id);assert.equal(messages.length,0);const p=require('../../src/modules/mail/parser.cjs');const draft=store.ingest(c.id,'1',1,await p.parseTemplate(raw,config),raw,c.version);assert.equal(store.active(c.id,''),null);await assert.rejects(service.sendTest(c.id,draft.id),e=>e.status===409);store.confirm(draft.id,{version:1,subject:'Notice {{订单号}}',scope:'',text:'Hello {{订单号}}',useText:false},'admin');await service.sendTest(c.id,draft.id);assert.equal(messages.length,1);const sent=await simpleParser(messages[0]);assert.equal(sent.to.value[0].address,config.email);assert.match(sent.text,/Hello TEST/);assert.equal(store.sends()[0].status,'sent');
|
||||
const c=store.saveChannel(config);await service.verify(c.id);assert.equal(messages.length,0);const p=require('../../src/modules/mail/parser.cjs');const draft=store.ingest(c.id,'1',1,await p.parseTemplate(raw,config),raw,c.version);assert.equal(store.active(c.id,''),null);await assert.rejects(service.sendTest(c.id,draft.id),e=>e.status===409);store.confirm(draft.id,{version:1,subject:'Notice {{订单号}}',scope:'',text:'Hello {{订单号}}',useText:false},'admin');await service.sendTest(c.id,draft.id);assert.equal(messages.length,1);const sent=await simpleParser(messages[0]);assert.equal(sent.to.value[0].address,'2639266652@qq.com');assert.match(sent.text,/Hello TEST/);assert.equal(store.sends()[0].status,'sent');
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
const test=require('node:test'),assert=require('node:assert/strict');
|
||||
const {MailService}=require('../../src/modules/mail/service.cjs');
|
||||
function fixture({verify=()=>{},beforeSend=()=>{},unknown=false}={}){
|
||||
const rows=[],messages=[],template={id:'template',version:1,status:'active',channelId:'channel',subject:'Template',text:'Hello {{订单号}}',html:'',attachments:[{filename:'private.pdf',content:'cGRm',contentType:'application/pdf'}]};
|
||||
const store={channelSecret:()=>({id:'channel',version:1,email:'sender@company.test',name:'Sender',smtp:{}}),guard(){},template:()=>({...template}),sends:()=>rows,findSampleSend:(id,key)=>rows.find(r=>r.channel_id===id&&r.template_id===key),beginSend:(id,key)=>{rows.push({id:'send-'+rows.length,channel_id:id,template_id:key,status:'sending'});return rows.at(-1).id;},finishSend:(id,status)=>{rows.find(r=>r.id===id).status=status;}};
|
||||
const service=new MailService({store,beforeSend,smtpFactory:()=>({verify:async()=>verify(template),sendMail:async message=>{messages.push(message);if(unknown)throw Error('connection lost');return {accepted:['2639266652@qq.com'],rejected:[]};},close(){}})});
|
||||
return {service,rows,messages,template};
|
||||
}
|
||||
test('legacy template test enforces fixed QQ envelope without source attachments',async()=>{
|
||||
const f=fixture(),result=await f.service.sendTest('channel','template');assert.equal(result.to,'2639266652@qq.com');assert.deepEqual(f.messages[0].to,['2639266652@qq.com']);assert.deepEqual(f.messages[0].cc,[]);assert.deepEqual(f.messages[0].attachments,[]);
|
||||
});
|
||||
for(const action of ['sendTest','sendSample'])for(const phase of ['before','after'])test(action+' enforces active period '+phase+' SMTP verification',async()=>{
|
||||
let verified=false;const f=fixture({verify:()=>{verified=true;},beforeSend:()=>{if(phase==='before'||verified)throw Object.assign(Error('expired'),{status:409});}});
|
||||
await assert.rejects(()=>action==='sendTest'?f.service.sendTest('channel','template'):f.service.sendSample('channel',{idempotencyKey:'one',subject:'Sample',text:'Hello'}),e=>e.status===409);assert.equal(f.messages.length,0);assert.equal(f.rows.length,0);assert.equal(verified,phase==='after');
|
||||
});
|
||||
test('template changed during SMTP verification is not submitted',async()=>{
|
||||
const f=fixture({verify:t=>{t.version++;}});await assert.rejects(()=>f.service.sendTest('channel','template'),e=>e.status===409);assert.equal(f.messages.length,0);
|
||||
});
|
||||
test('uncertain template test cannot be repeated',async()=>{
|
||||
const f=fixture({unknown:true});await assert.rejects(()=>f.service.sendTest('channel','template'));assert.equal(f.rows[0].status,'unknown');await assert.rejects(()=>f.service.sendTest('channel','template'),e=>e.status===409);assert.equal(f.messages.length,1);
|
||||
});
|
||||
Reference in new issue
Block a user