Files
wekan/tests/boardViewMissingProfile6691.test.cjs
T
Lauri Ojansivu 326d9bbf61 Board View / Version Control, issue files and synced issue details.
Version Control shows the board as a GitHub or Gitea issue tracker in the
board's own theme: cards are issues (open, or closed when archived, in a
Closed or Done list, or closed upstream), the board's labels are labels,
labels with a due date and the importers' milestone: labels are
milestones, comments are the conversation and attachments are the files.
The issue list takes GitHub's search words (is:open, label:, milestone:,
author:, assignee:@me, no:label, sort:). New issue, Close and Reopen write
through the ordinary collections, so the History tab - the board's
history table - shows them and Restore undoes them. Import links open the
GitHub, GitLab, Gitea and Forgejo importers, and each synced list has a
Sync now button.

The GitHub, GitLab, Gitea and Forgejo importers bring each issue's files:
user-attachments and user-images links, GitLab uploads, and Gitea and
Forgejo assets, downloaded through the import's SSRF guard and stored as
card attachments in Admin Panel / Attachments / Default Storage.

List sync has a new option, Comments, labels, state and files, off by
default: comments come from one paged repository-wide list (GitHub,
Gitea) or each issue's notes (GitLab), keyed by the tracker's ids, so a
second run writes only what changed, and nothing written in WeKan is
changed. Gitea issues are fetched without pull requests, and a rate
limited answer is waited out once or ends the run with the time to retry.

A public board's Show on / Default on Public Board settings are for the
public: visitors and non-members get them, read-only, and the board's own
members get the private side, so any view - Version Control, Email, Chat
and the rest - can be the read-only public view.

The card menu has a QR code of the card's link, drawn in the browser by
WeKan's own encoder, which the tests check module for module against
qrcode-svg.

The new strings are translated in every locale.

Thanks to xet7 !

Fixes #2015.
2026-10-14 12:09:34 +03:00

145 lines
8.8 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const settings = require('../models/lib/boardViewSettings');
const source = fs.readFileSync('client/lib/utils.js', 'utf8');
const methods = source.slice(source.indexOf(' boardView() {'), source.indexOf(' // `swimlaneId` scopes'));
let user, browserView, pending = null, board = null;
const context = {
...settings,
// #6736: no instance feature is disabled here; tests/instanceFeatures.test.cjs
// covers boardView() with disabled views.
allowBoardView: () => true,
// boardView() passes the viewer, so a public board's visitors get its
// public views (boardViewAudience); here the member is the viewer.
Meteor: { userId: () => 'member' },
pendingBoardView: { get: () => pending, set: value => { pending = value; } },
ReactiveCache: { getCurrentUser: () => user },
// #6745: storedBoardView reads the user through currentUserWith (only the two
// board-view fields); here that is the same stub user.
currentUserWith: () => user,
window: { localStorage: { getItem: () => browserView,
setItem: () => { throw Error('Reading a view must not write storage'); } } },
};
vm.createContext(context);
vm.runInContext(`var Utils = {${methods} getCurrentBoard() { return currentBoard(); }, reload() { throw Error('Reading a view must not reload'); } };`,
Object.assign(context, { currentBoard: () => board }));
const Utils = context.Utils;
for (const profile of [undefined, {}, { boardView: undefined }, { boardView: '' }, { boardView: 'invalid' }]) {
user = { _id: 'member', profile };
for (const stored of [null, '', 'invalid', 'board-view-swimlanes', 'board-view-lists', 'board-view-cal']) {
browserView = stored;
assert.equal(Utils.storedBoardView(), settings.isKnownBoardView(stored) ? stored : settings.DEFAULT_BOARD_VIEW);
assert.equal(Utils.boardView(), Utils.storedBoardView());
}
}
user = { profile: { boardView: 'board-view-lists' } };
browserView = 'board-view-cal';
assert.equal(Utils.boardView(), 'board-view-lists', 'published preference wins');
user.boardViewPreference = 'board-view-swimlanes';
assert.equal(Utils.boardView(), 'board-view-swimlanes', 'private preference survives a competing partial profile');
user.boardViewPreference = 'board-view-lists';
// #4906 / #4256: a pending choice remembers the board it was made on (none here).
pending = { view: 'board-view-cal', boardId: null };
user.profile.boardView = 'board-view-cal';
assert.equal(Utils.boardView(), 'board-view-cal');
assert.deepEqual(pending, { view: 'board-view-cal', boardId: null }, 'stale private preference cannot acknowledge a pending choice');
user.boardViewPreference = 'board-view-cal';
assert.equal(Utils.boardView(), 'board-view-cal');
assert.equal(pending, null);
delete user.boardViewPreference;
pending = { view: 'board-view-swimlanes', boardId: null };
assert.equal(Utils.boardView(), 'board-view-swimlanes', 'pending choice wins until publication catches up');
user.profile.boardView = 'board-view-swimlanes';
assert.equal(Utils.boardView(), 'board-view-swimlanes');
assert.equal(pending, null);
user = { profile: {} }; browserView = null;
board = { permission: 'private', defaultPrivateBoardView: 'board-view-lists', boardViewSettings: { 'board-view-swimlanes': { showOnPrivate: false } } };
assert.equal(Utils.boardView(), 'board-view-lists', 'board default is honored');
board = null;
const listSource = fs.readFileSync('client/components/lists/listBody.js', 'utf8');
const scope = listSource.slice(listSource.indexOf(' this.idOrNull ='), listSource.indexOf(' this.addCard ='));
context.instance = { data: { board: () => ({ isTemplatesBoard: () => false }) } };
vm.runInContext(`(function() { ${scope} }).call(instance);`, context);
assert.equal(context.instance.idOrNull('lane-A'), 'lane-A', 'partial profile retains swimlane scope');
assert.equal(context.instance.idOrNull('lane-B'), 'lane-B');
browserView = 'board-view-lists';
assert.equal(context.instance.idOrNull('lane-A'), undefined, 'list view stays unscoped');
context.instance.data = null;
assert.equal(context.instance.idOrNull('lane-A'), undefined);
(async () => {
let publish, observer, selector, options, cleanup;
const messages = [];
let stops = 0;
const handle = { stop: () => { stops++; } };
vm.runInNewContext(fs.readFileSync('server/publications/userBoardView.js', 'utf8').replace(/^import .*;\n/m, ''), {
Meteor: { publish: (name, handler) => { assert.equal(name, 'userBoardView'); publish = handler; },
users: { find: (filter, projection) => {
selector = JSON.parse(JSON.stringify(filter)); options = JSON.parse(JSON.stringify(projection));
return { observeChangesAsync: async callbacks => {
observer = callbacks; callbacks.added(filter._id, { profile: { boardView: 'board-view-swimlanes' } });
return handle;
} };
} } },
});
assert.equal(await publish.call({ userId: null, ready: () => 'ready' }, 'victim'), 'ready');
for (const id of ['admin', 'impersonated-member']) {
messages.length = 0;
await publish.call({ userId: id, onStop: callback => { cleanup = callback; },
added: (collection, userId, fields) => messages.push(['added', collection, userId, JSON.parse(JSON.stringify(fields))]),
changed: (collection, userId, fields) => messages.push(['changed', collection, userId, fields.boardViewPreference]),
removed: (collection, userId) => messages.push(['removed', collection, userId]),
ready: () => messages.push(['ready']),
}, 'victim');
assert.deepEqual(selector, { _id: id }, 'client-supplied IDs cannot select another user');
assert.deepEqual(options, { fields: { 'profile.boardView': 1, 'profile.boardViews': 1 } });
// #4906 / #4256: the per-board choices travel beside the shared one.
assert.deepEqual(messages, [['added', 'users', id, { boardViewPreference: 'board-view-swimlanes', boardViewPreferences: {} }], ['ready']]);
observer.changed(id, { profile: { boardView: 'board-view-lists' } });
observer.changed(id, {});
observer.removed(id);
assert.deepEqual(messages.slice(2), [['changed', 'users', id, 'board-view-lists'], ['changed', 'users', id, undefined], ['removed', 'users', id]],
'preference changes, unsets and deleted users remain reactive');
cleanup();
const count = messages.length;
observer.added(id, { profile: { boardView: 'board-view-cal' } });
assert.equal(messages.length, count, 'stopped subscriptions send no preference');
}
assert.equal(stops, 2, 'every observer is stopped');
console.log('boardViewMissingProfile6691: private preference publication, reactivity and cleanup passed');
})().catch(error => { console.error(error); process.exitCode = 1; });
assert.match(fs.readFileSync('server/imports.js', 'utf8'), /import '\/server\/publications\/userBoardView'/);
assert.match(fs.readFileSync('client/00-startup.js', 'utf8'), /Meteor\.subscribe\('userBoardView'\)/);
console.log('boardViewMissingProfile6691: fallback, scoping, defaults and private publication passed');
(async () => {
const serverSource = fs.readFileSync('server/models/users.js', 'utf8');
const method = serverSource.slice(serverSource.indexOf(' async impersonate(userId) {'), serverSource.indexOf(' async isImpersonated(userId) {'));
let finishSwitch;
const switched = new Promise(resolve => { finishSwitch = resolve; });
let enteredSwitch;
const entered = new Promise(resolve => { enteredSwitch = resolve; });
const calls = [];
const methods = vm.runInNewContext(`({${method}})`, {
check: () => {}, Match: { Any: {}, test: value => typeof value === 'string' },
Meteor: { Error: class extends Error { constructor(code, message) { super(message); this.error = code; } } },
ReactiveCache: { getUser: async () => ({ _id: 'member' }), getCurrentUser: async () => ({ _id: 'admin', isAdmin: true }) },
ImpersonatedUsers: { insertAsync: async () => { calls.push('record'); } },
});
let returned = false;
const result = methods.impersonate.call({ setUserId: id => { calls.push(id); enteredSwitch(); return switched; } }, 'member').then(() => { returned = true; });
// Let the real async method reach setUserId, but leave its promise pending.
await entered;
assert.deepEqual(calls, ['record', 'member']);
assert.equal(returned, false, 'method must not return before subscriptions finish switching');
finishSwitch();
await result;
assert.equal(returned, true);
await assert.rejects(methods.impersonate.call({}, ''), /user id is required/);
await assert.rejects(methods.impersonate.call({ setUserId: async () => { throw Error('switch failed'); } }, 'member'), /switch failed/,
'subscription-switch failures propagate to the client');
console.log('boardViewMissingProfile6691: impersonation awaits the Meteor 3 subscription transition');
})().catch(error => { console.error(error); process.exitCode = 1; });