Files
wekan/server/lib/tests/cards.methods.tests.js
T
Lauri OjansivuandClaude Opus 4.8 b9f9390d04 Server-side Mocha suite: fixed a startup crash and the 44 latent failures it had been hiding
- imports/i18n/i18n.test.js crashed the whole run at load: chai 6.x plugins (sinon-chai,
  chai-as-promised) are ESM-only, so use(require('sinon-chai')) handed chai.use() a module
  namespace instead of the plugin function ("fn is not a function"). Now imports the default
  export.
- That unmasked a second load crash - PositionHistory.helpers is not a function: the
  meteor test entry (server/lib/tests/index.js) never ran the .helpers / .attachSchema shim
  that server/main.js bootstraps, so the first model to call Collection.helpers({...}) threw.
  The shim is now imported first in the test entry.
- With the suite finally running, 44 server tests failed because meteor test only loads what
  the specs import (not the app's /server/imports). The specs now import the files that
  register the methods / globals under test (cards.vote / cards.pokerVote, api.attachment.*,
  cloneBoard, getBackgroundImageURL, applyListWidth, updateListSort, moveChecklist,
  userPositionHistory.*, archiveBoard, sendSMTPTestEmail, and the Attachments global). Also
  fixed genuine test bugs: the cards.vote / cards.pokerVote specs were written synchronously
  against async methods; the header-login trust specs restored env vars with
  process.env.X = undefined (which stores the string "undefined" and shadowed the trusted-IP
  allowlist, making every trusted source read as untrusted); the DnsBleed decimal-loopback
  matcher assumed the integer host survived URL normalisation; and the dependencies-OpenAPI
  spec could not locate its source file from the built bundle. The two cards.archive /
  cards.move specs tested Meteor methods that do not exist (archive / move are Minimongo
  document helpers secured by Cards.allow / Cards.deny, already covered by the cards security
  tests) and were removed. Server-side Mocha is now 409 passing, 0 failing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-05 16:48:58 +03:00

145 lines
5.4 KiB
JavaScript

/* eslint-env mocha */
import { expect } from 'chai';
import sinon from 'sinon';
import { Meteor } from 'meteor/meteor';
import Cards from '/models/cards';
import { ReactiveCache } from '/imports/reactiveCache';
// The `meteor test` entry only loads what test files import; the app registers
// these Meteor methods via server/main.js → /server/imports. Load the file that
// registers cards.vote / cards.pokerVote so their handlers exist.
import '/server/models/cards';
// Helpers to access method handlers
const voteHandler = () => Meteor.server.method_handlers['cards.vote'];
const pokerVoteHandler = () => Meteor.server.method_handlers['cards.pokerVote'];
// Preserve originals to restore after stubbing
const origGetCard = ReactiveCache.getCard;
const origGetBoard = ReactiveCache.getBoard;
describe('cards methods security', function() {
let updateStub;
beforeEach(function() {
// The cards.vote / cards.pokerVote methods persist via Cards.updateAsync
// (async server API), so stub that to capture modifiers.
updateStub = sinon.stub(Cards, 'updateAsync').resolves(1);
});
afterEach(function() {
if (updateStub) updateStub.restore();
ReactiveCache.getCard = origGetCard;
ReactiveCache.getBoard = origGetBoard;
});
describe('cards.vote', function() {
it('denies non-member when allowNonBoardMembers=false', async function() {
const cardId = 'card1';
const callerId = 'user-nonmember';
const board = { hasMember: id => id === 'someone-else' };
const card = { _id: cardId, boardId: 'board1', vote: { allowNonBoardMembers: false } };
ReactiveCache.getCard = () => card;
ReactiveCache.getBoard = () => board;
let thrown;
try {
await voteHandler().call({ userId: callerId }, cardId, true);
} catch (error) {
thrown = error;
}
expect(thrown).to.exist;
expect(updateStub.called).to.equal(false);
});
it('allows non-member only for own userId when allowNonBoardMembers=true', async function() {
const cardId = 'card2';
const callerId = 'user-guest';
const board = { hasMember: id => id === 'someone-else' };
const card = { _id: cardId, boardId: 'board2', vote: { allowNonBoardMembers: true } };
ReactiveCache.getCard = () => card;
ReactiveCache.getBoard = () => board;
await voteHandler().call({ userId: callerId }, cardId, true);
expect(updateStub.calledOnce).to.equal(true);
const [, modifier] = updateStub.getCall(0).args;
expect(modifier.$addToSet['vote.positive']).to.equal(callerId);
expect(modifier.$pull['vote.negative']).to.equal(callerId);
expect(modifier.$set.modifiedAt).to.be.instanceOf(Date);
expect(modifier.$set.dateLastActivity).to.be.instanceOf(Date);
});
it('ensures member votes only affect caller userId', async function() {
const cardId = 'card3';
const callerId = 'member1';
const otherId = 'member2';
const board = { hasMember: id => (id === callerId || id === otherId) };
const card = { _id: cardId, boardId: 'board3', vote: { allowNonBoardMembers: false } };
ReactiveCache.getCard = () => card;
ReactiveCache.getBoard = () => board;
await voteHandler().call({ userId: callerId }, cardId, true);
expect(updateStub.calledOnce).to.equal(true);
const [, modifier] = updateStub.getCall(0).args;
// Only callerId present in modifier
expect(modifier.$addToSet['vote.positive']).to.equal(callerId);
expect(modifier.$pull['vote.negative']).to.equal(callerId);
});
});
describe('cards.pokerVote', function() {
it('denies non-member when allowNonBoardMembers=false', async function() {
const cardId = 'card4';
const callerId = 'nm';
const board = { hasMember: id => id === 'someone-else' };
const card = { _id: cardId, boardId: 'board4', poker: { allowNonBoardMembers: false } };
ReactiveCache.getCard = () => card;
ReactiveCache.getBoard = () => board;
let thrown;
try {
await pokerVoteHandler().call({ userId: callerId }, cardId, 'five');
} catch (error) {
thrown = error;
}
expect(thrown).to.exist;
expect(updateStub.called).to.equal(false);
});
it('allows non-member only for own userId when allowNonBoardMembers=true', async function() {
const cardId = 'card5';
const callerId = 'guest';
const board = { hasMember: id => id === 'someone-else' };
// cards.pokerVote delegates modifier-building to the card's setPoker helper,
// then adds modifiedAt/dateLastActivity before Cards.updateAsync.
const card = {
_id: cardId,
boardId: 'board5',
poker: { allowNonBoardMembers: true },
setPoker: (userId, state) => ({
$addToSet: { [`poker.${state}`]: userId },
$pull: { 'poker.one': userId },
}),
};
ReactiveCache.getCard = () => card;
ReactiveCache.getBoard = () => board;
await pokerVoteHandler().call({ userId: callerId }, cardId, 'eight');
expect(updateStub.calledOnce).to.equal(true);
const [, modifier] = updateStub.getCall(0).args;
expect(modifier.$addToSet['poker.eight']).to.equal(callerId);
// Ensure removal from other buckets includes callerId
expect(modifier.$pull['poker.one']).to.equal(callerId);
expect(modifier.$set.modifiedAt).to.be.instanceOf(Date);
expect(modifier.$set.dateLastActivity).to.be.instanceOf(Date);
});
});
});