generateMemberList-compactify

pull/1177/head
Vincent 5 years ago
parent 18af13d7d9
commit 20b2ba1c8a

@ -31,27 +31,37 @@ export class MultiDeviceProtocol {
// This return here stops an infinite loop when we get all our other devices // This return here stops an infinite loop when we get all our other devices
const ourKey = await UserUtil.getCurrentDevicePubKey(); const ourKey = await UserUtil.getCurrentDevicePubKey();
if (!ourKey || device.key === ourKey) { if (!ourKey || device.key === ourKey) {
console.log(`[vince] 1 GUARD`);
console.log('[vince] ourKey:', ourKey);
console.log('[vince] device:', device);
return; return;
} }
// We always prefer our local pairing over the one on the server // We always prefer our local pairing over the one on the server
const isOurDevice = await this.isOurDevice(device); const isOurDevice = await this.isOurDevice(device);
if (isOurDevice) { if (isOurDevice) {
console.log(`[vince] 2 GUARD`);
return; return;
} }
// Only fetch if we hit the refresh delay // Only fetch if we hit the refresh delay
const lastFetchTime = this.lastFetch[device.key]; const lastFetchTime = this.lastFetch[device.key];
if (lastFetchTime && lastFetchTime + this.refreshDelay > Date.now()) { if (lastFetchTime && lastFetchTime + this.refreshDelay > Date.now()) {
console.log(`[vince] 3 GUARD`);
return; return;
} }
this.lastFetch[device.key] = Date.now(); this.lastFetch[device.key] = Date.now();
console.log(`[vince] 4 GUARD`);
try { try {
console.log(`[vince] 5 GUARD`);
const authorisations = await this.fetchPairingAuthorisations(device); const authorisations = await this.fetchPairingAuthorisations(device);
await Promise.all(authorisations.map(this.savePairingAuthorisation)); await Promise.all(authorisations.map(this.savePairingAuthorisation));
} catch (e) { } catch (e) {
console.log(`[vince] 6 GUARD`);
// Something went wrong, let it re-try another time // Something went wrong, let it re-try another time
this.lastFetch[device.key] = lastFetchTime; this.lastFetch[device.key] = lastFetchTime;
} }

@ -42,7 +42,7 @@ describe('MultiDeviceProtocol', () => {
'getPairingAuthorisationsFor' 'getPairingAuthorisationsFor'
).resolves([]); ).resolves([]);
await MultiDeviceProtocol.getPairingAuthorisations( await MultiDeviceProtocol.getPairingAuthorisations(
TestUtils.generateFakePubkey() TestUtils.generateFakePubKey()
); );
expect(fetchPairingStub.called).to.equal(true, 'Pairing is not fetched.'); expect(fetchPairingStub.called).to.equal(true, 'Pairing is not fetched.');
expect(fetchPairingStub.calledBefore(dataStub)).to.equal( expect(fetchPairingStub.calledBefore(dataStub)).to.equal(
@ -52,8 +52,8 @@ describe('MultiDeviceProtocol', () => {
}); });
it('should return the authorisations from the database', async () => { it('should return the authorisations from the database', async () => {
const device1 = TestUtils.generateFakePubkey(); const device1 = TestUtils.generateFakePubKey();
const device2 = TestUtils.generateFakePubkey(); const device2 = TestUtils.generateFakePubKey();
const pairing: PairingAuthorisation = { const pairing: PairingAuthorisation = {
primaryDevicePubKey: device1.key, primaryDevicePubKey: device1.key,
secondaryDevicePubKey: device2.key, secondaryDevicePubKey: device2.key,
@ -74,7 +74,7 @@ describe('MultiDeviceProtocol', () => {
TestUtils.stubWindow('lokiFileServerAPI', undefined); TestUtils.stubWindow('lokiFileServerAPI', undefined);
expect( expect(
MultiDeviceProtocol.fetchPairingAuthorisations( MultiDeviceProtocol.fetchPairingAuthorisations(
TestUtils.generateFakePubkey() TestUtils.generateFakePubKey()
) )
).to.be.rejectedWith('lokiFileServerAPI is not initialised.'); ).to.be.rejectedWith('lokiFileServerAPI is not initialised.');
}); });
@ -100,7 +100,7 @@ describe('MultiDeviceProtocol', () => {
}); });
const authorisations = await MultiDeviceProtocol.fetchPairingAuthorisations( const authorisations = await MultiDeviceProtocol.fetchPairingAuthorisations(
TestUtils.generateFakePubkey() TestUtils.generateFakePubKey()
); );
expect(authorisations.length).to.equal(1); expect(authorisations.length).to.equal(1);
@ -139,8 +139,8 @@ describe('MultiDeviceProtocol', () => {
fetchPairingAuthorisationStub = sandbox fetchPairingAuthorisationStub = sandbox
.stub(MultiDeviceProtocol, 'fetchPairingAuthorisations') .stub(MultiDeviceProtocol, 'fetchPairingAuthorisations')
.resolves([]); .resolves([]);
currentDevice = TestUtils.generateFakePubkey(); currentDevice = TestUtils.generateFakePubKey();
device = TestUtils.generateFakePubkey(); device = TestUtils.generateFakePubKey();
sandbox sandbox
.stub(UserUtil, 'getCurrentDevicePubKey') .stub(UserUtil, 'getCurrentDevicePubKey')
.resolves(currentDevice.key); .resolves(currentDevice.key);
@ -235,7 +235,7 @@ describe('MultiDeviceProtocol', () => {
describe('getAllDevices', () => { describe('getAllDevices', () => {
it('should return all devices', async () => { it('should return all devices', async () => {
const primary = TestUtils.generateFakePubkey(); const primary = TestUtils.generateFakePubKey();
const otherDevices = TestUtils.generateFakePubKeys(2); const otherDevices = TestUtils.generateFakePubKeys(2);
const authorisations = generateFakeAuthorisations(primary, otherDevices); const authorisations = generateFakeAuthorisations(primary, otherDevices);
sandbox sandbox
@ -253,7 +253,7 @@ describe('MultiDeviceProtocol', () => {
describe('getPrimaryDevice', () => { describe('getPrimaryDevice', () => {
it('should return the primary device', async () => { it('should return the primary device', async () => {
const primary = TestUtils.generateFakePubkey(); const primary = TestUtils.generateFakePubKey();
const otherDevices = TestUtils.generateFakePubKeys(2); const otherDevices = TestUtils.generateFakePubKeys(2);
const authorisations = generateFakeAuthorisations(primary, otherDevices); const authorisations = generateFakeAuthorisations(primary, otherDevices);
sandbox sandbox
@ -270,7 +270,7 @@ describe('MultiDeviceProtocol', () => {
describe('getSecondaryDevices', () => { describe('getSecondaryDevices', () => {
it('should return the secondary devices', async () => { it('should return the secondary devices', async () => {
const primary = TestUtils.generateFakePubkey(); const primary = TestUtils.generateFakePubKey();
const otherDevices = TestUtils.generateFakePubKeys(2); const otherDevices = TestUtils.generateFakePubKeys(2);
const authorisations = generateFakeAuthorisations(primary, otherDevices); const authorisations = generateFakeAuthorisations(primary, otherDevices);
sandbox sandbox

@ -1,5 +1,6 @@
import { expect } from 'chai'; import { expect } from 'chai';
import * as sinon from 'sinon'; import * as sinon from 'sinon';
import * as _ from 'lodash';
import { GroupUtils, SyncMessageUtils } from '../../../session/utils'; import { GroupUtils, SyncMessageUtils } from '../../../session/utils';
import { Stubs, TestUtils } from '../../../test/test-utils'; import { Stubs, TestUtils } from '../../../test/test-utils';
import { MessageQueue } from '../../../session/sending/MessageQueue'; import { MessageQueue } from '../../../session/sending/MessageQueue';
@ -36,9 +37,9 @@ describe('MessageQueue', () => {
// Initialize new stubbed cache // Initialize new stubbed cache
let data: StorageItem; let data: StorageItem;
const sandbox = sinon.createSandbox(); const sandbox = sinon.createSandbox();
const ourDevice = TestUtils.generateFakePubkey(); const ourDevice = TestUtils.generateFakePubKey();
const ourNumber = ourDevice.key; const ourNumber = ourDevice.key;
const pairedDevices = TestUtils.generateMemberList(2); const pairedDevices = TestUtils.generateFakePubKeys(2);
// Initialize new stubbed queue // Initialize new stubbed queue
let messageQueueStub: MessageQueue; let messageQueueStub: MessageQueue;
@ -106,7 +107,7 @@ describe('MessageQueue', () => {
sandbox.stub(GroupUtils, 'isMediumGroup').returns(false); sandbox.stub(GroupUtils, 'isMediumGroup').returns(false);
groupMembersStub = sandbox groupMembersStub = sandbox
.stub(GroupUtils, 'getGroupMembers' as any) .stub(GroupUtils, 'getGroupMembers' as any)
.resolves(TestUtils.generateMemberList(10)); .resolves(TestUtils.generateFakePubKeys(10));
// Session Protocol Stubs // Session Protocol Stubs
sandbox.stub(SessionProtocol, 'sendSessionRequest').resolves(); sandbox.stub(SessionProtocol, 'sendSessionRequest').resolves();
@ -121,7 +122,7 @@ describe('MessageQueue', () => {
TestUtils.generateChatMessage TestUtils.generateChatMessage
); );
const rawMessage = toRawMessage( const rawMessage = toRawMessage(
TestUtils.generateFakePubkey(), TestUtils.generateFakePubKey(),
TestUtils.generateChatMessage() TestUtils.generateChatMessage()
); );
@ -129,11 +130,11 @@ describe('MessageQueue', () => {
sandbox.stub(PendingMessageCache.prototype, 'remove').resolves(); sandbox.stub(PendingMessageCache.prototype, 'remove').resolves();
sandbox sandbox
.stub(PendingMessageCache.prototype, 'getDevices') .stub(PendingMessageCache.prototype, 'getDevices')
.returns(TestUtils.generateMemberList(10)); .returns(TestUtils.generateFakePubKeys(10));
sandbox sandbox
.stub(PendingMessageCache.prototype, 'getForDevice') .stub(PendingMessageCache.prototype, 'getForDevice')
.returns( .returns(
chatMessages.map(m => toRawMessage(TestUtils.generateFakePubkey(), m)) chatMessages.map(m => toRawMessage(TestUtils.generateFakePubKey(), m))
); );
// Spies // Spies
@ -155,7 +156,7 @@ describe('MessageQueue', () => {
describe('send', () => { describe('send', () => {
it('can send to a single device', async () => { it('can send to a single device', async () => {
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.send(device, message); const promise = messageQueueStub.send(device, message);
@ -163,7 +164,7 @@ describe('MessageQueue', () => {
}); });
it('can send sync message', async () => { it('can send sync message', async () => {
const devices = TestUtils.generateMemberList(3); const devices = TestUtils.generateFakePubKeys(3);
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.sendSyncMessage(message, devices); const promise = messageQueueStub.sendSyncMessage(message, devices);
@ -175,7 +176,7 @@ describe('MessageQueue', () => {
it('will send session request message if no session', async () => { it('will send session request message if no session', async () => {
hasSessionStub.resolves(false); hasSessionStub.resolves(false);
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const promise = messageQueueStub.processPending(device); const promise = messageQueueStub.processPending(device);
await expect(promise).to.be.fulfilled; await expect(promise).to.be.fulfilled;
@ -183,7 +184,7 @@ describe('MessageQueue', () => {
}); });
it('will send message if session exists', async () => { it('will send message if session exists', async () => {
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const hasSession = await hasSessionStub(device); const hasSession = await hasSessionStub(device);
const promise = messageQueueStub.processPending(device); const promise = messageQueueStub.processPending(device);
@ -196,7 +197,7 @@ describe('MessageQueue', () => {
describe('sendUsingMultiDevice', () => { describe('sendUsingMultiDevice', () => {
it('can send using multidevice', async () => { it('can send using multidevice', async () => {
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.sendUsingMultiDevice(device, message); const promise = messageQueueStub.sendUsingMultiDevice(device, message);
@ -238,7 +239,7 @@ describe('MessageQueue', () => {
describe('sendMessageToDevices', () => { describe('sendMessageToDevices', () => {
it('can send to many devices', async () => { it('can send to many devices', async () => {
const devices = TestUtils.generateMemberList(10); const devices = TestUtils.generateFakePubKeys(10);
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.sendMessageToDevices(devices, message); const promise = messageQueueStub.sendMessageToDevices(devices, message);
@ -248,10 +249,9 @@ describe('MessageQueue', () => {
it('can send sync message and confirm canSync is valid', async () => { it('can send sync message and confirm canSync is valid', async () => {
canSyncStub.returns(true); canSyncStub.returns(true);
const devices = TestUtils.generateMemberList(3); const devices = TestUtils.generateFakePubKeys(3);
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const pairedDeviceKeys = pairedDevices.map(device => device.key); const pairedDeviceKeys = pairedDevices.map(device => device.key);
const ourDeviceKeys = [...pairedDeviceKeys, ourNumber].sort();
const promise = messageQueueStub.sendMessageToDevices(devices, message); const promise = messageQueueStub.sendMessageToDevices(devices, message);
await expect(promise).to.be.fulfilled; await expect(promise).to.be.fulfilled;
@ -267,7 +267,7 @@ describe('MessageQueue', () => {
expect(previousArgs).to.have.length(2); expect(previousArgs).to.have.length(2);
const argsChatMessage = previousArgs[0]; const argsChatMessage = previousArgs[0];
const argsPairedKeys = [...previousArgs[1]].map(d => d.key).sort(); const argsPairedKeys = [...previousArgs[1]].map(d => d.key);
expect(argsChatMessage instanceof ChatMessage).to.equal( expect(argsChatMessage instanceof ChatMessage).to.equal(
true, true,
@ -278,9 +278,9 @@ describe('MessageQueue', () => {
'message passed into sendMessageToDevices has been mutated' 'message passed into sendMessageToDevices has been mutated'
); );
argsPairedKeys.forEach((argsPaired: string, index: number) => { // argsPairedKeys and pairedDeviceKeys should contain the same values
expect(argsPaired).to.equal(ourDeviceKeys[index]); const keyArgsValid = _.isEmpty(_.xor(argsPairedKeys, pairedDeviceKeys));
}); expect(keyArgsValid).to.equal(true, 'devices passed into sendSyncMessage were invalid');
}); });
}); });
@ -334,7 +334,7 @@ describe('MessageQueue', () => {
}); });
it('wont send message to empty closed group', async () => { it('wont send message to empty closed group', async () => {
groupMembersStub.resolves(TestUtils.generateMemberList(0)); groupMembersStub.resolves(TestUtils.generateFakePubKeys(0));
const message = TestUtils.generateClosedGroupMessage(); const message = TestUtils.generateClosedGroupMessage();
const response = await messageQueueStub.sendToGroup(message); const response = await messageQueueStub.sendToGroup(message);
@ -358,7 +358,7 @@ describe('MessageQueue', () => {
const successSpy = sandbox.spy(); const successSpy = sandbox.spy();
messageQueueStub.events.on('success', successSpy); messageQueueStub.events.on('success', successSpy);
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const promise = messageQueueStub.processPending(device); const promise = messageQueueStub.processPending(device);
await expect(promise).to.be.fulfilled; await expect(promise).to.be.fulfilled;
@ -372,7 +372,7 @@ describe('MessageQueue', () => {
const failureSpy = sandbox.spy(); const failureSpy = sandbox.spy();
messageQueueStub.events.on('fail', failureSpy); messageQueueStub.events.on('fail', failureSpy);
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const promise = messageQueueStub.processPending(device); const promise = messageQueueStub.processPending(device);
await expect(promise).to.be.fulfilled; await expect(promise).to.be.fulfilled;

@ -52,7 +52,7 @@ describe('PendingMessageCache', () => {
}); });
it('can add to cache', async () => { it('can add to cache', async () => {
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const rawMessage = MessageUtils.toRawMessage(device, message); const rawMessage = MessageUtils.toRawMessage(device, message);
@ -69,7 +69,7 @@ describe('PendingMessageCache', () => {
}); });
it('can remove from cache', async () => { it('can remove from cache', async () => {
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const rawMessage = MessageUtils.toRawMessage(device, message); const rawMessage = MessageUtils.toRawMessage(device, message);
@ -90,15 +90,15 @@ describe('PendingMessageCache', () => {
it('can get devices', async () => { it('can get devices', async () => {
const cacheItems = [ const cacheItems = [
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
]; ];
@ -122,11 +122,11 @@ describe('PendingMessageCache', () => {
it('can get pending for device', async () => { it('can get pending for device', async () => {
const cacheItems = [ const cacheItems = [
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
]; ];
@ -149,7 +149,7 @@ describe('PendingMessageCache', () => {
}); });
it('can find nothing when empty', async () => { it('can find nothing when empty', async () => {
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const rawMessage = MessageUtils.toRawMessage(device, message); const rawMessage = MessageUtils.toRawMessage(device, message);
@ -158,7 +158,7 @@ describe('PendingMessageCache', () => {
}); });
it('can find message in cache', async () => { it('can find message in cache', async () => {
const device = TestUtils.generateFakePubkey(); const device = TestUtils.generateFakePubKey();
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const rawMessage = MessageUtils.toRawMessage(device, message); const rawMessage = MessageUtils.toRawMessage(device, message);
@ -175,15 +175,15 @@ describe('PendingMessageCache', () => {
it('can clear cache', async () => { it('can clear cache', async () => {
const cacheItems = [ const cacheItems = [
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
]; ];
@ -205,15 +205,15 @@ describe('PendingMessageCache', () => {
it('can restore from db', async () => { it('can restore from db', async () => {
const cacheItems = [ const cacheItems = [
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
{ {
device: TestUtils.generateFakePubkey(), device: TestUtils.generateFakePubKey(),
message: TestUtils.generateChatMessage(), message: TestUtils.generateChatMessage(),
}, },
]; ];

@ -69,7 +69,7 @@ export function restoreStubs() {
sandbox.restore(); sandbox.restore();
} }
export function generateFakePubkey(): PubKey { export function generateFakePubKey(): PubKey {
// Generates a mock pubkey for testing // Generates a mock pubkey for testing
const numBytes = PubKey.PUBKEY_LEN / 2 - 1; const numBytes = PubKey.PUBKEY_LEN / 2 - 1;
const hexBuffer = crypto.randomBytes(numBytes).toString('hex'); const hexBuffer = crypto.randomBytes(numBytes).toString('hex');
@ -79,8 +79,12 @@ export function generateFakePubkey(): PubKey {
} }
export function generateFakePubKeys(amount: number): Array<PubKey> { export function generateFakePubKeys(amount: number): Array<PubKey> {
const numPubKeys = amount > 0
? Math.floor(amount)
: 0;
// tslint:disable-next-line: no-unnecessary-callback-wrapper // tslint:disable-next-line: no-unnecessary-callback-wrapper
return new Array(amount).fill(0).map(() => generateFakePubkey()); return new Array(numPubKeys).fill(0).map(() => generateFakePubKey());
} }
export function generateChatMessage(): ChatMessage { export function generateChatMessage(): ChatMessage {
@ -118,15 +122,8 @@ export function generateClosedGroupMessage(
): ClosedGroupChatMessage { ): ClosedGroupChatMessage {
return new ClosedGroupChatMessage({ return new ClosedGroupChatMessage({
identifier: uuid(), identifier: uuid(),
groupId: groupId ?? generateFakePubkey().key, groupId: groupId ?? generateFakePubKey().key,
chatMessage: generateChatMessage(), chatMessage: generateChatMessage(),
}); });
} }
export function generateMemberList(size: number): Array<PubKey> {
const numMembers = Math.floor(size);
return numMembers > 0
? Array.from({ length: numMembers }, generateFakePubkey)
: [];
}

Loading…
Cancel
Save