Files
bpq_gui/delete-message.test.mjs

37 lines
4.2 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import {deleteNativeMessage} from './delete-message.mjs';
const list=(status='N')=>'Message List\r\n<a href=/WebMail/WM?fresh&7> 7</a> 17-Oct B'+status+' 100 '+['ALL','WW','N1TEST'].map(s=>s.padEnd(7)).join(' ')+' Test bulletin\r\n';
test('delete uses a fresh private WebMail session and confirms the message is gone',async()=>{
const calls=[];let killed=false;
const bpq=async url=>{calls.push(url);if(url==='/Webmail')return '<a href=/WebMail/WMB?fresh>Bulls</a>';if(url==='/WebMail/WMALL?fresh')return killed?'Message List':list();if(url==='/WebMail/WMDel/7?fresh'){killed=true;return 'Message #7 Killed';}throw Error('Unexpected or stale WebMail key');};
assert.deepEqual(await deleteNativeMessage(7,bpq),{ok:true});assert.deepEqual(calls,['/Webmail','/WebMail/WMALL?fresh','/WebMail/WMDel/7?fresh','/WebMail/WMALL?fresh']);
});
test('a BPQ permission denial is reported instead of pretending the message was deleted',async()=>{
await assert.rejects(deleteNativeMessage(7,async url=>url==='/Webmail'?'<a href=/WebMail/WMB?fresh>Mail</a>':url.includes('WMDel')?'Not your message':list()),/permessi sysop/);
});
test('a native HTTP 200 or session-redirect page is insufficient if the message remains visible',async()=>{
await assert.rejects(deleteNativeMessage(7,async url=>url==='/Webmail'?'<a href=/WebMail/WMB?fresh>Mail</a>':list()),/non ha confermato/);
});
test('already absent or killed messages are idempotent and never issue another delete',async()=>{
for(const html of ['Message List',list('K')]){const calls=[];const result=await deleteNativeMessage(7,async url=>{calls.push(url);return url==='/Webmail'?'<a href=/WebMail/WMB?fresh>Mail</a>':html;});assert.equal(result.alreadyDeleted,true);assert.ok(!calls.some(url=>url.includes('WMDel')));}
});
test('BPQ may retain the killed row for sysop inspection; status K confirms deletion',async()=>{
let killed=false;assert.deepEqual(await deleteNativeMessage(7,async url=>{if(url==='/Webmail')return '<a href=/WebMail/WMB?fresh>Mail</a>';if(url.includes('WMDel')){killed=true;return 'Killed';}return list(killed?'K':'N');}),{ok:true});
});
test('unverifiable sessions and invalid message IDs cannot issue a deletion',async()=>{
await assert.rejects(deleteNativeMessage(0,async()=>{throw Error('Must not contact BPQ');}),/non valido/);
await assert.rejects(deleteNativeMessage(7,async()=>'<h1>Sign in</h1>'),/WebMail/);
await assert.rejects(deleteNativeMessage(7,async url=>url==='/Webmail'?'<a href=/WebMail/WMB?fresh>Mail</a>':'Sign in'),/Lista messaggi/);
});
test('sysop Console can delete another author bulletin through the native admin action, preserving metadata',async()=>{
let killed=false,save=null;const fields={From:'N1TEST',To:'ALL',BID:'BID_123',EFROM:'',VIA:'WW',Title:'Test & bulletin'};
const bpq=async(url,options)=>{if(url==='/Webmail')return '<a href=/WebMail/WMB?fresh>Mail</a>';if(url.includes('WMALL'))return killed?'Message List':list();if(url.includes('WMDel'))return 'Not your message';if(url==='/Mail/Status')return '<a href=/Mail/Msgs?private>Messages</a>';if(url.startsWith('/Mail/MsgDetails'))return '<h2>Message 7</h2>'+Object.entries(fields).map(([k,v])=>'<input name="'+k+'" value="'+v.replace('&','&amp;')+'">').join('')+'<select name="Type"><option value="P">Personal</option><option value="B" selected>Bulletin</option></select>';if(url.startsWith('/Mail/MsgSave')){save=options.body;killed=true;return 'Saved';}throw Error(url);};
assert.deepEqual(await deleteNativeMessage(7,bpq,{sysop:true}),{ok:true});assert.equal(save,'N1TEST|ALL|BID_123||WW|Test & bulletin|B|K|');
});
test('admin fallback refuses to modify the wrong message or incomplete metadata',async()=>{
let saves=0;const bpq=async url=>{if(url==='/Webmail')return '<a href=/WebMail/WMB?fresh>Mail</a>';if(url.includes('WMALL'))return list();if(url.includes('WMDel'))return 'Not your message';if(url==='/Mail/Status')return '<a href=/Mail/Msgs?admin>Messages</a>';if(url.includes('MsgDetails'))return '<h2>Message 8</h2><input name="From" value="N1TEST">';if(url.includes('MsgSave'))saves++;return '';};
await assert.rejects(deleteNativeMessage(7,bpq,{sysop:true}),/non ha confermato il messaggio/);assert.equal(saves,0);
});