Files
wekan/tests/boardMemberMapPlan.test.cjs
T
Lauri Ojansivu 642fa8e93f "Map to existing user" now searches every user, not only board members.
After importing a Trello board and choosing to map users later, "Map to existing
user" listed only the admin: the picker offered nothing but the ACTIVE REAL
MEMBERS of the board, and right after an import the real people have no
membership at all - the placeholders do. There was no search box either, so on a
board whose only real member is the importing admin there was literally nobody to
pick.

The picker now has a search box that goes through the same permission-checked
`searchUsers` method the normal add-member typeahead uses, so the same board
membership check and the same #6116 same-org/team restriction apply. With the box
empty it still lists the board's real members (the common case, which changes no
roles at all); typing searches every real user and labels the ones who are not on
this board yet.

Mapping onto a user who is not a board member adds them to the board with the
PLACEHOLDER's OWN role - the role the import recorded for that person - never a
higher one, and only after passing exactly the checks inviting them would: the
Admin Panel "roles allowed to invite" setting, the same-org/team restriction, and
the refusal to add a deactivated account. Adding a member with a chosen role is
something the board admin doing the mapping can already do through the normal
add-member flow, so this grants no capability the caller did not have; an
existing active member is still left completely untouched, entry and role.

Also fixes sanitizeUserForSearch dropping `profile` from every search result: it
read the Mongo-style paths in its allowlist ('profile.fullname') as FLAT keys, so
the lookup was always undefined and both this picker and the add-member typeahead
showed a blank name with only the username in brackets. Dotted paths are now
copied into the same nested shape, still allowlisting exactly the listed leaves.

Adds positive and negative unit tests for the new planner contract, including
that a comment-only placeholder can never yield an admin, that reactivating an
inactive membership does not duplicate the entry, and that the invite policy runs
before any write.

Thanks to AmigaAbattoir and xet7 !

Fixes #6519,
2026-07-24 03:23:37 +03:00

164 lines
7.3 KiB
JavaScript

'use strict';
// Unit tests for the board-scoped virtual->real member mapping planner
// (models/lib/boardMemberMapPlan.js). This is the security gate for mapping an imported
// VIRTUAL (placeholder) board member to a REAL user from the sidebar: it must be usable
// only by a board (or site) admin, only from an imported placeholder, only onto a real
// user, and it must never hand out a role the import did not already record.
//
// #6519: the target no longer has to be an existing active board member. After a
// Trello/CSV import the real people usually have no membership at all, so that rule left
// the picker showing nobody but the admin. A target who is already an ACTIVE member still
// keeps their own entry and role untouched; a target who is NOT a member is added with
// the PLACEHOLDER's own role - never a higher one - which is something the board admin
// doing the mapping can already do through the normal add-member flow.
//
// Run: node tests/boardMemberMapPlan.test.cjs
const assert = require('assert');
const { planBoardMemberMapping } = require('../models/lib/boardMemberMapPlan.js');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
// A board: admin (real), normal member "bob" (real), a virtual placeholder "ph".
const members = [
{ userId: 'admin', isAdmin: true, isActive: true },
{ userId: 'bob', isAdmin: false, isActive: true },
{ userId: 'ph', isAdmin: false, isActive: false },
];
const base = {
members,
placeholderId: 'ph',
targetId: 'bob',
callerId: 'admin',
placeholderIsImported: true,
targetIsImported: false,
callerIsSiteAdmin: false,
};
console.log('boardMemberMapPlan:');
test('board admin maps a virtual member onto an existing active real member', () => {
const r = planBoardMemberMapping(base);
assert.strictEqual(r.ok, true);
// The placeholder entry is removed; every other entry (incl. the target and its role)
// is left EXACTLY as it was — no escalation.
assert.deepStrictEqual(r.newMembers, [
{ userId: 'admin', isAdmin: true, isActive: true },
{ userId: 'bob', isAdmin: false, isActive: true },
]);
const bob = r.newMembers.find(m => m.userId === 'bob');
assert.strictEqual(bob.isAdmin, false, 'target keeps its own (non-admin) role');
});
test('a non-admin board member cannot map', () => {
const r = planBoardMemberMapping({ ...base, callerId: 'bob' });
assert.deepStrictEqual(r, { ok: false, error: 'not-authorized' });
});
test('a site admin who is not a board member may map', () => {
const r = planBoardMemberMapping({ ...base, callerId: 'someSiteAdmin', callerIsSiteAdmin: true });
assert.strictEqual(r.ok, true);
});
test('source must be an imported placeholder', () => {
const r = planBoardMemberMapping({ ...base, placeholderIsImported: false });
assert.deepStrictEqual(r, { ok: false, error: 'not-a-placeholder' });
});
test('target must be a REAL (non-imported) user', () => {
const r = planBoardMemberMapping({ ...base, targetIsImported: true });
assert.deepStrictEqual(r, { ok: false, error: 'target-not-real' });
});
test('an ACTIVE member target is reported as needing no membership change', () => {
const r = planBoardMemberMapping(base);
assert.strictEqual(r.addedMember, false, 'no membership is created for an existing member');
});
// #6519 - the reported bug: only board members could be picked, so a freshly imported
// board (where the real people are not members yet) offered nobody but the admin.
test('a target who is NOT a board member is added with the PLACEHOLDER role', () => {
const r = planBoardMemberMapping({ ...base, targetId: 'stranger' });
assert.strictEqual(r.ok, true);
assert.strictEqual(r.addedMember, true, 'caller must run the invite policy checks');
const added = r.newMembers.find(m => m.userId === 'stranger');
assert.ok(added, 'target is added to the board');
assert.strictEqual(added.isActive, true);
// The placeholder was a plain member, so the mapped-in user is a plain member too.
assert.strictEqual(added.isAdmin, false, 'never more than the placeholder had');
assert.strictEqual(added.isNoComments, false);
assert.strictEqual(added.isCommentOnly, false);
assert.strictEqual(added.isWorker, false);
// The placeholder entry is gone and nobody else changed.
assert.strictEqual(r.newMembers.find(m => m.userId === 'ph'), undefined);
assert.deepStrictEqual(
r.newMembers.find(m => m.userId === 'admin'),
{ userId: 'admin', isAdmin: true, isActive: true },
);
});
test('an INACTIVE member target is reactivated with the placeholder role', () => {
const inactiveTarget = members.map(m => (m.userId === 'bob' ? { ...m, isActive: false } : m));
const r = planBoardMemberMapping({ ...base, members: inactiveTarget });
assert.strictEqual(r.ok, true);
assert.strictEqual(r.addedMember, true);
const bob = r.newMembers.find(m => m.userId === 'bob');
assert.strictEqual(bob.isActive, true);
assert.strictEqual(bob.isAdmin, false);
// Exactly one entry per user - reactivating must not duplicate the membership.
assert.strictEqual(r.newMembers.filter(m => m.userId === 'bob').length, 1);
});
// The no-escalation property, stated directly: a mapped-in user can never come out with a
// role the placeholder did not already have.
test('the added role never exceeds the placeholder role (negative)', () => {
const restricted = [
{ userId: 'admin', isAdmin: true, isActive: true },
{ userId: 'ph', isAdmin: false, isActive: false, isCommentOnly: true },
];
const r = planBoardMemberMapping({ ...base, members: restricted, targetId: 'stranger' });
assert.strictEqual(r.ok, true);
const added = r.newMembers.find(m => m.userId === 'stranger');
assert.strictEqual(added.isAdmin, false, 'a comment-only placeholder cannot yield an admin');
assert.strictEqual(added.isCommentOnly, true, 'the placeholder restriction carries over');
});
test('an admin placeholder yields an admin target only because the caller could do that anyway', () => {
const adminPlaceholder = [
{ userId: 'admin', isAdmin: true, isActive: true },
{ userId: 'ph', isAdmin: true, isActive: false },
];
const r = planBoardMemberMapping({ ...base, members: adminPlaceholder, targetId: 'stranger' });
assert.strictEqual(r.ok, true);
assert.strictEqual(r.newMembers.find(m => m.userId === 'stranger').isAdmin, true);
});
test('a non-admin still cannot map onto a non-member (negative)', () => {
const r = planBoardMemberMapping({ ...base, callerId: 'bob', targetId: 'stranger' });
assert.deepStrictEqual(r, { ok: false, error: 'not-authorized' });
});
test('placeholder must be a member of this board', () => {
const r = planBoardMemberMapping({ ...base, placeholderId: 'ghost' });
assert.deepStrictEqual(r, { ok: false, error: 'placeholder-not-member' });
});
test('placeholder and target must differ', () => {
const r = planBoardMemberMapping({ ...base, targetId: 'ph' });
assert.deepStrictEqual(r, { ok: false, error: 'bad-map' });
});
test('an inactive board-admin entry does not authorize', () => {
const inactiveAdmin = [
{ userId: 'admin', isAdmin: true, isActive: false },
{ userId: 'bob', isAdmin: false, isActive: true },
{ userId: 'ph', isAdmin: false, isActive: false },
];
const r = planBoardMemberMapping({ ...base, members: inactiveAdmin });
assert.deepStrictEqual(r, { ok: false, error: 'not-authorized' });
});
console.log(`\n${passed} tests passed`);