9 lines
3.4 KiB
JavaScript
9 lines
3.4 KiB
JavaScript
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs');const os=require('node:os');const path=require('node:path');const {MailStore}=require('../../src/modules/mail/store.cjs');const {MailService}=require('../../src/modules/mail/service.cjs');
|
|
function setup(t){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'unified-mail-')),store=new MailStore(dir);t.after(()=>{store.close();fs.rmSync(dir,{recursive:true,force:true});});return store;}
|
|
const legacy={name:'Legacy',email:'old@company.test',enabled:false,senders:['author@company.test'],smtp:{host:'smtp.company.test',port:465,security:'tls',user:'smtp-login',password:'smtp-original'},imap:{host:'imap.company.test',port:993,security:'tls',user:'imap-login',password:'imap-original'}};
|
|
test('shared account uses provider presets, optional IMAP and never returns secrets',t=>{const store=setup(t);const c=store.saveChannel({name:'发送',email:'notice@163.com',password:'shared-secret',receiveEnabled:false});assert.equal(c.smtp.host,'smtp.163.com');assert.equal(c.smtp.user,'notice@163.com');assert.equal(c.smtp.port,465);assert.equal(c.receiveEnabled,false);assert.deepEqual(c.senders,[]);assert.equal(c.enabled,false);assert.doesNotMatch(JSON.stringify(c),/shared-secret/);assert.equal(store.channelSecret(c.id).smtp.password,'shared-secret');});
|
|
test('blank shared password preserves distinct legacy credentials and identity',t=>{const store=setup(t),c=store.saveChannel(legacy);const updated=store.saveChannel({...c,password:''});const secret=store.channelSecret(c.id);assert.equal(updated.id,c.id);assert.equal(updated.receiveEnabled,true);assert.equal(secret.smtp.password,'smtp-original');assert.equal(secret.imap.password,'imap-original');assert.equal(secret.smtp.user,'smtp-login');assert.equal(secret.imap.user,'imap-login');});
|
|
test('enabled receiving uses shared credentials for both protocols without enabling polling',t=>{const store=setup(t),c=store.saveChannel({name:'QQ',email:'123@qq.com',password:'shared-secret',receiveEnabled:true,enabled:false});const s=store.channelSecret(c.id);assert.equal(s.imap.host,'imap.qq.com');assert.equal(s.imap.password,'shared-secret');assert.equal(s.smtp.password,'shared-secret');assert.equal(c.enabled,false);assert.throws(()=>store.saveChannel({...c,password:'',enabled:true}),/发件人/);});
|
|
test('SMTP-only check never connects IMAP or sends, and receiving operations are rejected',async t=>{const store=setup(t),old=store.saveChannel(legacy),c=store.saveChannel({...old,receiveEnabled:false,smtp:{...old.smtp,password:''},imap:{...old.imap,password:''}});let checks=0,imapConnections=0;const service=new MailService({store,smtpFactory:()=>({async verify(){checks++;},sendMail(){assert.fail('check must never send');},close(){}}),imapFactory:()=>{imapConnections++;assert.fail('disabled IMAP must never connect');}});t.after(()=>service.stop());const result=await service.verify(c.id);assert.equal(result.state.smtp,true);assert.equal(result.state.imap,false);assert.equal(checks,1);assert.equal(imapConnections,0);await assert.rejects(service.history(c.id,{}),e=>e.status===409);await assert.rejects(service.poll(c.id),e=>e.status===409);});
|
|
test('supplied polling switch must be boolean while omission retains SMTP-only defaults',t=>{const store=setup(t);for(const enabled of ['true',null,1])assert.throws(()=>store.saveChannel({name:'invalid',email:'invalid@163.com',password:'secret',receiveEnabled:false,enabled}),/收信|开关/);});
|