Files
wekan/tests/cardCopyRuntimeCache.test.cjs
T
Lauri Ojansivu 63ebdb9072 Copying or linking a card carries only the custom field values of its new board.
A card made from a card template, linked from another board, or copied - and
a subtask copied with it - took its whole customFields array along: values of
another board's fields, of deleted fields, and entries with no field id. The
admin-only field guard could not match those to a field of the new card's
board, refused the whole card and recorded AdminFieldBleed against the user.

copy(), its subtask copies and link() now keep only the values of fields
defined on the destination board (models/lib/customFieldsForBoard.cjs), read
from the collection rather than the cache, just before the insert. An
admin-only value of the destination board is kept, so the guard still decides
it. tests/copyForeignCustomFields.test.cjs fails on the code before the fix,
and fails on any card cloned from another and inserted without the filter.

Thanks to Raymond-Voogt and xet7 !

Fixes #6800,
2026-10-22 19:58:16 +03:00

93 lines
6.7 KiB
JavaScript

"use strict";
(async () => {
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const root = path.resolve(__dirname, '..');
const source = fs.readFileSync(path.join(root, 'models/cards.js'), 'utf8');
// Keep exercising the runtime body when optional copy-pipeline arguments grow.
const copyBody = source.slice(source.indexOf(' async copy('), source.indexOf('\n async link('));
const linkStart = source.indexOf(' async link(');
const linkBody = source.slice(linkStart, source.indexOf('\n list()', linkStart));
const { normalizeDependencies } = await import('../models/metadata/dependencies.js');
// #1759: cross-board labels go through the pure plan (models/lib/crossBoardLabels.js);
// here as a member who is not the destination board's admin, so nothing is created.
const { planCrossBoardLabels } = require('../models/lib/crossBoardLabels');
const crossBoardLabelIds = async (card, oldBoard, newBoard) => planCrossBoardLabels({ sourceLabels: oldBoard.labels,
labelIds: card.labelIds, destLabels: newBoard.labels, canCreate: false, newId: () => 'unused' }).labelIds;
const { buildCopiedSubtaskFields } = await import('../models/lib/subtaskCopy.js');
async function exercise({ sameBoard = false, cache = true, fail = false, withChildren = false, cardIdMap = null } = {}) {
const original = {
_id: 'original', boardId: 'source', swimlaneId: 'old-swimlane', listId: 'old-list',
title: 'Template', labelIds: ['source-label'],
syncExternalId: 'KEY-1', syncSourceType: 'jira', syncSourceKey: 'original-project', syncLastSource: { title: 'Template' },
customFields: [{ _id: 'source-field', value: 'kept' }],
cardDependencies: [{ cardId: 'destination-card', type: 'related' }, { cardId: 'missing-card', type: 'related' }],
...(cache ? { __id: 'original' } : {}),
async mapCustomFieldsToBoard() { return [{ _id: 'destination-field', value: 'kept' }]; },
};
Object.defineProperty(original, 'mapCustomFieldsToBoard', { enumerable: false });
const before = JSON.stringify(original);
const inserted = [], children = [];
const boardId = sameBoard ? 'source' : 'destination';
const Cards = { async insertAsync(document) {
assert.equal(Object.hasOwn(document, '__id'), false, 'runtime cache must not reach schema validation');
assert.equal(Object.hasOwn(document, '_id'), false, 'copies require a new document ID');
for (const key of ['syncExternalId','syncSourceType','syncSourceKey','syncLastSource']) assert.equal(Object.hasOwn(document,key),false, 'copies must not inherit Sync identity');
if (fail) throw new Error('insert rejected');
inserted.push(structuredClone(document)); return `new-${inserted.length}`;
} };
const ReactiveCache = {
getCard: async id => id === original._id ? original : id === 'destination-card' ? { boardId } : null,
getBoard: async id => ({ _id: id, labels: [{ _id: id === 'source' ? 'source-label' : 'destination-label', name: 'Shared' }], async getNextCardNumber() { return 7; } }),
getChecklists: async query => withChildren && query.cardId === 'original' ? [{ async copy(id) { children.push(['checklist', id]); } }] : [],
getCards: async () => withChildren ? [{ _id: 'child', __id: 'child', boardId: 'source', parentId: 'original', title: 'Child' }] : [],
getCardComments: async () => withChildren ? [{ async copy(id, target) { children.push(['comment', id, target]); } }] : [],
};
// #3626: copy() finds the card's subtasks with the shared children selector.
const { childrenSelector } = require('../models/lib/cardParents');
// #6800: copy() keeps only values of fields of the destination board; here
// every field the test copies is one.
const { customFieldsForBoard } = require('../models/lib/customFieldsForBoard.cjs');
const fieldDefinitionNow = async () => ({ boardIds: [boardId] });
const copy = new Function('ReactiveCache', 'Cards', 'Meteor', 'crossBoardLabelIds', 'normalizeDependencies', 'require', 'childrenSelector',
'customFieldsForBoard', 'fieldDefinitionNow',
`return ({${copyBody}}).copy;`)(ReactiveCache, Cards, { isServer: false }, crossBoardLabelIds, normalizeDependencies,
specifier => {
// The copy path now also delegates Scrum metadata to its pure helper.
if (specifier === './lib/scrumCopy') return require('../models/lib/scrumCopy');
assert.equal(specifier, './lib/subtaskCopy'); return { buildCopiedSubtaskFields };
}, childrenSelector, customFieldsForBoard, fieldDefinitionNow);
if (fail) await assert.rejects(copy.call(original, boardId, 'new-swimlane', 'new-list', cardIdMap), /insert rejected/);
else assert.equal(await copy.call(original, boardId, 'new-swimlane', 'new-list', cardIdMap), 'new-1');
assert.equal(JSON.stringify(original), before, 'successful and rejected copies must preserve the source card');
if (!fail) {
assert.equal(inserted[0].boardId, boardId);
assert.equal(inserted[0].listId, 'new-list');
assert.equal(inserted[0].swimlaneId, 'new-swimlane');
assert.equal(inserted[0].cardNumber, 7);
assert.equal(inserted[0].labelIds[0], sameBoard ? 'source-label' : 'destination-label');
assert.equal(inserted[0].customFields[0]._id, sameBoard ? 'source-field' : 'destination-field');
assert.equal(inserted[0].cardDependencies.length, cardIdMap ? 2 : 1);
if (withChildren) {
assert.equal(inserted[1].parentId, 'new-1'); assert.equal(inserted[1].boardId, boardId);
assert.deepEqual(children, [['checklist', 'new-1'], ['comment', 'new-1', boardId]]);
}
}
}
await exercise({ withChildren: true });
await exercise({ sameBoard: true, cache: false });
await exercise({ fail: true });
const map = {}; await exercise({ cardIdMap: map }); assert.equal(map.original, 'new-1');
const linked = { _id: 'original', __id: 'original', boardId: 'source', title: 'Source', labelIds: ['label'] };
const beforeLink = JSON.stringify(linked);
const link = new Function('Cards', 'customFieldsForBoard', 'fieldDefinitionNow', `return ({${linkBody}}).link;`)({ async insertAsync(document) {
assert.equal(Object.hasOwn(document, '__id'), false); assert.equal(Object.hasOwn(document, '_id'), false);
assert.equal(document.linkedId, 'original'); assert.equal(document.type, 'cardType-linkedCard');
assert.equal(document.boardId, 'destination'); return 'linked';
} }, require('../models/lib/customFieldsForBoard.cjs').customFieldsForBoard, async () => ({ boardIds: ['destination'] }));
assert.equal(await link.call(linked, 'destination', 'swimlane', 'list'), 'linked');
assert.equal(JSON.stringify(linked), beforeLink);
console.log('card copy cache: 5 cases pass, including cross-board children, same-board copies, rejected inserts and linked cards');
})().catch(error => { console.error(error); process.exitCode = 1; });