Files
wekan/tests/importMapUserFixes.test.cjs
T
Lauri Ojansivu 2f07b29336 Fix #6508: two console crashes in the import "map users later" workflow.
A) Import "map members": clicking a searched user threw
   `TypeError: Cannot read properties of undefined (reading '_id')` at
   `click .js-select-import`. The handler read `Template.currentData()._id`, which is
   undefined when the click lands on a CHILD of the anchor (the avatar or the name
   span). Read the WeKan user id from the anchor's `data-id` (event.currentTarget)
   instead, and guard it — robust regardless of which inner node was clicked.

B) Admin Panel / People: saving an IMPORTED (placeholder) user as an active person
   with an email threw `TypeError: Cannot read properties of undefined (reading '0')`.
   The profile submit handler read `user.emails[0].verified` (and `.address`), but an
   imported placeholder user (and some SSO users) have no `emails` array at all. Read
   the primary email defensively (missing OR empty array) via a `primaryEmail` guard.

Tests: tests/importMapUserFixes.test.cjs; tests/importMemberMapping.test.cjs updated
for the data-id mapping.

Thanks to AmigaAbattoir and xet7 !

Fixes #6508,
2026-07-23 00:52:46 +03:00

44 lines
2.4 KiB
JavaScript

'use strict';
// Regression for #6508 (import / map imported users later): two console-crash bugs.
// A) Import "map members": clicking a search result threw
// "Cannot read properties of undefined (reading '_id')" at click .js-select-import,
// because it read Template.currentData()._id — undefined when the click lands on a
// child node (the avatar or the name span). Read the _id from the anchor's data-id.
// B) Admin Panel / People: saving an IMPORTED (placeholder) user as active with an
// email threw "Cannot read properties of undefined (reading '0')" because the
// submit handler read user.emails[0] and a placeholder user has no `emails` array.
// Run: node tests/importMapUserFixes.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
let passed = 0;
function check(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
check('A: .js-select-import maps by the anchor data-id, not Template.currentData()', () => {
const js = read('client/components/import/import.js');
const at = js.indexOf("'click .js-select-import'");
assert.ok(at > -1, 'the select-import handler exists');
const block = js.slice(at, at + 700);
assert.ok(/event\.currentTarget\.getAttribute\('data-id'\)/.test(block),
'reads the _id from the anchor data-id');
assert.ok(/if \(id\) importMapToUser\(id\)/.test(block), 'guards the id before mapping');
assert.ok(!/Template\.currentData\(\)\._id/.test(js),
'must not read _id from Template.currentData() (undefined on a child click)');
});
check('B: People submit reads the primary email defensively (no unguarded emails[0])', () => {
const js = read('client/components/settings/peopleBody.js');
assert.ok(/Array\.isArray\(user\.emails\) && user\.emails\.length \? user\.emails\[0\] : null/.test(js),
'primary email is read defensively (missing / empty emails array)');
assert.ok(/primaryEmail \? primaryEmail\.verified : undefined/.test(js), 'verified compares against the guarded email');
assert.ok(/primaryEmail \? primaryEmail\.address\.toLowerCase\(\) : false/.test(js), 'address compares against the guarded email');
// The old unguarded access must be gone.
assert.ok(!/verified !== user\.emails\[0\]\.verified/.test(js), 'the unguarded user.emails[0].verified access is removed');
});
console.log(`\nimportMapUserFixes: ${passed} checks passed`);