[MM-50995] Harden Calls events handling (#2571)

* Simplify server switching logic in calls events

* Harden calls events handling

* Fix subpath
This commit is contained in:
Claudio Costa
2023-03-01 16:01:39 -06:00
committed by GitHub
parent 7d109b9084
commit cd051fe679
6 changed files with 285 additions and 62 deletions

View File

@@ -30,13 +30,19 @@ jest.mock('../views/webContentEvents', () => ({
generateNewWindowListener: jest.fn(),
}));
jest.mock('common/utils/url', () => {
const originalModule = jest.requireActual('common/utils/url');
return {
...originalModule,
...originalModule.default,
};
});
describe('main/windows/callsWidgetWindow', () => {
describe('create CallsWidgetWindow', () => {
const widgetConfig = {
callID: 'test-call-id',
siteURL: 'http://localhost:8065',
title: '',
serverName: 'test-server-name',
channelURL: '/team/channel_id',
};
@@ -48,8 +54,18 @@ describe('main/windows/callsWidgetWindow', () => {
view: {
webContents: {
send: jest.fn(),
id: 'mainViewID',
},
},
serverInfo: {
server: {
name: 'test-server-name',
url: new URL('http://localhost:8065'),
},
},
getWebContents: () => ({
id: 'mainViewID',
}),
};
const baseWindow = new EventEmitter();
@@ -170,6 +186,11 @@ describe('main/windows/callsWidgetWindow', () => {
});
it('resize', () => {
baseWindow.webContents = {
...baseWindow.webContents,
id: 'windowID',
};
baseWindow.show = jest.fn(() => {
baseWindow.emit('show');
});
@@ -202,7 +223,24 @@ describe('main/windows/callsWidgetWindow', () => {
height: MINIMUM_CALLS_WIDGET_HEIGHT,
});
widgetWindow.onResize(null, {
widgetWindow.onResize({
sender: {
id: 'badID',
},
},
'widget',
{
element: 'calls-widget',
width: 300,
height: 100,
});
expect(baseWindow.webContents.getZoomFactor).not.toHaveBeenCalled();
widgetWindow.onResize({
sender: baseWindow.webContents,
},
'widget',
{
element: 'calls-widget',
width: 300,
height: 100,
@@ -217,6 +255,11 @@ describe('main/windows/callsWidgetWindow', () => {
});
it('zoom', () => {
baseWindow.webContents = {
...baseWindow.webContents,
id: 'windowID',
};
baseWindow.show = jest.fn(() => {
baseWindow.emit('show');
});
@@ -244,7 +287,9 @@ describe('main/windows/callsWidgetWindow', () => {
expect(baseWindow.webContents.getZoomFactor).toHaveBeenCalledTimes(0);
baseWindow.webContents.getZoomFactor = jest.fn(() => 2.0);
widgetWindow.onResize(null, {
widgetWindow.onResize({
sender: baseWindow.webContents,
}, 'widget', {
element: 'calls-widget',
width: 300,
height: 100,
@@ -258,7 +303,10 @@ describe('main/windows/callsWidgetWindow', () => {
});
baseWindow.webContents.getZoomFactor = jest.fn(() => 0.5);
widgetWindow.onResize(null, {
widgetWindow.onResize({
sender: baseWindow.webContents,
}, 'widget', {
element: 'calls-widget',
width: 300,
height: 100,
@@ -290,11 +338,29 @@ describe('main/windows/callsWidgetWindow', () => {
it('getWidgetURL', () => {
const config = {
...widgetConfig,
siteURL: 'http://localhost:8065/subpath',
title: 'call test title #/&',
};
const widgetWindow = new CallsWidgetWindow(mainWindow, mainView, config);
const expected = `${config.siteURL}/plugins/${CALLS_PLUGIN_ID}/standalone/widget.html?call_id=${config.callID}&title=call+test+title+%23%2F%26`;
const expected = `${mainView.serverInfo.server.url}plugins/${CALLS_PLUGIN_ID}/standalone/widget.html?call_id=${config.callID}&title=call+test+title+%23%2F%26`;
expect(widgetWindow.getWidgetURL()).toBe(expected);
});
it('getWidgetURL - under subpath', () => {
const config = {
...widgetConfig,
title: 'call test title #/&',
};
const view = {
serverInfo: {
server: {
url: new URL('http://localhost:8065/subpath'),
},
},
};
const widgetWindow = new CallsWidgetWindow(mainWindow, view, config);
const expected = `${view.serverInfo.server.url}/plugins/${CALLS_PLUGIN_ID}/standalone/widget.html?call_id=${config.callID}&title=call+test+title+%23%2F%26`;
expect(widgetWindow.getWidgetURL()).toBe(expected);
});
@@ -302,6 +368,7 @@ describe('main/windows/callsWidgetWindow', () => {
baseWindow.webContents = {
...baseWindow.webContents,
send: jest.fn(),
id: 'windowID',
};
const widgetWindow = new CallsWidgetWindow(mainWindow, mainView, widgetConfig);
@@ -309,7 +376,15 @@ describe('main/windows/callsWidgetWindow', () => {
sourceID: 'test-source-id',
withAudio: false,
};
widgetWindow.onShareScreen(null, '', message);
widgetWindow.onShareScreen({
sender: {id: 'badID'},
}, '', message);
expect(widgetWindow.win.webContents.send).not.toHaveBeenCalled();
widgetWindow.onShareScreen({
sender: baseWindow.webContents,
}, '', message);
expect(widgetWindow.win.webContents.send).toHaveBeenCalledWith(CALLS_WIDGET_SHARE_SCREEN, message);
});
@@ -317,13 +392,22 @@ describe('main/windows/callsWidgetWindow', () => {
baseWindow.webContents = {
...baseWindow.webContents,
send: jest.fn(),
id: 'windowID',
};
const widgetWindow = new CallsWidgetWindow(mainWindow, mainView, widgetConfig);
const message = {
callID: 'test-call-id',
};
widgetWindow.onJoinedCall(null, message);
widgetWindow.onJoinedCall({
sender: {id: 'badID'},
}, message);
expect(widgetWindow.mainView.view.webContents.send).not.toHaveBeenCalled();
widgetWindow.onJoinedCall({
sender: baseWindow.webContents,
}, 'widget', message);
expect(widgetWindow.mainView.view.webContents.send).toHaveBeenCalledWith(CALLS_JOINED_CALL, message);
});
@@ -402,5 +486,27 @@ describe('main/windows/callsWidgetWindow', () => {
const widgetWindow = new CallsWidgetWindow(mainWindow, mainView, widgetConfig);
expect(widgetWindow.getMainView()).toEqual(mainView);
});
it('isAllowedEvent', () => {
baseWindow.webContents = {
...baseWindow.webContents,
id: 'windowID',
};
const widgetWindow = new CallsWidgetWindow(mainWindow, mainView, widgetConfig);
expect(widgetWindow.isAllowedEvent({
sender: {
id: 'senderID',
},
})).toEqual(false);
expect(widgetWindow.isAllowedEvent({
sender: widgetWindow.mainView.view.webContents,
})).toEqual(true);
expect(widgetWindow.isAllowedEvent({
sender: baseWindow.webContents,
})).toEqual(true);
});
});
});