Files
wekan/tests/rulesReadOnly.test.cjs
T
Lauri Ojansivu f4a3690256 Rules can be shown to board members, read-only, when the Admin Panel allows it.
Admin Panel / Settings / Visibility / Rules gets "Boards may show their
Rules to members, read-only", off by default (Settings
rulesReadOnlyForMembers, site admin only). With it on, a board admin ticks
"Show these Rules to board members, read-only" on the board's Rules page
(rulesVisibleToMembers). Members of that board then get Rules in Board
Settings and see the rules list and each rule's details, with no control
that changes a rule; the methods that change rules keep their board-admin
checks, so nothing is granted. Without both ticks only board and site
admins see the Rules page, which until now drew its list for any member
who opened the address.

models/lib/rulesReadOnly.js decides 'edit', 'read' or 'none', and
client/lib/rulesAccess.js is the one place the Rules page, Board Settings
and rule details ask. tests/rulesReadOnly.test.cjs covers both switches,
each alone, a non-member, and that a reader's page has no edit control.
boardMenuOrder now leaves the reader's alternative branch out of the
admin's sequence and pins it alone; instanceFeatures counts the fourth
site-admin method; roleHelperRegressions follows the canAdmin check to
rulesAccess.js. The new strings are English placeholders until the next
translation pass.

Thanks to justinr1234 and xet7 !

Fixes #2217.
2026-10-14 14:23:31 +03:00

63 lines
4.0 KiB
JavaScript

'use strict';
// #2217: an Admin Panel option, off by default, lets a board show its Rules to
// members read-only; the board admin ticks it per board.
// Run: node tests/rulesReadOnly.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { rulesAccess } = require('../models/lib/rulesReadOnly.js');
const ROOT = path.join(__dirname, '..');
const read = f => fs.readFileSync(path.join(ROOT, f), 'utf8');
const on = { rulesReadOnlyForMembers: true };
const shown = { rulesVisibleToMembers: true };
// Positive.
assert.equal(rulesAccess({ setting: on, board: shown, isActiveMember: true }), 'read');
assert.equal(rulesAccess({ setting: {}, board: {}, isBoardAdmin: true }), 'edit', 'board admins edit as before');
assert.equal(rulesAccess({ setting: {}, board: {}, isSiteAdmin: true }), 'edit');
assert.equal(rulesAccess({ setting: on, board: shown, isBoardAdmin: true, isActiveMember: true }), 'edit');
console.log(' ok - both switches on: members read; admins edit');
// Negative: off by default, and each switch alone is not enough.
for (const [setting, board] of [[undefined, undefined], [{}, shown], [on, {}], [{ rulesReadOnlyForMembers: 'true' }, shown],
[on, { rulesVisibleToMembers: 1 }]]) {
assert.equal(rulesAccess({ setting, board, isActiveMember: true }), 'none');
}
assert.equal(rulesAccess({ setting: on, board: shown, isActiveMember: false }), 'none', 'never for a non-member');
console.log(' ok - off by default; one switch alone, or a non-member, sees nothing');
// The setting: site admin only, published, and the board field.
const methods = read('server/methods/instanceFeatures.js');
assert.match(methods, /async saveRulesReadOnlyForMembers\(enabled\) \{\s*await requireSiteAdmin\(\);\s*check\(enabled, Boolean\);/);
assert.match(read('server/publications/settings.js'), /rulesReadOnlyForMembers: 1,/);
assert.match(read('models/settings.js'), /rulesReadOnlyForMembers: \{\s*type: Boolean,\s*optional: true,/);
assert.match(read('models/boards.js'), /rulesVisibleToMembers: \{[\s\S]{0,300}type: Boolean,\s*optional: true,/);
console.log(' ok - the setting is site-admin only and the board field exists');
// UI: one access function everywhere; nothing that edits for a reader.
const main = read('client/components/rules/rulesMain.jade');
const readBranch = main.slice(main.indexOf("else if($eq rulesAccess 'read')"), main.indexOf(' +sidebar'));
assert.ok(readBranch.length > 0);
assert.doesNotMatch(readBranch, /rulesTriggers|rulesActions|rulesWorkflow|rulesBlocks|rulesBots|historyTable/, 'a reader gets the list and details only');
assert.match(main, /if\(\$eq rulesAccess 'edit'\)\s*\n\s*if\(\$eq rulesCurrentTab\.get 'rulesList'\)/);
assert.match(read('client/components/rules/rulesList.js'), /canAdmin\(\) \{[\s\S]{0,300}return currentRulesAccess\(\) === 'edit';/);
const details = read('client/components/rules/ruleDetails.jade');
for (const m of details.matchAll(/button\.js-(add|remove)-rule-part/g)) {
const before = details.slice(0, m.index).split('\n').slice(-2).join('\n');
assert.match(before, /if canEditRules/, 'every add/remove button is behind canEditRules');
}
const menu = read('client/components/sidebar/sidebar.jade');
const popup = menu.slice(menu.indexOf('template(name="boardMenuPopup")'));
assert.match(popup, /\n if currentUser\.isBoardAdmin\n[\s\S]*?\n else if rulesReadable\n[\s\S]*?a\.js-open-rules-view[\s\S]*?\n if currentUser\n/,
'the member entry is the else of the WHOLE admin block, so no admin entry leaks to it');
const readable = popup.slice(popup.indexOf(' else if rulesReadable'), popup.indexOf('\n if currentUser\n'));
assert.equal((readable.match(/a\.js-/g) || []).length, 1, 'a reader gets exactly one entry: Rules');
assert.match(read('client/components/rules/rulesList.jade'), /if canAdmin\s*\n\s*if rulesForMembersOffered\s*\n\s*a\.flex\.js-toggle-rules-visible-to-members/);
console.log(' ok - readers get the rules list and details with no edit control');
console.log('\nrulesReadOnly: all tests passed');