Files
wekan/tests/webhookUsername.test.cjs
T
Lauri Ojansivu 5b83e8e256 Webhooks send the username too, and TODO Later loses three settled entries.
An audit of the TODO Later backlog against the open issues and the current
source. Three no longer belong there.

#3113 - an outgoing webhook said who did something by DISPLAY name and had no
field for the login name. params.user comes from getActivityUserName(), which
prefers getName() because the same params feed the e-mail notification text,
where "Lauri Ojansivu commented on ..." is what a person wants to read; a webhook
consumer needs the identifier, received "Lauri Ojansivu" where it needed "xet7",
and matching users by display name is wrong the moment two people share one.
Changing what `user` means would break every consumer already reading it, which
is what left this recorded as needing a maintainer decision. It needs none: the
username travels as its OWN field. `user` is untouched, `username` is beside it
in the default WEBHOOKS_ATTRIBUTES list, and a deployment that pinned its own
list still gets exactly the fields it asked for.

#6542 - "Import finished. Users imported: 60, but only 25 are listed". The Admin
Panel People pane paginates and passes its total to the shared table page
(totalLabelKey: 'people-number'), so the 60 the importer reported is on screen
next to the 25 rows of the current page. That is what the report was about, and
it is already true in this source.

#6500 - Docker `latest` not loading in a browser: already closed upstream, so it
was only sitting in the backlog.

The rest of the backlog stays, each with its reason: they need an SMTP server, an
LDAP directory, a container, a browser to drag in, or a maintainer's decision on
an intended contract - none of which a source reading can settle.

tests/webhookUsername.test.cjs pins both halves of the webhook change: that the
username is there and set from the same user document as the display name, and
that `user` still means what it always did, including the notification text that
depends on it. Three of its seven assertions fail on the previous commit.

Thanks to xet7 !

Fixes #3113,
Fixes #6542.
2026-08-08 02:50:15 +03:00

77 lines
3.6 KiB
JavaScript

'use strict';
// #3113: an outgoing webhook said who did something by DISPLAY name, and had no
// field for the login name.
//
// `params.user` comes from getActivityUserName(), which prefers `getName()` -
// the full name - because the same params feed the e-mail notification text,
// where "Lauri Ojansivu commented on ..." is what a person wants to read. A
// webhook consumer needs the identifier instead: it received "Lauri Ojansivu"
// where it needed "xet7", and matching users by display name is wrong the moment
// two people share one.
//
// Changing what `user` means would break every consumer already reading it, and
// that is what left this "needs a maintainer decision" for years. It needs no
// decision: the username travels as its OWN field. Nothing that reads `user`
// changes, and the missing identifier is simply there.
//
// Run: node tests/webhookUsername.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const ROOT = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
const activities = read('server/models/activities.js');
const outgoing = read('server/notifications/outgoing.js');
const code = src => src.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('webhookUsername:');
test('the payload carries the login name as its own field', () => {
const src = code(activities);
assert.ok(/params\.username = user\.username \|\| ''/.test(src),
'params.username is the username, and empty rather than undefined when a '
+ 'user somehow has none - a webhook consumer parsing JSON should not have '
+ 'to distinguish "absent" from "unknown"');
// Right where the display name is set, from the same user document.
const userAt = src.indexOf('params.user = getActivityUserName');
const nameAt = src.indexOf('params.username =');
assert.ok(userAt !== -1 && nameAt > userAt && nameAt - userAt < 400,
'both are set from the same user, together, or one drifts from the other');
});
test('and `user` still means what it always did', () => {
// The e-mail text is `${params.user} ${descriptionText}`, so this field is a
// display name by design. The fix must not have changed it.
const src = code(activities);
assert.ok(/params\.user = getActivityUserName\(user, activity\.userId\)/.test(src),
'the display name is untouched');
assert.ok(/user\.getName\?\.\(\) \|\| user\.username/.test(src),
'and getActivityUserName still prefers the full name for it');
assert.ok(/\$\{params\.user\} \$\{descriptionText\}/.test(code(outgoing)),
'which is what the notification text reads');
});
test('username is in the default webhook attribute list', () => {
const list = /webhooksAtbts = \([\s\S]*?\) \|\| \[([\s\S]*?)\];/.exec(code(outgoing));
assert.ok(list, 'the default attribute list must be there');
const fields = [...list[1].matchAll(/'([a-zA-Z]+)'/g)].map(m => m[1]);
assert.ok(fields.includes('username'), 'or the new field is computed and never sent');
assert.ok(fields.includes('user'), 'beside the display name, not instead of it');
});
test('WEBHOOKS_ATTRIBUTES still overrides the whole list', () => {
// A deployment that pinned its own field list keeps exactly what it asked for:
// the default only applies when the variable is unset.
assert.ok(/process\.env\.WEBHOOKS_ATTRIBUTES &&\s*process\.env\.WEBHOOKS_ATTRIBUTES\.split\(','\)/
.test(code(outgoing).replace(/\s+/g, ' ')),
'the env var replaces the list rather than adding to it');
});
console.log(`\n${passed} tests passed`);