Files
wekan/server/lib/tests/utils.tests.js
T
Lauri Ojansivu 2790fe5092 Fix the 19 tests my header and menu restructuring broke.
The newest run (../log/2026-08-01_03-14-39) is the first since this work: 18
Playwright failures in every one of the three browsers, and one mocha failure.
The run before it had none. All of them are mine, and all of them are tests
describing markup I moved rather than behaviour that broke.

.js-toggle-sidebar was eight of them. The hamburger moved to the first header
bar and became .js-toggle-page-sidebar - one control that opens whichever
sidebar the page has. The page object and the one spec that named it directly
now name the new one. Its handlers in boardHeader.js and sidebar.js are DELETED
rather than left: markup for them no longer exists anywhere, so they were dead
code that reads as a working feature, and the injected CSS rule in boardBody.js
is repointed at the class that exists.

#header-main-bar was two: the RTL layout test used the second header bar as its
"the page has visible text" anchor, and the three list pages it checks no longer
render one. It anchors on the first bar's page title instead, which is where
those titles went.

The rules controls were three: they are a view of the shared page sidebar now,
so the specs open it first. The All Boards search field was one, for the same
reason. The card's copy-link button was two - it is a row of the card's actions
menu, so the page object opens the menu, and the test asserts what reached the
CLIPBOARD rather than reading an href that no longer exists.

That clipboard is RECORDED, not read back: reading it needs a clipboard-read
permission only Chromium can be granted, so a test that read it would pass on
one browser of three. WeKan copies through navigator.clipboard.writeText, so
wrapping that captures the real value everywhere - and it is a stronger check
than the old test, which accepted "no href, so click it and see that nothing
threw" and would have passed even if the button copied nothing.

Two were addresses: /templates now redirects to /allboards/templates, and the
admin spec navigated to /admin-reports and then clicked a menu row. Every Admin
Panel pane has an address of its own, so it goes straight to
/admin/problems/files - no redirect to race with the menu rendering.

The mocha failure is from the board-roles capability table:
allowIsBoardMemberCommentOnly asks that table now, and the table reads
board.members, while the test's stub offered only the old hasMember/hasReadOnly
methods - so it answered false for everyone. The stub is a members array now,
and a second case pins what the old one could not express: it is the ROLE that
decides, not membership, so isNoComments, isReadOnly and an inactive member are
each false. Every expectation was checked against the real function before
being written down.

Not verified by running: Playwright needs the app server and browsers, which
this sandbox does not have. Every edited file parses, and the 292 node suites
pass.

Thanks to xet7 !
2026-08-01 10:56:37 +03:00

138 lines
4.8 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,
} 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);
});
});
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);
});
});
});