Files
wekan/tests/ldapPhoto.test.cjs
T
Lauri Ojansivu bb7fe88bd5 Tests: unit + negative tests for the avatar/import-export feature; extract pure logic
Split the testable, security- and correctness-critical logic of the new avatars +
original-members feature into pure Node modules (module.exports, no Meteor) and cover them
with plain-Node tests/*.test.cjs (positive AND negative), matching the repo's existing
unit-test style. Wired into the test:unit:node script.

- models/lib/avatarUrlSafety.js  (from server/lib/localizeAvatar.js): isExternalAvatarUrl,
  isBlockedIp (SSRF ranges), decodeDataUri, checkUrlHostSync. Tests assert external vs
  local/relative/non-http URLs, that private/loopback/link-local/CGNAT/metadata/multicast
  IPv4+IPv6 (incl. IPv4-mapped) are blocked while genuine public addresses and near-miss
  ranges are allowed, data: decoding, and that non-http(s) schemes, localhost and
  metadata hosts are rejected.
- models/lib/importedUserReconciliationPlan.js (from server/importedUserReconciliation.js):
  planReconciliation. Tests: matches merge, non-matches deactivate, never match another
  placeholder or self or a username-less placeholder.
- packages/wekan-ldap/server/ldapPhoto.js (from sync.js): getLdapPhotoBuffer. Tests the
  Buffer / array / {type:'Buffer'} / base64 shapes and jpegPhoto>thumbnailPhoto, and that
  missing/empty/short values yield null (never throw).
- models/lib/importedBoardMemberPlan.js (from wekanCreator.js): planImportedBoardMember.
  Tests: unmapped member kept by original id but inactive+non-admin, mapped role preserved,
  importer and id-less members dropped.

The extractions are behaviour-preserving; the app modules now import these helpers.

Thanks to xet7.
2026-07-13 09:16:33 +03:00

69 lines
2.9 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for the LDAP photo (jpegPhoto/thumbnailPhoto) buffer
// extraction. Run: node tests/ldapPhoto.test.cjs
const assert = require('assert');
const { getLdapPhotoBuffer } = require('../packages/wekan-ldap/server/ldapPhoto');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a]);
// ── positive: the various shapes ldapjs can produce ──────────────────────────
test('reads a Buffer jpegPhoto from _raw', () => {
const out = getLdapPhotoBuffer({ _raw: { jpegPhoto: png } });
assert.ok(Buffer.isBuffer(out) && out.equals(png));
});
test('reads a Buffer jpegPhoto from the entry itself (no _raw)', () => {
const out = getLdapPhotoBuffer({ jpegPhoto: png });
assert.ok(out && out.equals(png));
});
test('reads the first element of an array of Buffers', () => {
const out = getLdapPhotoBuffer({ _raw: { jpegPhoto: [png, Buffer.from('second')] } });
assert.ok(out && out.equals(png));
});
test('reads a serialized { type:"Buffer", data:[…] }', () => {
const out = getLdapPhotoBuffer({ _raw: { jpegPhoto: { type: 'Buffer', data: Array.from(png) } } });
assert.ok(out && out.equals(png));
});
test('reads a long base64 string', () => {
const b64 = png.toString('base64').repeat(8); // > 32 chars
const out = getLdapPhotoBuffer({ _raw: { jpegPhoto: b64 } });
assert.ok(Buffer.isBuffer(out) && out.length > 0);
});
test('falls back to thumbnailPhoto when jpegPhoto is absent', () => {
const out = getLdapPhotoBuffer({ _raw: { thumbnailPhoto: png } });
assert.ok(out && out.equals(png));
});
test('prefers jpegPhoto over thumbnailPhoto', () => {
const other = Buffer.from('thumb');
const out = getLdapPhotoBuffer({ _raw: { jpegPhoto: png, thumbnailPhoto: other } });
assert.ok(out && out.equals(png));
});
// ── negative: nothing usable → null, never a throw ───────────────────────────
test('returns null when there is no photo attribute', () => {
assert.strictEqual(getLdapPhotoBuffer({ _raw: { cn: 'Alice' } }), null);
});
test('returns null for an empty / undefined / null user', () => {
assert.strictEqual(getLdapPhotoBuffer({}), null);
assert.strictEqual(getLdapPhotoBuffer(null), null);
assert.strictEqual(getLdapPhotoBuffer(undefined), null);
});
test('ignores an empty array and empty values', () => {
assert.strictEqual(getLdapPhotoBuffer({ _raw: { jpegPhoto: [] } }), null);
assert.strictEqual(getLdapPhotoBuffer({ _raw: { jpegPhoto: '' } }), null);
assert.strictEqual(getLdapPhotoBuffer({ _raw: { jpegPhoto: null } }), null);
});
test('ignores a too-short string (not a real base64 photo)', () => {
assert.strictEqual(getLdapPhotoBuffer({ _raw: { jpegPhoto: 'abc' } }), null);
});
console.log(`\nldapPhoto: ${passed} tests passed`);