20 lines
5.1 KiB
JavaScript
20 lines
5.1 KiB
JavaScript
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs');const path=require('node:path');const os=require('node:os');const {SMTPServer}=require('smtp-server');const {simpleParser}=require('mailparser');
|
|
const {MailStore}=require('../../src/modules/mail/store.cjs');const {MailService}=require('../../src/modules/mail/service.cjs');
|
|
const config={name:'邮箱',email:'orders@company.test',smtp:{host:'smtp.company.test',port:465,security:'tls',user:'orders',password:'Secret-mail-123'},imap:{host:'imap.company.test',port:993,security:'tls',user:'orders',password:'Secret-mail-123'},senders:['author@company.test'],enabled:true};
|
|
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('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(){}};
|
|
const {store,service}=await fixture(t,{imapFactory:()=>client});const c=store.saveChannel(config);await service.poll(c.id);await service.poll(c.id);assert.equal(downloads,2);assert.equal(locks,0);assert.equal(store.templates().length,1);assert.equal(store.cursor(c.id).uid,3);assert.equal(store.receipts()[0].status,'rejected');
|
|
});
|
|
test('failed download does not advance cursor and overlapping polls are blocked; provider error never exposes secret',async t=>{
|
|
let release;const wait=new Promise(r=>release=r);const client={mailbox:{uidValidity:1n},on(){},async connect(){await wait;},async getMailboxLock(){return {release(){}};},async search(){return [9];},async fetchOne(uid,q){if(q.source)throw new Error(config.imap.password);return {size:100,envelope:{subject:'[模板]Notice'}};},async logout(){},close(){}};
|
|
const {store,service}=await fixture(t,{imapFactory:()=>client});const c=store.saveChannel(config);const first=service.poll(c.id);await assert.rejects(service.poll(c.id),e=>e.status===409);release();await assert.rejects(first,e=>!e.message.includes(config.imap.password));assert.deepEqual(store.cursor(c.id),{});assert.doesNotMatch(JSON.stringify(store.channels()),/Secret-mail/);
|
|
});
|
|
test('initial empty seven-day scan retains date boundary on future polls',async t=>{const queries=[];const client={mailbox:{uidValidity:1n},on(){},async connect(){},async getMailboxLock(){return {release(){}};},async search(q){queries.push(q);return [];},async logout(){},close(){}};const {store,service}=await fixture(t,{imapFactory:()=>client});const c=store.saveChannel(config);await service.poll(c.id);await service.poll(c.id);assert.ok(queries[0].since instanceof Date);assert.ok(queries[1].since instanceof Date);assert.ok(Math.abs(queries[0].since-queries[1].since)<5000);});
|
|
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');
|
|
});
|