Files
wekan/tests/syncRuleEmailCommand.test.cjs
T

63 lines
3.7 KiB
JavaScript

'use strict';
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { prepareRulePlan } = require('../server/lib/syncRulePlan');
const { ensureRuleEmailCommand: ensure } = require('../server/lib/syncRuleEmailCommand');
async function fixture() {
const f = { activity: { _id: 'activity', boardId: 'board', cardId: 'card', userId: 'actor' },
effectId: 'a'.repeat(64), index: 0, assertCurrent: async () => {} };
f.plan = await prepareRulePlan({ ...f,
selectRules: async () => [{ _id: 'rule', boardId: 'board', triggerId: 'trigger', actionId: 'action' }],
readAction: async () => ({ _id: 'action', actionType: 'sendEmail' }) });
f.mail = { to: 'outside@example.org', from: 'wekan@example.org', subject: 'Captured subject', text: 'Captured body' };
f.prepare = async () => f.mail;
f.rows = new Map();
f.commands = { findOne: async ({ _id }) => structuredClone(f.rows.get(_id)),
insertOne: async row => { if (f.rows.has(row._id)) throw new Error('duplicate'); f.rows.set(row._id, structuredClone(row)); } };
return f;
}
test('first prepared recipient and content survive changed live settings and returned-object mutation', async () => {
const f = await fixture(), first = await ensure(f), expected = structuredClone(first);
f.mail.to = 'changed@example.org'; first.mail.text = 'changed';
assert.deepEqual(await ensure({ ...f, prepare: () => assert.fail('must not render again') }), expected);
assert.equal(f.rows.size, 1);
});
test('the full saved plan, activity and invocation position bind a command', async () => {
const f = await fixture(); await ensure(f);
for (const index of [-1, 1, 0.5]) await assert.rejects(ensure({ ...f, index }), /command-invalid/);
const changed = structuredClone(f.plan); changed.actions[0].action.emailTo = 'different';
await assert.rejects(ensure({ ...f, plan: changed }), /command-invalid/);
await assert.rejects(ensure({ ...f, activity: { ...f.activity, userId: 'other' } }), /plan-invalid/);
changed.actions[0].action.actionType = 'archive';
await assert.rejects(ensure({ ...f, plan: changed }), /command-invalid/);
});
test('malformed or oversized transport fields cannot be stored as commands', async () => {
for (const patch of [{ to: '' }, { from: 'sender\nBcc: secret' }, { subject: 'line\rbreak' },
{ replyTo: 42 }, { text: {} }, { html: 'x'.repeat(1024 * 1024) }, { attachments: [] }, { bcc: 'hidden@example.org' }]) {
const f = await fixture();
await assert.rejects(ensure({ ...f, prepare: async () => ({ ...f.mail, ...patch }) }), /command-invalid/);
assert.equal(f.rows.size, 0);
}
});
test('uncertain insertion is read back, false acknowledgement and corruption are refused', async () => {
const f = await fixture(), insert = f.commands.insertOne;
f.commands.insertOne = async () => ({ acknowledged: true });
await assert.rejects(ensure(f), /command-unconfirmed/);
f.commands.insertOne = async row => { await insert(row); throw new Error('lost reply'); };
const saved = await ensure(f);
f.rows.get(saved._id).mail.to = 'tampered@example.org';
await assert.rejects(ensure(f), /command-invalid/);
});
test('preparation loses ownership before storage and cannot mutate the captured identity', async () => {
const f = await fixture(); let current = true;
f.assertCurrent = async () => { if (!current) throw new Error('lease lost'); };
f.prepare = async ({ invocation, activity }) => {
invocation.id = 'changed'; activity.cardId = 'other'; current = false; return f.mail;
};
await assert.rejects(ensure(f), /lease lost/); assert.equal(f.rows.size, 0);
current = true; f.prepare = async ({ invocation, activity }) => {
invocation.id = 'changed'; activity.cardId = 'other'; return f.mail;
};
assert.equal((await ensure(f)).cardId, 'card');
});