[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent broadcasting of system messages when hide setting is enabled #32522

Merged
Prev Previous commit
Next Next commit
fix: fix unit test by isolating the stubs
  • Loading branch information
AllanPazRibeiro committed May 29, 2024
commit 38800b14f91259f63aa0681197268131d2be6c87
201 changes: 105 additions & 96 deletions apps/meteor/tests/unit/server/modules/watchers/lib/messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,126 +2,135 @@ import { expect } from 'chai';
import proxyquire from 'proxyquire';
import sinon from 'sinon';

const getValueByIdStub = sinon.stub();
const usersFindOneStub = sinon.stub();
const messagesFindOneStub = sinon.stub();
const broadcastStub = sinon.stub();

const modelsStubs = {
Messages: {
findOneById: messagesFindOneStub,
},
Users: {
findOne: usersFindOneStub,
},
Settings: {
getValueById: getValueByIdStub,
},
};

const coreStubs = (dbWatchersDisabled: boolean) => ({
api: {
broadcast: broadcastStub,
},
dbWatchersDisabled,
});

const memStubs = (value: (data: string) => any) => (data: string) => value(data);

const sampleMessage = {
_id: '123',
rid: 'room1',
msg: 'Hello',
ts: new Date(),
u: { _id: 'user1', username: 'user1', name: 'Real User' },
mentions: [],
t: 'user-muted',
_updatedAt: new Date(),
};

describe('getMessageToBroadcast', () => {
let originalEnv: NodeJS.ProcessEnv;
describe('Message Broadcast Tests', () => {
let getValueByIdStub: sinon.SinonStub;
let usersFindOneStub: sinon.SinonStub;
let messagesFindOneStub: sinon.SinonStub;
let broadcastStub: sinon.SinonStub;
let getMessageToBroadcast: any;
let broadcastMessageFromData: any;

const sampleMessage = {
_id: '123',
rid: 'room1',
msg: 'Hello',
ts: new Date(),
u: { _id: 'user1', username: 'user1', name: 'Real User' },
mentions: [],
t: 'user-muted',
_updatedAt: new Date(),
};

const modelsStubs = () => ({
Messages: {
findOneById: messagesFindOneStub,
},
Users: {
findOne: usersFindOneStub,
},
Settings: {
getValueById: getValueByIdStub,
},
});

const coreStubs = (dbWatchersDisabled: boolean) => ({
api: {
broadcast: broadcastStub,
},
dbWatchersDisabled,
});

const memStubs = (value: (data: string) => any) => (data: string) => value(data);

beforeEach(() => {
originalEnv = { ...process.env };
getValueByIdStub = sinon.stub();
usersFindOneStub = sinon.stub();
messagesFindOneStub = sinon.stub();
broadcastStub = sinon.stub();

const proxyMock = proxyquire.noCallThru().load('../../../../../../server/modules/watchers/lib/messages', {
'@rocket.chat/models': modelsStubs,
'@rocket.chat/models': modelsStubs(),
'@rocket.chat/core-services': coreStubs(false),
'mem': memStubs,
});

getMessageToBroadcast = proxyMock.getMessageToBroadcast;
broadcastMessageFromData = proxyMock.broadcastMessageFromData;
});

afterEach(() => {
process.env = originalEnv;
sinon.reset();
});

it('should return undefined if message is hidden or imported', async () => {
messagesFindOneStub.resolves({ ...sampleMessage, _hidden: true });

const result = await getMessageToBroadcast({ id: '123' });

expect(result).to.be.undefined;
});

it('should hide message if type is in hideSystemMessage settings', async () => {
messagesFindOneStub.resolves(sampleMessage);
getValueByIdStub.withArgs('Hide_System_Messages').resolves(['user-muted', 'mute_unmute']);

const result = await getMessageToBroadcast({ id: '123' });
describe('getMessageToBroadcast', () => {
it('should return undefined if message is hidden or imported', async () => {
messagesFindOneStub.resolves({ ...sampleMessage, _hidden: true });

expect(result).to.be.undefined;
});
const result = await getMessageToBroadcast({ id: '123' });

it('should return the message with real name if useRealName is true', async () => {
getValueByIdStub.withArgs('Hide_System_Messages').resolves([]);
getValueByIdStub.withArgs('UI_Use_Real_Name').resolves(true);
messagesFindOneStub.resolves(sampleMessage);
usersFindOneStub.resolves({ name: 'Real User' });
expect(result).to.be.undefined;
});

const result = await getMessageToBroadcast({ id: '123' });
it('should hide message if type is in hideSystemMessage settings', async () => {
messagesFindOneStub.resolves(sampleMessage);
getValueByIdStub.withArgs('Hide_System_Messages').resolves(['user-muted', 'mute_unmute']);

expect(result).to.have.property('u');
expect(result?.u).to.have.property('name', 'Real User');
});
});
const result = await getMessageToBroadcast({ id: '123' });

describe('broadcastMessageFromData', () => {
let broadcastMessageFromData: any;

const setupProxyMock = (dbWatchersDisabled: boolean) => {
const proxyMock = proxyquire.noCallThru().load('../../../../../../server/modules/watchers/lib/messages', {
'@rocket.chat/models': modelsStubs,
'@rocket.chat/core-services': coreStubs(dbWatchersDisabled),
'mem': memStubs,
expect(result).to.be.undefined;
});
broadcastMessageFromData = proxyMock.broadcastMessageFromData;
};

afterEach(() => {
sinon.reset();
});

it('should broadcast the message if dbWatchersDisabled is true', async () => {
setupProxyMock(true);
messagesFindOneStub.resolves(sampleMessage);
getValueByIdStub.resolves([]);
it('should return the message with real name if useRealName is true', async () => {
getValueByIdStub.withArgs('Hide_System_Messages').resolves([]);
getValueByIdStub.withArgs('UI_Use_Real_Name').resolves(true);
messagesFindOneStub.resolves(sampleMessage);
usersFindOneStub.resolves({ name: 'Real User' });

await broadcastMessageFromData({ id: '123', data: sampleMessage });
const result = await getMessageToBroadcast({ id: '123' });

expect(broadcastStub.calledOnce).to.be.true;
expect(broadcastStub.calledOnceWith('watch.messages', { message: sampleMessage })).to.be.true;
expect(result).to.have.property('u');
expect(result?.u).to.have.property('name', 'Real User');
});
});

it('should not broadcast the message if dbWatchersDisabled is false', async () => {
setupProxyMock(false);
messagesFindOneStub.resolves(sampleMessage);
getValueByIdStub.resolves([]);

await broadcastMessageFromData({ id: '123', data: sampleMessage });

expect(broadcastStub.called).to.be.false;
describe('broadcastMessageFromData', () => {
const setupProxyMock = (dbWatchersDisabled: boolean) => {
const proxyMock = proxyquire.noCallThru().load('../../../../../../server/modules/watchers/lib/messages', {
'@rocket.chat/models': modelsStubs(),
'@rocket.chat/core-services': coreStubs(dbWatchersDisabled),
'mem': memStubs,
});
broadcastMessageFromData = proxyMock.broadcastMessageFromData;
};

const testCases = [
{
description: 'should broadcast the message if dbWatchersDisabled is true',
dbWatchersDisabled: true,
expectBroadcast: true,
},
{
description: 'should not broadcast the message if dbWatchersDisabled is false',
dbWatchersDisabled: false,
expectBroadcast: false,
},
];

testCases.forEach(({ description, dbWatchersDisabled, expectBroadcast }) => {
it(description, async () => {
setupProxyMock(dbWatchersDisabled);
messagesFindOneStub.resolves(sampleMessage);
getValueByIdStub.resolves([]);

await broadcastMessageFromData({ id: '123', data: sampleMessage });

if (expectBroadcast) {
expect(broadcastStub.calledOnce).to.be.true;
expect(broadcastStub.calledOnceWith('watch.messages', { message: sampleMessage })).to.be.true;
} else {
expect(broadcastStub.called).to.be.false;
}
});
});
});
});
Loading