params-check

pull/1177/head
Vincent 5 years ago
parent ac2bb65c2a
commit 94f0fbb65a

@ -153,11 +153,10 @@ export class ChatMessage extends DataMessage {
return dataMessage; return dataMessage;
} }
public isEqual(comparator: ChatMessage) { public isEqual(comparator: ChatMessage): boolean {
return ( return (
this.identifier === comparator.identifier && this.identifier === comparator.identifier &&
this.timestamp === comparator.timestamp && this.timestamp === comparator.timestamp
this.plainTextBuffer.prototype.isEqual(comparator.plainTextBuffer)
); );
} }
} }

@ -3,13 +3,14 @@ import Sinon, * as sinon from 'sinon';
import { GroupUtils } from '../../../session/utils'; import { GroupUtils } 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';
import { OpenGroupMessage } from '../../../session/messages/outgoing'; import { OpenGroupMessage, ChatMessage } from '../../../session/messages/outgoing';
import { PubKey, RawMessage } from '../../../session/types'; import { PubKey, RawMessage } from '../../../session/types';
import { UserUtil } from '../../../util'; import { UserUtil } from '../../../util';
import { MessageSender } from '../../../session/sending'; import { MessageSender } from '../../../session/sending';
import { toRawMessage } from '../../../session/utils/Messages'; import { toRawMessage } from '../../../session/utils/Messages';
import { SessionProtocol } from '../../../session/protocols'; import { SessionProtocol } from '../../../session/protocols';
import { PendingMessageCache } from '../../../session/sending/PendingMessageCache'; import { PendingMessageCache } from '../../../session/sending/PendingMessageCache';
import { generateChatMessage } from '../../test-utils/testUtils';
// Equivalent to Data.StorageItem // Equivalent to Data.StorageItem
interface StorageItem { interface StorageItem {
@ -22,6 +23,7 @@ describe('MessageQueue', () => {
let data: StorageItem; let data: StorageItem;
const sandbox = sinon.createSandbox(); const sandbox = sinon.createSandbox();
const ourNumber = TestUtils.generateFakePubkey().key; const ourNumber = TestUtils.generateFakePubkey().key;
const pairedDevices = TestUtils.generateMemberList(2).map(m => m.key);
// Initialize new stubbed queue // Initialize new stubbed queue
let messageQueueStub: MessageQueue; let messageQueueStub: MessageQueue;
@ -71,9 +73,7 @@ describe('MessageQueue', () => {
// Utils Stubs // Utils Stubs
sandbox.stub(UserUtil, 'getCurrentDevicePubKey').resolves(ourNumber); sandbox.stub(UserUtil, 'getCurrentDevicePubKey').resolves(ourNumber);
TestUtils.stubData('getPairedDevicesFor').callsFake(async () => { TestUtils.stubData('getPairedDevicesFor').resolves(pairedDevices);
return TestUtils.generateMemberList(2);
});
TestUtils.stubWindow('libsignal', { TestUtils.stubWindow('libsignal', {
SignalProtocolAddress: sandbox.stub(), SignalProtocolAddress: sandbox.stub(),
SessionCipher: Stubs.SessionCipherStub, SessionCipher: Stubs.SessionCipherStub,
@ -170,15 +170,25 @@ describe('MessageQueue', () => {
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
const promise = messageQueueStub.sendUsingMultiDevice(device, message); const promise = messageQueueStub.sendUsingMultiDevice(device, message);
expect(promise).to.be.fulfilled;
// Ensure the arguments passed into sendMessageToDevices are correct // Ensure the arguments passed into sendMessageToDevices are correct
await tick(); await tick();
const previousArgs = sendMessageToDevicesSpy.lastCall.args; const previousArgs = sendMessageToDevicesSpy.lastCall.args as [Array<PubKey>, ChatMessage];
// Need to check that instances of ChatMesasge are equal // Check that instances are equal
expect(previousArgs).to.equal({}); expect(previousArgs).to.have.length(2);
expect(promise).to.be.fulfilled; const argsPairedDevices = previousArgs[0];
const argsChatMessage = previousArgs[1];
expect(argsChatMessage instanceof ChatMessage).to.equal(true, 'message passed into sendMessageToDevices was not a valid ChatMessage');
expect(argsChatMessage.isEqual(message)).to.equal(true, 'message passed into sendMessageToDevices has been mutated');
argsPairedDevices.forEach((argsPaired: PubKey, index: number) => {
expect(argsPaired instanceof PubKey).to.equal(true, 'a device passed into sendMessageToDevices was not a PubKey');
expect(argsPaired.key).to.equal(pairedDevices[index]);
});
}); });
}); });

Loading…
Cancel
Save