Files
wekan/server/lib/tests/utils.tests.js
T
Lauri Ojansivu 4d9282359e Let a global Admin Panel admin edit/delete any board, not only their own.
A board's own admin/member could leave the organization (or be removed)
and leave nobody - not even a global site admin - able to edit its
settings, add members to it, or archive/delete it: Boards.allow's
update/remove rule only checked board.hasAdmin(userId) against the
board's own member list. inviteUserToBoard already had a bypass for
Meteor.user().isAdmin; the whole-board update/remove rule did not.

server/lib/utils.js gets isBoardAdminOrSiteAdmin (pure, unit-tested) and
allowIsBoardAdminOrSiteAdmin (the async Meteor.users-aware wrapper), and
server/permissions/boards.js's update/remove rules now use it instead of
the board-only allowIsBoardAdmin. Scoped to the whole-board rule only -
the narrower rules/actions/triggers/comments allow rules keep the
board-only check, pinned by a negative test.

Thanks to relikd and xet7 !
2026-09-10 20:04:07 +03:00

159 lines
5.7 KiB
JavaScript

/* eslint-env mocha */
import { Random } from 'meteor/random';
import { expect } from 'chai';
import sinon from 'sinon';
import Boards from '/models/boards';
import {
allowIsBoardAdmin,
allowIsBoardMember,
allowIsAnyBoardMember,
allowIsBoardMemberCommentOnly,
allowIsBoardMemberNoComments,
allowIsBoardMemberByCard,
isBoardAdminOrSiteAdmin,
} from '../utils';
describe('utils', function() {
afterEach(function() {
sinon.restore();
});
describe(allowIsBoardAdmin.name, function() {
it('returns if a board has an admin', function() {
const userId = Random.id();
const board = {
hasAdmin: id => {
return id === userId;
}
};
expect(allowIsBoardAdmin(userId, board)).to.equal(true);
expect(allowIsBoardAdmin(Random.id(), board)).to.equal(false);
});
});
// Issue #3249: a global site admin must be able to manage a board they are
// not themselves a member/admin of (e.g. its own admin left the org).
describe(isBoardAdminOrSiteAdmin.name, function() {
it('allows the board\'s own admin', function() {
const userId = Random.id();
const board = { hasAdmin: id => id === userId };
expect(isBoardAdminOrSiteAdmin(userId, board, false)).to.equal(true);
});
it('allows a global site admin who is not a board member/admin', function() {
const board = { hasAdmin: () => false };
expect(isBoardAdminOrSiteAdmin(Random.id(), board, true)).to.equal(true);
});
it('denies a non-admin, non-member, non-site-admin caller', function() {
const board = { hasAdmin: () => false };
expect(isBoardAdminOrSiteAdmin(Random.id(), board, false)).to.equal(false);
});
});
describe(allowIsBoardMember.name, function() {
it('returns if a board has a member', function() {
const userId = Random.id();
const board = {
hasMember: id => {
return id === userId;
}
};
expect(allowIsBoardMember(userId, board)).to.equal(true);
expect(allowIsBoardMember(Random.id(), board)).to.equal(false);
});
});
describe(allowIsAnyBoardMember.name, function() {
it('returns if any board has a member', function() {
const userId = Random.id();
const boardsExpectedTrue = [{
hasMember: id => {
return id === userId;
}
}];
expect(allowIsAnyBoardMember(userId, boardsExpectedTrue)).to.equal(true);
expect(allowIsAnyBoardMember(Random.id(), boardsExpectedTrue)).to.equal(false);
const boardsExpectedFalse = [{
hasMember: () => false
}];
expect(allowIsAnyBoardMember(userId, boardsExpectedFalse)).to.equal(false);
expect(allowIsAnyBoardMember(Random.id(), boardsExpectedFalse)).to.equal(false);
});
});
describe(allowIsBoardMemberCommentOnly.name, function() {
it('returns if a board has a member that can post comments', function() {
// The stub is a board's MEMBERS now, not a set of hasX() methods. This
// function used to ask the board `hasMember` / `hasReadOnly` /
// `hasNoComments` and spell the rule out as a list of flags; it asks the
// one capability table instead (models/lib/boardRoleCapabilities.js), and
// that table reads `board.members`. A stub with the old methods and no
// `members` array answered false for everyone, because there was no
// member to find - the test was describing an implementation that is gone
// rather than a behaviour that changed.
//
// `isActive: true` is required by memberRoleOf: an inactive member has no
// role at all, which is what makes a removed member unable to comment.
const userId = Random.id();
const board = {
members: [{ userId, isActive: true }],
};
expect(allowIsBoardMemberCommentOnly(userId, board)).to.equal(true);
expect(allowIsBoardMemberCommentOnly(Random.id(), board)).to.equal(false);
});
it('and a member who may not comment cannot', function() {
// The point of the function, and what the old stub could not express:
// it is the ROLE that decides, not membership.
const userId = Random.id();
const noComments = { members: [{ userId, isActive: true, isNoComments: true }] };
expect(allowIsBoardMemberCommentOnly(userId, noComments)).to.equal(false);
const readOnly = { members: [{ userId, isActive: true, isReadOnly: true }] };
expect(allowIsBoardMemberCommentOnly(userId, readOnly)).to.equal(false);
// ...and an INACTIVE member of any role: a removed member is not a member.
const inactive = { members: [{ userId, isActive: false }] };
expect(allowIsBoardMemberCommentOnly(userId, inactive)).to.equal(false);
});
});
describe(allowIsBoardMemberNoComments.name, function() {
it('returns if a board has a member that has comment any comments', function() {
const userId = Random.id();
const board = {
hasMember: id => {
return id === userId;
},
hasNoComments: id => {
return id !== userId;
}
};
expect(allowIsBoardMemberNoComments(userId, board)).to.equal(true);
expect(allowIsBoardMemberNoComments(Random.id(), board)).to.equal(false);
});
});
describe(allowIsBoardMemberByCard.name, function() {
it('returns if the board for a given card has a member', async function() {
const userId = Random.id();
const board = {
hasMember: id => id === userId,
};
const card = { boardId: 'board1' };
sinon.stub(Boards, 'findOneAsync').resolves(board);
expect(await allowIsBoardMemberByCard(userId, card)).to.equal(true);
expect(await allowIsBoardMemberByCard(Random.id(), card)).to.equal(false);
});
});
});