Files
wekan/tests/ruleRemoveMemberAction.test.cjs
T
Lauri Ojansivu 6f79e87914 Add the missing acting-user module and UI button for rule "add member" (#2522).
The "add member" rule action's acting-user option (issue #2522 - "add
whoever just moved the card as a member") landed on main missing its
shared module: server/rulesHelper.js and cardActions.js already import
RULE_ACTING_USER_SENTINEL/resolveActingUserId from
/models/lib/ruleActingUser, but that file was never committed, breaking
the import on main. It also landed without the actual UI button that
saves the sentinel action.

models/lib/ruleActingUser.js resolves "who triggered this rule" from
activity.userId - the exact same source buildRuleVars() already uses for
the {username} template variable (#3304/#3301) - so both call one shared
function instead of two implementations of the same concept.
cardActions.jade gets the missing trigger button, wired to the existing
js-add-actinguser-member-action handler in cardActions.js.

tests/ruleAddMemberActingUser.test.cjs pins the sentinel resolution, that
rulesHelper.js and buildRuleVars() both call the one shared resolver (with
a negative, whole-tree source-pattern check that no other file
re-implements the same activity.userId shape), that the ordinary
fixed-member path is unaffected, the client/jade wiring, and that every
locale file has a real (non-English-placeholder) translation of the new
label. tests/ruleRemoveMemberAction.test.cjs and
tests/rulesApiTriggerNormalize.test.cjs are updated for the addMember
block's new if/else shape around the sentinel check.

Thanks to arisjr and xet7 !

Closes #2522,
2026-09-10 20:46:04 +03:00

86 lines
3.8 KiB
JavaScript

'use strict';
// Regression guard for #2674 ("Rule: remove USER from card when move" — a member
// added by a move-to rule was never removed by a move-from rule, and the
// "remove every member" action removed nobody).
// Run: node tests/ruleRemoveMemberAction.test.cjs
//
// The rule member actions operate on the card's ASSIGNEES: `addMember` calls
// card.assignMember() and `removeMember` calls card.unassignMember() (both write
// card.assignees). The "remove all" branch (username === '*') must therefore
// iterate the card's assignees too. It previously iterated card.MEMBERS, a
// different collection, so it never removed anyone a rule had assigned — the
// action silently did nothing (@mweiss237's "remove every member" report).
//
// This is a source guard because performAction() is Meteor-coupled (ReactiveCache,
// Card model methods) and cannot run under plain Node; the invariant we protect is
// that add and remove act on the SAME collection.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const src = fs.readFileSync(
path.join(__dirname, '..', 'server', 'rulesHelper.js'),
'utf8',
);
let passed = 0;
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
// Isolate the removeMember action block so assertions are specific to it.
const removeBlock = (() => {
const start = src.indexOf("action.actionType === 'removeMember'");
assert.ok(start > -1, 'removeMember action must exist');
return src.slice(start, start + 1100);
})();
const addBlock = (() => {
const start = src.indexOf("action.actionType === 'addMember'");
assert.ok(start > -1, 'addMember action must exist');
// #2522: the block grew to cover the acting-user sentinel branch as well
// as the original fixed-member branch, so slice up to the next action
// instead of a fixed byte count.
const end = src.indexOf("if (action.actionType === 'removeMember')", start);
assert.ok(end > start, 'removeMember action must follow addMember');
return src.slice(start, end);
})();
check('addMember assigns (writes card.assignees via assignMember)', () => {
assert.ok(/card\.assignMember\(/.test(addBlock),
'addMember must call card.assignMember()');
});
check('removeMember (single user) unassigns the same collection', () => {
assert.ok(/card\.unassignMember\(member\._id\)/.test(removeBlock),
'removeMember must call card.unassignMember(member._id)');
});
check('#2674: "remove all" (username === "*") iterates ASSIGNEES, not members', () => {
// The bug: iterating card.members while unassigning from card.assignees.
assert.ok(/getAssignees\(\)|card\.assignees/.test(removeBlock),
'the "*" branch must read the card assignees');
assert.ok(!/const members = card\.members/.test(removeBlock),
'the "*" branch must NOT iterate card.members (wrong collection)');
});
check('#2674: "remove all" still unassigns each entry (removal actually happens)', () => {
const star = removeBlock.slice(removeBlock.indexOf("=== '*'"));
assert.ok(/for \([^)]*\)[\s\S]*card\.unassignMember\(/.test(star),
'the "*" branch must loop and call card.unassignMember() for each assignee');
});
check('symmetry: add and remove act on the SAME concept (assign/unassign)', () => {
// add -> assignMember, remove -> unassignMember: a user added by a move-to
// rule is removed by a move-from rule (the core #2674 report).
assert.ok(/assignMember\(/.test(addBlock) && /unassignMember\(/.test(removeBlock),
'add uses assignMember, remove uses unassignMember');
});
check('negative: an unresolved username is skipped, not crashed on (#2674 defensive)', () => {
assert.ok(/user "\$\{action\.username\}" not found; skipping/.test(removeBlock) ||
/not found; skipping/.test(removeBlock),
'a missing user must warn+skip rather than throw on undefined._id');
});
console.log(`\nruleRemoveMemberAction: ${passed} checks passed`);