Commit 25502a3b authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Support filtering groups through `changeFocusModeUser` RPC method

parent 84d66163
......@@ -25,7 +25,17 @@ let preStartQueue = [];
*/
const registeredMethods = store => {
return {
changeFocusModeUser: store.changeFocusModeUser,
/** @param {FocusUserInfo} userInfo */
changeFocusModeUser: userInfo => {
store.changeFocusModeUser(userInfo);
const groupIds = userInfo?.groups ?? [];
const filteredGroupIds = normalizeGroupIds(groupIds, store.allGroups());
if (groupIds.length && !filteredGroupIds.length) {
console.error('No matching groups found in list of filtered group IDs');
}
store.filterGroups(filteredGroupIds);
},
};
};
......
......@@ -11,6 +11,7 @@ class FakeWindow {
}
describe('sidebar/cross-origin-rpc', () => {
let fakeNormalizeGroupIds;
let fakeStore;
let fakeWarnOnce;
let fakeWindow;
......@@ -19,9 +20,13 @@ describe('sidebar/cross-origin-rpc', () => {
beforeEach(() => {
fakeStore = {
allGroups: sinon.stub().returns([]),
changeFocusModeUser: sinon.stub(),
filterGroups: sinon.stub(),
};
fakeNormalizeGroupIds = sinon.stub().returns(['1', '2']);
frame = { postMessage: sinon.stub() };
fakeWindow = new FakeWindow();
......@@ -32,6 +37,7 @@ describe('sidebar/cross-origin-rpc', () => {
fakeWarnOnce = sinon.stub();
$imports.$mock({
'./helpers/groups': { normalizeGroupIds: fakeNormalizeGroupIds },
'../shared/warn-once': fakeWarnOnce,
});
});
......@@ -64,6 +70,83 @@ describe('sidebar/cross-origin-rpc', () => {
);
});
describe('changeFocusModeUser', () => {
function callRPC(params) {
fakeWindow.emitter.emit('message', {
data: {
jsonrpc: '2.0',
method: 'changeFocusModeUser',
id: 42,
params,
},
origin: 'https://allowed1.com',
source: frame,
});
}
beforeEach(() => {
sinon.stub(console, 'error');
});
afterEach(() => {
console.error.restore();
});
it('sets the focused user', () => {
startServer(fakeStore, settings, fakeWindow);
callRPC([{ username: 'foobar', displayName: 'Simon Says' }]);
assert.calledWith(
fakeStore.changeFocusModeUser,
sinon.match({ username: 'foobar', displayName: 'Simon Says' })
);
});
context('groups provided', () => {
it('normalizes any provided group IDs and sets filtered groups', () => {
startServer(fakeStore, settings, fakeWindow);
fakeStore.allGroups.returns(['1', '2', '3']);
fakeNormalizeGroupIds.returns(['1', '2']);
callRPC([{ groups: ['1', '2', '3', '4'] }]);
assert.calledWith(
fakeNormalizeGroupIds,
['1', '2', '3', '4'],
['1', '2', '3']
);
assert.calledWith(fakeStore.filterGroups, ['1', '2']);
assert.notCalled(console.error);
});
it('logs an error if there are provided group IDs but none match any known groups', () => {
startServer(fakeStore, settings, fakeWindow);
fakeNormalizeGroupIds.returns([]);
callRPC([{ groups: ['1', '2', '3'] }]);
assert.calledWith(
console.error,
'No matching groups found in list of filtered group IDs'
);
assert.calledWith(fakeStore.filterGroups, []);
});
});
context('no groups provided', () => {
it('sets filtered groups to an empty set', () => {
startServer(fakeStore, settings, fakeWindow);
fakeNormalizeGroupIds.returns([]);
callRPC([{ groups: [] }]);
assert.calledWith(fakeStore.filterGroups, []);
assert.notCalled(console.error);
});
});
});
it('calls the registered method with the provided params', () => {
startServer(fakeStore, settings, fakeWindow);
......@@ -78,9 +161,7 @@ describe('sidebar/cross-origin-rpc', () => {
source: frame,
});
assert.isTrue(
fakeStore.changeFocusModeUser.calledWithExactly('one', 'two')
);
assert.isTrue(fakeStore.changeFocusModeUser.calledWithExactly('one'));
});
it('calls the registered method with no params', () => {
......@@ -95,7 +176,7 @@ describe('sidebar/cross-origin-rpc', () => {
origin: 'https://allowed1.com',
source: frame,
});
assert.isTrue(fakeStore.changeFocusModeUser.calledWithExactly());
assert.isTrue(fakeStore.changeFocusModeUser.calledWithExactly(undefined));
});
it('does not call the unregistered method', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment