Files
wekan/tests/ldapBackgroundSync.test.cjs
T
Lauri Ojansivu b933a69cd4 People shows everyone's Last active; inactive users can be disabled; LDAP sync honours its search filter.
Admin Panel / People showed "Last active" as "-" for every user but the
administrator: the people publication did not carry lastConnectionDate.

USERS_DISABLE_INACTIVE_AFTER_DAYS disables, once a night, accounts that have
not used WeKan for that many days, and revokes their logins. Admins are
never disabled. An account with no recorded activity starts its count on the
first run rather than from createdAt, and re-enabling an account restarts it.
The setting is in Admin Panel / People / Login as well as in the
environment, and the job resolves it every night, so a change there needs no
restart. Every platform's settings file lists it.

LDAP background sync looked a user up by id without LDAP_USER_SEARCH_FILTER,
so an account the filter excludes - one disabled in Active Directory - could
not log in but was still "found". It was never disabled, and a manual
disable was undone on the next run. The filter is now ANDed into the lookup.
An account disabled for inactivity is not re-enabled by the sync.

From the patch by Raymond-Voogt, with the setting moved into the Admin
Panel's login settings. Tests: tests/inactiveUsers.test.cjs,
tests/ldapBackgroundSync.test.cjs, tests/ldapUserIdFilter.test.cjs,
tests/authConfigCatalog.test.cjs and
tests/playwright/specs/inactive-users-last-active.e2e.js.

Thanks to Raymond-Voogt and xet7 !

Fixes #6783,
Fixes #6784,
Fixes #6785.
2026-10-19 17:35:51 +03:00

147 lines
4.1 KiB
JavaScript

'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const {
ldapPresenceUpdate,
} = require('../packages/wekan-ldap/server/presenceSync');
const read = (relative) =>
fs.readFileSync(path.join(__dirname, '..', relative), 'utf8');
const syncSource = read('packages/wekan-ldap/server/sync.js');
const ldapSource = read('packages/wekan-ldap/server/ldap.js');
const documentation = read('docs/Features/Login-Page/LDAP.md');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
test('#4654 disables a confirmed missing LDAP user when explicitly enabled', () => {
assert.deepStrictEqual(
ldapPresenceUpdate({
ldapUserFound: false,
disableNonexistentUsers: true,
loginDisabled: false,
}),
{ loginDisabled: true, loginDisabledReason: 'ldap' },
);
});
test('an account disabled for inactivity is not re-enabled by LDAP sync', () => {
assert.strictEqual(
ldapPresenceUpdate({
ldapUserFound: true,
disableNonexistentUsers: true,
loginDisabled: true,
loginDisabledReason: 'inactive',
}),
null,
);
// ...but one LDAP disabled itself, or an admin disabled, still is.
for (const loginDisabledReason of ['ldap', undefined]) {
assert.deepStrictEqual(
ldapPresenceUpdate({
ldapUserFound: true,
disableNonexistentUsers: true,
loginDisabled: true,
loginDisabledReason,
}),
{ loginDisabled: false },
);
}
});
test('#4654 re-enables an LDAP user that reappears', () => {
assert.deepStrictEqual(
ldapPresenceUpdate({
ldapUserFound: true,
disableNonexistentUsers: true,
loginDisabled: true,
}),
{ loginDisabled: false },
);
});
test('negative: default mode never changes active status', () => {
for (const ldapUserFound of [true, false]) {
for (const loginDisabled of [true, false, undefined]) {
assert.strictEqual(
ldapPresenceUpdate({
ldapUserFound,
disableNonexistentUsers: false,
loginDisabled,
}),
null,
);
}
}
});
test('negative: already-correct states do not produce redundant writes', () => {
assert.strictEqual(
ldapPresenceUpdate({
ldapUserFound: true,
disableNonexistentUsers: true,
loginDisabled: false,
}),
null,
);
assert.strictEqual(
ldapPresenceUpdate({
ldapUserFound: false,
disableNonexistentUsers: true,
loginDisabled: true,
}),
null,
);
});
test('#4654 repeat sync uses the stored ID attribute and real update writes', () => {
assert.match(
syncSource,
/getUserById\(user\.services\.ldap\.id, user\.services\.ldap\.idAttribute\)/,
);
assert.match(
syncSource,
/updateAsync\(\{ _id: user\._id \}, \{ \$set: \{ username \}\}\)/,
);
assert.match(syncSource, /'emails\.0\.address': email/);
assert.match(syncSource, /'profile\.fullname' : fullname/);
assert.doesNotMatch(
syncSource,
/findOne(?:Async)?\(\{ _id: user\._id \}, \{ \$set:/,
);
});
test('ambiguous or unconfigured ID lookups abort instead of disabling users', () => {
assert.match(ldapSource, /if \(!filter\) \{\s*throw new Error\(/);
assert.match(ldapSource, /if \(result\.length > 1\) \{\s*throw new Error\(/);
assert.match(ldapSource, /ambiguous LDAP background-sync update/);
});
test('the background loop wires both presence outcomes through the helper', () => {
const calls = syncSource.match(/ldapPresenceUpdate\(\{/g) || [];
assert.strictEqual(calls.length, 2);
assert.match(syncSource, /ldapUserFound: true/);
assert.match(syncSource, /ldapUserFound: false/);
assert.match(syncSource, /\{ \$set: presenceUpdate \}/);
});
test('documentation distinguishes confirmed removal from lookup failure', () => {
assert.match(documentation, /successful LDAP search[\s\S]*no longer present/);
assert.match(
documentation,
/LDAP_BACKGROUND_SYNC_KEEP_EXISTANT_USERS_UPDATED=true/,
);
assert.match(
documentation,
/Lookup errors,[\s\S]*abort the run instead of[\s\S]*disabling accounts/,
);
});
console.log(`\nldapBackgroundSync: all ${passed} tests passed`);