Files
wekan/tests/ruleCardActionSchema.test.cjs
T
Lauri Ojansivu 16c8f425d0 Let a rule's trigger/action be edited in place, and email the card's description too.
#2713 asked for two things.

Editing a rule used to mean delete it and rebuild it from scratch, losing
its position and its identity. rulesList's rule row gets a second "Edit
trigger/action" button next to the existing title-rename pencil, opening
the same trigger/action wizard used to create a rule (rulesMain.js /
rulesTriggers / rulesActions and the board/card/checklist/mail action
templates), pre-filled with the rule's current title. A new server method,
rules.updateRule (server/rulesButton.js), mirrors rules.createRule's
authorization and cross-board destination checks but REPLACES the existing
trigger/action documents in place (full-document update by their existing
_id, not remove+insert), so the rule keeps its own _id, and its trigger/
action keep theirs, across an edit. Every action-template click handler
that used to call rules.createRule or insert Triggers/Actions/Rules
directly now goes through one shared helper,
client/components/rules/rulesSaveHelper.js, which picks create vs. update
based on whether the wizard was opened to edit an existing rule.

The "send an email" rule action already appended the card's title and a
link to it automatically (#3301), even when the user's own template used
none of the {card}/{cardLink} tokens; it did not do the same for the card's
description. server/rulesHelper.js now appends "Description: ..." to the
footer alongside the existing title/link lines, using the vars.description
value buildRuleVars already computed.

Actually attaching the card's FILE ATTACHMENTS to the outgoing email is
deferred - see CHANGELOG TODO Later. Every WeKan email today goes through
either Meteor's `Email.sendAsync`/`EmailLocalization.sendEmail`, which take
{ to, from, subject, text }, with no `attachments` parameter anywhere in
this codebase's call sites; wiring a real file through would need the
mailer wrapper itself to grow attachment support and code to read the file
back out of whichever storage backend (local disk / GridFS / S3) holds it,
which is mailer-level scope well past this rule action.

tests/ruleUpdateInPlace.test.cjs pins rules.updateRule's authorization,
its RuleBleed cross-board guard, and - the part that actually makes it an
edit rather than a second create - that it keeps the rule/trigger/action
_ids stable, including a logic-level round trip against a small in-memory
mock proving a changed actionType does not leave the old type's fields
behind. tests/ruleEmailIncludesDescription.test.cjs pins the new
description line, guarded the same way the existing title/line lines are
(only appended when non-empty).
tests/ruleCardActionSchema.test.cjs (#6630's guard, that desc lives on the
Action document and never leaks onto the Rule document) is updated for the
new saveRuleTriggerAction call shape.

Thanks to jvavassori and xet7 !
2026-09-10 20:37:56 +03:00

70 lines
2.9 KiB
JavaScript

'use strict';
// Regression guard for #6630. Action descriptions belong to Actions; putting
// the same `desc` key in a Rule document makes SimpleSchema reject the whole
// rule with keyNotInSchema.
//
// #2713 routed every card-action handler through
// rulesSaveHelper.saveRuleTriggerAction(boardId, ruleId, ruleName, trigger,
// actionDoc) instead of raw Actions.insert()/Rules.insert() calls, so this
// now checks: the action object literal passed to that helper still carries
// `desc`, and the rule document server/rulesButton.js actually persists
// (both rules.createRule's `ruleDoc` and rules.updateRule's `ruleSet`) never
// does.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const source = fs.readFileSync(
path.join(__dirname, '..', 'client/components/rules/actions/cardActions.js'),
'utf8',
);
const rulesButtonSource = fs.readFileSync(
path.join(__dirname, '..', 'server/rulesButton.js'),
'utf8',
);
function handler(selector, nextSelector) {
const start = source.indexOf(`'click ${selector}'`);
assert.notEqual(start, -1, `${selector} handler exists`);
const end = source.indexOf(`'click ${nextSelector}'`, start + selector.length);
assert.notEqual(end, -1, `${nextSelector} follows ${selector}`);
return source.slice(start, end);
}
function savedActionObjects(block) {
return [...block.matchAll(/saveRuleTriggerAction\([^,]*,[^,]*,[^,]*,[^,]*,\s*\{([\s\S]*?)\}\);/g)]
.map((match) => match[1]);
}
const cases = [
['set current date', '.js-set-date-action', '.js-remove-datevalue-action'],
['remove date value', '.js-remove-datevalue-action', '.js-add-label-action'],
['add member', '.js-add-member-action', '.js-add-removeall-action'],
];
for (const [name, selector, nextSelector] of cases) {
const block = handler(selector, nextSelector);
const actions = savedActionObjects(block);
assert.ok(actions.length > 0, `${name}: calls saveRuleTriggerAction`);
assert.ok(actions.some((object) => /\bdesc\b/.test(object)), `${name}: action object keeps desc`);
}
// Negative guard: the RULE document the server actually persists must never
// carry `desc` - neither on create nor on update (#2713's rules.updateRule).
for (const [methodName, docVar] of [
["rules.createRule", 'ruleDoc'],
["rules.updateRule", 'ruleSet'],
]) {
const methodStart = rulesButtonSource.indexOf(`async '${methodName}'`);
assert.notEqual(methodStart, -1, `${methodName} exists in server/rulesButton.js`);
const nextMethod = rulesButtonSource.indexOf("async '", methodStart + methodName.length);
const block = rulesButtonSource.slice(methodStart, nextMethod === -1 ? undefined : nextMethod);
const docLiteral = block.match(new RegExp(`const ${docVar} = \\{([\\s\\S]*?)\\};`));
assert.ok(docLiteral, `${methodName}: builds a ${docVar} literal`);
assert.ok(!/\bdesc\b/.test(docLiteral[1]), `${methodName}: ${docVar} must not carry desc`);
}
console.log('\nruleCardActionSchema: 8 checks passed');