Files
wekan/tests/boardCardScope.test.cjs
T
Lauri Ojansivu e46d7a2376 Destination pickers list the cards of a lazily loaded board.
Copy Checklist's card dropdown stayed empty for a board with more cards
than the lazy-loading threshold (CARDS_LOADING=auto above 500, or lazy).
The pickers read the chosen board's cards from the `board` publication,
which ships no cards for a lazy board, and a picker opens no list that
would fetch its window. A new `destinationCards` publication ships the
titles and places of one list's cards in one swimlane (or of the whole
board for the parent-card picker), whatever the card-loading mode, scoped
like the other card publications: the board must be readable, and an
assigned-only member gets only their own cards.

Copy / Move checklist, Copy checklist from a template card, Move / Copy
card, Link card and the parent-card picker subscribe to it. Done waits
until the chosen list's cards are there, a remembered card is checked
against them rather than against the board, and the open board's Filter
no longer empties the destination list.

Thanks to ChrisMagnuson and xet7 !

Fixes #6917.
2026-11-15 03:11:14 +02:00

230 lines
12 KiB
JavaScript

'use strict';
// Tests for models/lib/boardCardScope.js and its use in the card publications.
//
// These helpers retain the normal MongoDB selector shapes. Compatibility databases
// must optimize those shapes rather than requiring application-specific rewrites.
// Run: node tests/boardCardScope.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const {
boardScopeIds,
boardCardScope,
isAssignedOnlyMember,
assignedOnlyCardScope,
mergeCardScope,
} = require('../models/lib/boardCardScope');
let passed = 0;
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
check('a board without a deposit excludes null and orphan card boards', () => {
assert.deepStrictEqual(boardScopeIds({ _id: 'B' }), ['B']);
assert.deepStrictEqual(boardCardScope({ _id: 'B' }), { boardId: { $in: ['B'] } });
});
check('null and undefined subtasksDefaultBoardId use the same MongoDB selector', () => {
assert.deepStrictEqual(boardCardScope({ _id: 'B', subtasksDefaultBoardId: null }),
{ boardId: { $in: ['B'] } });
assert.deepStrictEqual(boardCardScope({ _id: 'B' }),
{ boardId: { $in: ['B'] } });
});
check('a real subtasks-default board -> an all-string $in (still pushes down)', () => {
assert.deepStrictEqual(boardScopeIds({ _id: 'B', subtasksDefaultBoardId: 'S' }), ['B']);
assert.deepStrictEqual(boardCardScope({ _id: 'B', subtasksDefaultBoardId: 'S' }),
{ boardId: { $in: ['B'] } });
});
check('tolerates a missing board', () => {
assert.deepStrictEqual(boardScopeIds(undefined), []);
assert.deepStrictEqual(boardScopeIds({}), []);
assert.deepStrictEqual(boardCardScope({ _id: 'B', subtasksDefaultBoardId: 5 }),
{ boardId: { $in: ['B'] } });
});
// ── source guards: no card query still uses the null-containing $in ────────────
check('board/card publications use boardCardScope, not the null-in-$in literal', () => {
for (const rel of [
'server/publications/boards.js',
'server/publications/cardsWindow.js',
'server/publications/cards.js',
]) {
const src = fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
assert.ok(!/boardId: \{ \$in: \[board\._id, board\.subtasksDefaultBoardId\] \}/.test(src),
`${rel} must not use the null-containing $in card scope`);
assert.ok(/boardCardScope\(board\)/.test(src), `${rel} must use boardCardScope`);
}
});
check('the lazy window keeps ordinary MongoDB conjunction semantics', () => {
assert.deepStrictEqual(
mergeCardScope({ listId: 'L', swimlaneId: 'S' }, { boardId: 'B', archived: false }),
{ $and: [
{ listId: 'L', swimlaneId: 'S' },
{ boardId: 'B', archived: false },
] });
const src = fs.readFileSync(path.join(__dirname, '..', 'server', 'publications', 'cardsWindow.js'), 'utf8');
assert.ok(/mergeCardScope\(safe, \{/.test(src), 'the window selector merges through it');
});
// ── assigned-only members ─────────────────────────────────────────────────────
//
// isReadAssignedOnly / isNormalAssignedOnly / isCommentAssignedOnly all mean the
// same thing: the member may only see the cards they are assigned to. The `board`
// publication has always narrowed its card cursor that way; `boardCardsWindow` —
// which is what ships the cards in LAZY card-loading mode — did not, so whether
// the restriction applied at all depended on the board's card-loading mode.
const member = (userId, extra = {}) => ({ userId, isActive: true, ...extra });
const boardWith = (...members) => ({ _id: 'B', members });
check('each of the three assigned-only flags restricts the member', () => {
for (const flag of ['isReadAssignedOnly', 'isNormalAssignedOnly', 'isCommentAssignedOnly']) {
const board = boardWith(member('ada', { [flag]: true }));
assert.strictEqual(isAssignedOnlyMember(board, 'ada'), true, flag);
assert.deepStrictEqual(assignedOnlyCardScope(board, 'ada'),
{ assignees: { $in: ['ada'] } }, flag);
}
});
check('an ordinary member is not restricted', () => {
const board = boardWith(member('ada'), member('bob', { isAdmin: true }));
assert.strictEqual(isAssignedOnlyMember(board, 'ada'), false);
assert.strictEqual(assignedOnlyCardScope(board, 'ada'), null);
assert.strictEqual(assignedOnlyCardScope(board, 'bob'), null);
});
check('an INACTIVE assigned-only member document does not restrict', () => {
// Matches findWhere(board.members, { userId, isActive: true }) in the board
// publication: a removed member's leftover document must not narrow anything.
const board = boardWith({ userId: 'ada', isActive: false, isReadAssignedOnly: true });
assert.strictEqual(assignedOnlyCardScope(board, 'ada'), null);
});
check('a non-member and a logged-out visitor are not restricted here', () => {
// Deliberate, and the same as the board publication: the restriction narrows
// what a MEMBER sees; whether a non-member may see the board at all is decided
// before this, by boardVisibleTo / isVisibleBy.
const board = boardWith(member('ada', { isReadAssignedOnly: true }));
assert.strictEqual(assignedOnlyCardScope(board, 'zoe'), null);
assert.strictEqual(assignedOnlyCardScope(board, null), null);
assert.strictEqual(assignedOnlyCardScope(board, undefined), null);
});
check('negative: a board with no/!array members never throws', () => {
assert.strictEqual(assignedOnlyCardScope({ _id: 'B' }, 'ada'), null);
assert.strictEqual(assignedOnlyCardScope({ _id: 'B', members: null }, 'ada'), null);
assert.strictEqual(assignedOnlyCardScope({ _id: 'B', members: {} }, 'ada'), null);
assert.strictEqual(assignedOnlyCardScope(null, 'ada'), null);
assert.strictEqual(assignedOnlyCardScope({ _id: 'B', members: [null] }, 'ada'), null);
});
check('the restriction survives a client selector that filters by assignee', () => {
// The board Filter has an assignee filter of its own, so this collision is
// reachable from the UI. A top-level merge would let one silently replace the
// other — and in the direction the publication spreads them, the CLIENT's value
// would win and the restriction would be gone. $and keeps both, so an
// assigned-only member filtering for someone else gets nothing rather than
// everything.
const board = boardWith(member('ada', { isReadAssignedOnly: true }));
const clientFilter = { listId: 'L', assignees: { $in: ['bob'] } };
const merged = mergeCardScope(clientFilter, {
boardId: 'B',
archived: false,
...assignedOnlyCardScope(board, 'ada'),
});
assert.deepStrictEqual(merged, {
$and: [clientFilter, { boardId: 'B', archived: false, assignees: { $in: ['ada'] } }],
});
});
check('an unrestricted member also keeps client and server selectors separate', () => {
const board = boardWith(member('ada'));
const merged = mergeCardScope({ listId: 'L' }, {
boardId: 'B',
archived: false,
...assignedOnlyCardScope(board, 'ada'),
});
assert.deepStrictEqual(merged, {
$and: [{ listId: 'L' }, { boardId: 'B', archived: false }],
});
});
check('both the window and its COUNT are narrowed', () => {
const src = fs.readFileSync(path.join(__dirname, '..', 'server', 'publications', 'cardsWindow.js'), 'utf8');
// On the CODE: the comments explain the restriction and name the function, and a
// guard that counts its own explanation counts wrong.
const code = src.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
const uses = code.match(/\.\.\.assignedOnlyCardScope\(/g) || [];
// #6917 added a third card publication, destinationCards (the card titles a
// destination picker lists); it is narrowed the same way.
assert.strictEqual(uses.length, 3,
'boardCardsWindow, boardListCardCount AND destinationCards — an unrestricted '
+ 'count would still tell the member how many cards the list really holds, '
+ 'and an unrestricted picker would list the titles of cards they may not see');
const picker = code.slice(code.indexOf("Meteor.publish('destinationCards'"));
assert.ok(/\.\.\.assignedOnlyCardScope\(board, this\.userId\)/.test(picker),
'destinationCards narrows its cards for an assigned-only member');
// The children (comments/attachments/checklists/items) hang off windowCardIds,
// which builds on the same selector — so they cannot leak past the restriction.
// #6745: windowCardIds now shares ONE query per board object between the
// children (a promise memoized in a WeakMap), so it is no longer `async`;
// it must still build on windowSel(board).
assert.ok(/const windowCardIds = (?:async )?board => \{[\s\S]*?windowSel\(board\)/.test(src),
'the window children are derived from the restricted window selector');
});
check('the composite parent publishes the members its children read', () => {
// publish-composite hands each child the document as the PARENT cursor published
// it. With the old `{ _id: 1 }` projection, board.members was undefined in every
// child, so assignedOnlyCardScope() would have found no flag to act on and the
// restriction would have been dead code.
const src = fs.readFileSync(path.join(__dirname, '..', 'server', 'publications', 'cardsWindow.js'), 'utf8');
// #6745: allowsCommentsOnMinicard rides along (the window's comment projection).
assert.ok(/fields: \{ _id: 1, members: 1(, allowsCommentsOnMinicard: 1)? \}/.test(src),
'the parent cursor must publish members');
assert.ok(!/fields: \{ _id: 1 \}, limit: 1/.test(src),
'the id-only projection must not come back');
});
// AssignedBleed copy sibling (2026-10-02): copyCard, saveCardAsTemplate,
// copySwimlane, copyList and moveList checked only that the caller belongs to
// the source board, so an assigned-only member could copy cards they cannot
// see - by id, or a whole list or swimlane - into a board of their own.
check('an assigned-only member copies only a card assigned to them, never a container', () => {
const { mayCopyFromBoard } = require('../models/lib/boardCardScope');
const board = flags => ({ members: [{ userId: 'u', isActive: true, ...flags }] });
const mine = { assignees: ['u'] }, other = { assignees: ['x'] };
for (const flags of [{ isNormalAssignedOnly: true }, { isReadAssignedOnly: true }, { isCommentAssignedOnly: true }]) {
assert.strictEqual(mayCopyFromBoard(board(flags), 'u', mine), true);
assert.strictEqual(mayCopyFromBoard(board(flags), 'u', other), false, 'an unassigned card');
assert.strictEqual(mayCopyFromBoard(board(flags), 'u'), false, 'a list or swimlane');
}
// Everyone else copies as before (negative).
assert.strictEqual(mayCopyFromBoard(board({}), 'u', other), true);
assert.strictEqual(mayCopyFromBoard(board({}), 'u'), true);
});
check('every copy and move of board content asks the rule', () => {
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
const cases = [
['server/models/cards.js', ' async copyCard(', "mayCopyFromBoard(sourceBoard, this.userId, card)"],
['server/models/cards.js', ' async saveCardAsTemplate(', "mayCopyFromBoard(sourceBoard, this.userId, card)"],
['server/models/lists.js', ' async copyList(', "mayCopyFromBoard(sourceBoard, this.userId)"],
['server/models/lists.js', ' async moveList(', "mayCopyFromBoard(sourceBoard, this.userId)"],
['server/publications/swimlanes.js', ' async copySwimlane(', "mayCopyFromBoard(sourceBoard, this.userId)"],
];
for (const [file, start, call] of cases) {
const src = read(file), at = src.indexOf(start);
assert.ok(at >= 0, `${file}${start}`);
const body = src.slice(at, src.indexOf('\n },', at));
assert.ok(body.includes(call), `${start} asks mayCopyFromBoard`);
}
});
console.log(`\nboardCardScope: ${passed} checks passed`);