Files
wekan/tests/importVirtualMembers.test.cjs
T
Lauri Ojansivu 0e0391f6c1 Fix #6506: restore the import "map members" step; unmapped members become virtual users, not the importer.
Regression from c0a1a2784 (v10.12): it dropped `importMapMembers` from the import
wizard's `steps` array and hardcoded an empty membersMapping in finishImport, so the
map-users screen never rendered and every imported card/member/comment was assigned
to the importing user even when the person existed in WeKan.

Client (client/components/import/import.js):
 - restore `steps = ['importTextarea', 'importMapMembers']` so the (auto-suggesting)
   map-members step shows again;
 - finishImport builds membersMapping from the members the user mapped
   (member.wekanId), instead of sending `{}`. The step stays OPTIONAL — the Skip
   button and the textarea "import without mapping" button still bypass it.

Server — unmapped members become virtual users instead of collapsing onto the importer:
 - models/trelloCreator.js: new createPlaceholderUsers(board), run in create() before
   the board is built, so it covers BOTH the Trello JSON import and the Trello API
   (all-boards background) import. For each member not already mapped it maps to an
   existing WeKan user by username / recorded importUsername, else inserts an inert
   virtual user (loginDisabled, isActive:false, authenticationMethod:'imported')
   reusing the Trello member id, so board membership and authorship keep the original
   person;
 - models/wekanCreator.js: createPlaceholderUsers no longer overwrites an explicit UI
   mapping (skip members already in this.members) so a member mapped to a real account
   is not clobbered by the identity placeholder.

A board admin can still map a virtual member to a real user later from the sidebar
(that feature is unchanged). Tests updated: tests/importVirtualMembers.test.cjs and
tests/importMemberMapping.test.cjs.

Thanks to AmigaAbattoir and xet7 !

Fixes #6506,
2026-07-22 18:31:32 +03:00

104 lines
6.0 KiB
JavaScript

'use strict';
// Wiring guards for import member handling:
// - #6506: import shows the OPTIONAL "map members" step so imported members can be
// mapped to existing WeKan users; members left unmapped (or skipped) are brought in
// as virtual (placeholder) users, NOT collapsed onto the importing user.
// - Import is bounded by a hang-mitigation watchdog (client + server) so it can never
// spin forever.
// - A board admin can also map a virtual member to an existing real board user LATER
// from the sidebar member-avatar popup, via a board-scoped, no-escalation method.
//
// Run: node tests/importVirtualMembers.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
console.log('importVirtualMembers:');
test('#6506: import shows the map-members step and builds the mapping from it', () => {
const js = read('client/components/import/import.js');
assert.ok(/this\.steps = \['importTextarea', 'importMapMembers'\]/.test(js),
'the wizard includes the importMapMembers step again');
// finishImport builds the mapping from the members the user mapped (member.wekanId).
const fi = js.slice(js.indexOf('this.finishImport'));
assert.ok(/this\.membersToMap\.get\(\)/.test(fi) && /member\.wekanId/.test(fi),
'finishImport builds membersMapping from the mapped members');
assert.ok(/mappingById\[member\.id\] = member\.wekanId/.test(fi),
'each mapped member maps its imported id to the chosen existing user');
// The skip button and the textarea "without mapping" button both still bypass it.
assert.ok(/js-import-skip-mapping/.test(js) && /js-import-without-mapping/.test(js),
'skip / import-without-mapping remain available');
});
test('#6506: unmapped Trello members become virtual users (both JSON and API import)', () => {
const tc = read('models/trelloCreator.js');
assert.ok(/async createPlaceholderUsers\(board\)/.test(tc), 'trelloCreator creates placeholder users');
assert.ok(/await this\.createPlaceholderUsers\(board\)/.test(tc), 'create() runs it before building the board');
// Unmapped members map to an existing user by username, else a virtual placeholder.
assert.ok(/if \(this\.members\[member\.id\]\) continue;/.test(tc), 'already-mapped members are kept');
assert.ok(/ReactiveCache\.getUser\(\{ username: member\.username \}\)/.test(tc), 'auto-maps by username');
assert.ok(/loginDisabled: true/.test(tc) && /isActive: false/.test(tc) && /authenticationMethod: 'imported'/.test(tc),
'placeholders are inert imported users');
// wekanCreator keeps an explicit UI mapping instead of overwriting it with a placeholder.
const wc = read('models/wekanCreator.js');
assert.ok(/if \(this\.members\[u\._id\]\) continue;/.test(wc),
'wekanCreator does not clobber an explicit mapping with the identity placeholder');
});
test('import is bounded by a client-side watchdog (no infinite spinner)', () => {
const js = read('client/components/import/import.js');
assert.ok(/Meteor\.setTimeout\(/.test(js) && /import-timeout/.test(js), 'a watchdog surfaces import-timeout');
assert.ok(/Meteor\.clearTimeout\(watchdog\)/.test(js), 'the watchdog is cleared on completion');
});
test('import is bounded on the server by a hard deadline', () => {
const srv = read('models/import.js');
assert.ok(/withDeadline\(/.test(srv), 'importBoard wraps creator.create with a deadline');
assert.ok(/WEKAN_IMPORT_TIMEOUT_MS/.test(srv), 'deadline is configurable');
assert.ok(/'import-timeout'/.test(srv), 'server raises import-timeout');
});
test('the deadline + map-plan helpers are CommonJS (importable + testable)', () => {
assert.ok(/module\.exports = \{ withDeadline \}/.test(read('models/lib/withDeadline.js')));
assert.ok(/module\.exports = \{ planBoardMemberMapping \}/.test(read('models/lib/boardMemberMapPlan.js')));
});
test('the board-scoped map method is board-scoped and no-escalation', () => {
const srv = read('server/importedUserReconciliation.js');
assert.ok(/mapImportedBoardMember\b/.test(srv), 'exposes mapImportedBoardMember');
assert.ok(/planBoardMemberMapping\(/.test(srv), 'authorizes/decides via the pure planner');
// Scoped to one board: card queries and the membership update are keyed by boardId.
assert.ok(/Cards\.find\(\{ boardId, /.test(srv), 'card reassignment is scoped to the board');
assert.ok(/Boards\.direct\.updateAsync\(boardId,/.test(srv), 'only this board\'s members are changed');
// Activities/comments reassignment is also board-scoped.
assert.ok(/\{ boardId, userId: placeholderId \}/.test(srv), 'activities/comments reassignment is board-scoped');
});
test('the sidebar offers mapping only for virtual members, to existing real board users', () => {
const js = read('client/components/sidebar/sidebar.js');
const jade = read('client/components/sidebar/sidebar.jade');
assert.ok(/isImportedMember\(\)/.test(js) && /authenticationMethod === 'imported'/.test(js),
'a virtual-member helper detects imported placeholders');
assert.ok(/if isImportedMember/.test(jade), 'the map action shows only for virtual members');
assert.ok(/'click \.js-map-imported-member': Popup\.open\('mapImportedMember'\)/.test(js), 'opens the picker');
assert.ok(/Meteor\.call\('mapImportedBoardMember'/.test(js), 'picking a target calls the board-scoped method');
// The picker excludes imported placeholders (can only map onto REAL users) and the self.
assert.ok(/authenticationMethod !== 'imported'/.test(js) && /activeMembers\(\)/.test(js),
'targets are existing active real board members only');
});
test('i18n keys for the map action exist', () => {
const en = JSON.parse(read('imports/i18n/data/en.i18n.json'));
for (const k of ['map-to-existing-user', 'map-to-existing-user-desc', 'map-to-existing-user-none', 'import-timeout']) {
assert.ok(typeof en[k] === 'string' && en[k].length > 0, `missing i18n key: ${k}`);
}
});
console.log(`\n${passed} tests passed`);