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 {Store}=require('../../src/modules/dashboard/store.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');const {MailStore}=require('../../src/modules/mail/store.cjs');const {MailService}=require('../../src/modules/mail/service.cjs');const {parseTemplate}=require('../../src/modules/mail/parser.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}; async function setup(t){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'mail-http-'));const store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');store.addUser({username:'reader',password:'Test-password-123',role:'viewer'},'admin');const mailStore=new MailStore(path.join(dir,'mail'));const mail=new MailService({store:mailStore});const server=createDashboard({store,mail,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(async()=>{await mail.stop();await new Promise(r=>server.close(r));mailStore.close();fs.rmSync(dir,{recursive:true,force:true});});const base='http://127.0.0.1:'+server.address().port;const call=(route,body,cookie,origin=base)=>fetch(base+route,{method:body===undefined?'GET':'POST',headers:{...(body===undefined?{}:{'Content-Type':'application/json',Origin:origin}),...(cookie?{Cookie:cookie}:{})},body:body===undefined?undefined:JSON.stringify(body)});const login=async username=>(await call('/api/login',{username,password:'Test-password-123'})).headers.get('set-cookie').split(';')[0];return {mail,mailStore,call,login};} test('mail API uses authentication, roles, same origin, redacted config and actual overview status',async t=>{ const {mailStore,call,login}=await setup(t);assert.equal((await call('/api/mail')).status,401);const admin=await login('admin'),reader=await login('reader');assert.equal((await call('/api/mail/channels',config,reader)).status,403);assert.equal((await call('/api/mail/channels',config,admin,'https://evil.test')).status,403); const save=await call('/api/mail/channels',config,admin);assert.equal(save.status,201);const c=await save.json();assert.equal(c.smtp.password,undefined);assert.equal(c.smtp.hasPassword,true);const snapshot=await (await call('/api/mail',undefined,reader)).json();assert.equal(snapshot.channels.length,1);assert.doesNotMatch(JSON.stringify(snapshot),/Secret-mail/);assert.equal((await call('/api/mail/channels',config,admin)).status,409); mailStore.setState(c.id,1,{smtp:true,imap:true});const overview=await (await call('/api/overview',undefined,admin)).json();assert.equal(overview.connections.find(x=>x.name==='邮件渠道').ready,true); }); test('template preview is isolated, source attachments downloadable and confirmation requires admin',async t=>{ const {mailStore,call,login}=await setup(t);const c=mailStore.saveChannel(config);const raw=Buffer.from('From: author@company.test\r\nTo: orders@company.test\r\nSubject: [模板]Notice\r\nMIME-Version: 1.0\r\nContent-Type: text/html\r\n\r\n
| Hello |