remove Content.toObject() from tests and update them

pull/1151/head
Audric Ackermann 5 years ago
parent 1f2f0535a1
commit 113cf8713b
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -1,6 +1,6 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { ChatMessage, Quote, Preview, AttachmentPointer } from '../../../session/messages/outgoing'; import { AttachmentPointer, ChatMessage, Preview, Quote } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf'; import { SignalService } from '../../../protobuf';
import { TextEncoder } from 'util'; import { TextEncoder } from 'util';
@ -10,8 +10,9 @@ describe('ChatMessage', () => {
timestamp: Date.now(), timestamp: Date.now(),
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded).to.have.deep.property('dataMessage', {}); expect(decoded).to.have.not.property('dataMessage', null);
expect(decoded).to.have.not.property('dataMessage', undefined);
}); });
it('can create message with a body', () => { it('can create message with a body', () => {
@ -20,7 +21,7 @@ describe('ChatMessage', () => {
body: 'body', body: 'body',
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage).to.have.deep.property('body', 'body'); expect(decoded.dataMessage).to.have.deep.property('body', 'body');
}); });
@ -30,7 +31,7 @@ describe('ChatMessage', () => {
expireTimer: 3600, expireTimer: 3600,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage).to.have.deep.property('expireTimer', 3600); expect(decoded.dataMessage).to.have.deep.property('expireTimer', 3600);
}); });
@ -47,9 +48,11 @@ describe('ChatMessage', () => {
lokiProfile: lokiProfile, lokiProfile: lokiProfile,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage.profile).to.have.deep.property('displayName', 'displayName'); expect(decoded.dataMessage).to.have.deep.property('profile');
expect(decoded.dataMessage.profile).to.have.deep.property('avatar', 'avatarPointer');
expect(decoded.dataMessage).to.have.property('profile').to.have.deep.property('displayName', 'displayName');
expect(decoded.dataMessage).to.have.property('profile').to.have.deep.property('avatar', 'avatarPointer');
expect(decoded.dataMessage).to.have.deep.property('profileKey', profileKey); expect(decoded.dataMessage).to.have.deep.property('profileKey', profileKey);
}); });
@ -62,11 +65,10 @@ describe('ChatMessage', () => {
quote, quote,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
const id = decoded.dataMessage.quote.id.toNumber(); expect(decoded.dataMessage?.quote?.id).to.have.property('low', 1234);
expect(id).to.be.deep.equal(1234); expect(decoded.dataMessage?.quote).to.have.deep.property('author', 'author');
expect(decoded.dataMessage.quote).to.have.deep.property('author', 'author'); expect(decoded.dataMessage?.quote).to.have.deep.property('text', 'text');
expect(decoded.dataMessage.quote).to.have.deep.property('text', 'text');
}); });
it('can create message with a preview', () => { it('can create message with a preview', () => {
@ -81,10 +83,10 @@ describe('ChatMessage', () => {
preview: previews, preview: previews,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage.preview).to.have.lengthOf(1); expect(decoded.dataMessage?.preview).to.have.lengthOf(1);
expect(decoded.dataMessage.preview[0]).to.have.deep.property('url', 'url'); expect(decoded.dataMessage).to.have.nested.property('preview[0].url').to.be.deep.equal('url');
expect(decoded.dataMessage.preview[0]).to.have.deep.property('title', 'title'); expect(decoded.dataMessage).to.have.nested.property('preview[0].title').to.be.deep.equal('title');
}); });
@ -100,12 +102,11 @@ describe('ChatMessage', () => {
attachments: attachments, attachments: attachments,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage.attachments).to.have.lengthOf(1); expect(decoded.dataMessage?.attachments).to.have.lengthOf(1);
expect(decoded.dataMessage.attachments[0]).to.have.deep.property('url', 'url'); expect(decoded.dataMessage).to.have.nested.property('attachments[0].id').to.have.property('low', 1234);
const id = decoded.dataMessage.attachments[0].id.toNumber(); expect(decoded.dataMessage).to.have.nested.property('attachments[0].contentType').to.be.deep.equal('contentType');
expect(id).to.be.equal(1234); expect(decoded.dataMessage).to.have.nested.property('attachments[0].url').to.be.deep.equal('url');
expect(decoded.dataMessage.attachments[0]).to.have.deep.property('contentType', 'contentType');
}); });
it('ttl of 1 day', () => { it('ttl of 1 day', () => {

@ -38,7 +38,7 @@ describe('DeviceLinkMessage', () => {
let decoded: any; let decoded: any;
before(() => { before(() => {
const plainText = linkRequestMessage.plainTextBuffer(); const plainText = linkRequestMessage.plainTextBuffer();
decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); decoded = SignalService.Content.decode(plainText);
}); });
it('has a pairingAuthorisation.primaryDevicePubKey', () => { it('has a pairingAuthorisation.primaryDevicePubKey', () => {
@ -51,7 +51,8 @@ describe('DeviceLinkMessage', () => {
expect(decoded.pairingAuthorisation).to.have.property('requestSignature').to.deep.equal(new Uint8Array([1, 2, 3, 4, 5, 6])); expect(decoded.pairingAuthorisation).to.have.property('requestSignature').to.deep.equal(new Uint8Array([1, 2, 3, 4, 5, 6]));
}); });
it('has no pairingAuthorisation.grantSignature', () => { it('has no pairingAuthorisation.grantSignature', () => {
expect(decoded.pairingAuthorisation).to.not.have.property('grantSignature'); console.log(decoded)
expect(decoded.pairingAuthorisation).to.have.property('grantSignature').to.have.lengthOf(0);
}); });
it('has no lokiProfile', () => { it('has no lokiProfile', () => {
expect(decoded).to.not.have.property('lokiProfile'); expect(decoded).to.not.have.property('lokiProfile');
@ -62,7 +63,7 @@ describe('DeviceLinkMessage', () => {
let decoded: any; let decoded: any;
before(() => { before(() => {
const plainText = linkGrantMessage.plainTextBuffer(); const plainText = linkGrantMessage.plainTextBuffer();
decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); decoded = SignalService.Content.decode(plainText);
}); });
it('has a pairingAuthorisation.primaryDevicePubKey', () => { it('has a pairingAuthorisation.primaryDevicePubKey', () => {
@ -79,10 +80,8 @@ describe('DeviceLinkMessage', () => {
}); });
it('has a lokiProfile', () => { it('has a lokiProfile', () => {
expect(decoded.dataMessage).to.have.property('profileKey').to.be.deep.equal(lokiProfile.profileKey); expect(decoded.dataMessage).to.have.property('profileKey').to.be.deep.equal(lokiProfile.profileKey);
expect(decoded.dataMessage).to.have.property('profile').to.be.deep.equal({ expect(decoded.dataMessage).to.have.property('profile').to.have.property('displayName').to.be.deep.equal('displayName');
displayName: 'displayName', expect(decoded.dataMessage).to.have.property('profile').to.have.property('avatar').to.be.deep.equal('avatarPointer');
avatar: 'avatarPointer',
});
}); });
}); });

@ -13,7 +13,7 @@ describe('DeviceUnlinkMessage', () => {
it('content of just the UNPAIRING_REQUEST flag set', () => { it('content of just the UNPAIRING_REQUEST flag set', () => {
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage).to.have.property('flags', SignalService.DataMessage.Flags.UNPAIRING_REQUEST); expect(decoded.dataMessage).to.have.property('flags', SignalService.DataMessage.Flags.UNPAIRING_REQUEST);
}); });

@ -3,25 +3,41 @@ import { beforeEach} from 'mocha';
import { EndSessionMessage } from '../../../session/messages/outgoing'; import { EndSessionMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf'; import { SignalService } from '../../../protobuf';
import { TextEncoder } from 'util';
describe('EndSessionMessage', () => { describe('EndSessionMessage', () => {
let message: EndSessionMessage; let message: EndSessionMessage;
const preKeyBundle = {
deviceId: 123456,
preKeyId: 654321,
signedKeyId: 111111,
preKey: new TextEncoder().encode('preKey'),
signature: new TextEncoder().encode('signature'),
signedKey: new TextEncoder().encode('signedKey'),
identityKey: new TextEncoder().encode('identityKey'),
};
beforeEach(() => { beforeEach(() => {
const timestamp = Date.now(); const timestamp = Date.now();
const preKeyBundle = {deviceId: 123456};
message = new EndSessionMessage({timestamp, preKeyBundle}); message = new EndSessionMessage({timestamp, preKeyBundle});
}); });
it('has a preKeyBundle', () => { it('has a preKeyBundle', () => {
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.preKeyBundleMessage).to.have.property('deviceId', preKeyBundle.deviceId);
expect(decoded.preKeyBundleMessage).to.have.property('preKeyId', preKeyBundle.preKeyId);
expect(decoded.preKeyBundleMessage).to.have.property('signedKeyId', preKeyBundle.signedKeyId);
expect(decoded.preKeyBundleMessage).to.have.property('deviceId', 123456); expect(decoded.preKeyBundleMessage).to.have.deep.property('signature', preKeyBundle.signature);
expect(decoded.preKeyBundleMessage).to.have.deep.property('signedKey', preKeyBundle.signedKey);
expect(decoded.preKeyBundleMessage).to.have.deep.property('identityKey', preKeyBundle.identityKey);
}); });
it('has a dataMessage with `END_SESSION` flag and `TERMINATE` as body', () => { it('has a dataMessage with `END_SESSION` flag and `TERMINATE` as body', () => {
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage).to.have.property('flags', SignalService.DataMessage.Flags.END_SESSION); expect(decoded.dataMessage).to.have.property('flags', SignalService.DataMessage.Flags.END_SESSION);
expect(decoded.dataMessage).to.have.deep.property('body', 'TERMINATE'); expect(decoded.dataMessage).to.have.deep.property('body', 'TERMINATE');

@ -22,11 +22,11 @@ describe('GroupInvitationMessage', () => {
it('dataMessage.groupInvitation has serverAddress, channelId, and serverName set', () => { it('dataMessage.groupInvitation has serverAddress, channelId, and serverName set', () => {
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.dataMessage.groupInvitation).to.have.property('serverAddress', serverAddress); expect(decoded.dataMessage?.groupInvitation).to.have.property('serverAddress', serverAddress);
expect(decoded.dataMessage.groupInvitation).to.have.property('channelId', channelId); expect(decoded.dataMessage?.groupInvitation).to.have.property('channelId', channelId);
expect(decoded.dataMessage.groupInvitation).to.have.property('serverName', serverName); expect(decoded.dataMessage?.groupInvitation).to.have.property('serverName', serverName);
}); });
it('ttl of 1 day', () => { it('ttl of 1 day', () => {

@ -18,24 +18,22 @@ describe('ReceiptMessage', () => {
it('content of a read receipt is correct', () => { it('content of a read receipt is correct', () => {
const plainText = readMessage.plainTextBuffer(); const plainText = readMessage.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.receiptMessage).to.have.property('type', 1); expect(decoded.receiptMessage).to.have.property('type', 1);
expect(decoded.receiptMessage.timestamp).to.have.lengthOf(2); expect(decoded.receiptMessage?.timestamp).to.have.lengthOf(2);
const timestamp0 = decoded.receiptMessage.timestamp[0].toNumber(); expect(decoded.receiptMessage).to.have.nested.property('timestamp[0].low', timestamps[0]);
const timestamp1 = decoded.receiptMessage.timestamp[1].toNumber(); expect(decoded.receiptMessage).to.have.nested.property('timestamp[1].low', timestamps[1]);
expect(timestamp0).to.have.be.equal(timestamps[0]);
expect(timestamp1).to.have.be.equal(timestamps[1]);
}); });
it('content of a delivery receipt is correct', () => { it('content of a delivery receipt is correct', () => {
const plainText = deliveryMessage.plainTextBuffer(); const plainText = deliveryMessage.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.receiptMessage).to.have.property('type', 0); expect(decoded.receiptMessage).to.have.property('type', 0);
expect(decoded.receiptMessage.timestamp).to.have.lengthOf(2); expect(decoded.receiptMessage?.timestamp).to.have.lengthOf(2);
expect(decoded.receiptMessage.timestamp[0]).to.have.property('low', timestamps[0]); expect(decoded.receiptMessage).to.have.nested.property('timestamp[0].low', timestamps[0]);
expect(decoded.receiptMessage.timestamp[1]).to.have.property('low', timestamps[1]); expect(decoded.receiptMessage).to.have.nested.property('timestamp[1].low', timestamps[1]);
}); });
it('ttl of 1 day', () => { it('ttl of 1 day', () => {

@ -13,7 +13,7 @@ describe('SessionEstablishedMessage', () => {
it('has a nullMessage not null', () => { it('has a nullMessage not null', () => {
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.nullMessage).to.be.not.equal(null, 'decoded.dataMessage.nullMessage should not be null'); expect(decoded.nullMessage).to.be.not.equal(null, 'decoded.dataMessage.nullMessage should not be null');
}); });

@ -3,25 +3,45 @@ import { beforeEach} from 'mocha';
import { SessionResetMessage } from '../../../session/messages/outgoing'; import { SessionResetMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf'; import { SignalService } from '../../../protobuf';
import { TextDecoder, TextEncoder } from 'util';
describe('SessionResetMessage', () => { describe('SessionResetMessage', () => {
let message: SessionResetMessage; let message: SessionResetMessage;
const preKeyBundle = {
deviceId: 123456,
preKeyId: 654321,
signedKeyId: 111111,
preKey: new TextEncoder().encode('preKey'),
signature: new TextEncoder().encode('signature'),
signedKey: new TextEncoder().encode('signedKey'),
identityKey: new TextEncoder().encode('identityKey'),
};
beforeEach(() => { beforeEach(() => {
const timestamp = Date.now(); const timestamp = Date.now();
const preKeyBundle = {deviceId: 123456};
message = new SessionResetMessage({timestamp, preKeyBundle}); message = new SessionResetMessage({timestamp, preKeyBundle});
}); });
it('has a preKeyBundle', () => { it('has a preKeyBundle', () => {
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.preKeyBundleMessage).to.have.property('deviceId', preKeyBundle.deviceId);
expect(decoded.preKeyBundleMessage).to.have.property('preKeyId', preKeyBundle.preKeyId);
expect(decoded.preKeyBundleMessage).to.have.property('signedKeyId', preKeyBundle.signedKeyId);
const signature = new TextDecoder().decode(decoded.preKeyBundleMessage?.signature);
const signedKey = new TextDecoder().decode(decoded.preKeyBundleMessage?.signedKey);
const identityKey = new TextDecoder().decode(decoded.preKeyBundleMessage?.identityKey);
expect(decoded.preKeyBundleMessage).to.have.property('deviceId', 123456); expect(signature).to.be.deep.equal('signature');
expect(signedKey).to.be.deep.equal('signedKey');
expect(identityKey).to.be.deep.equal('identityKey');
}); });
it('has a nullMessage not null', () => { it('has a nullMessage not null', () => {
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.nullMessage).to.be.not.equal(null, 'decoded.dataMessage.nullMessage should not be null'); expect(decoded.nullMessage).to.be.not.equal(null, 'decoded.dataMessage.nullMessage should not be null');
}); });

@ -3,6 +3,7 @@ import { expect } from 'chai';
import { TypingMessage } from '../../../session/messages/outgoing'; import { TypingMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf'; import { SignalService } from '../../../protobuf';
import { TextEncoder } from 'util'; import { TextEncoder } from 'util';
import Long from 'long';
describe('TypingMessage', () => { describe('TypingMessage', () => {
it('has Action.STARTED if isTyping = true', () => { it('has Action.STARTED if isTyping = true', () => {
@ -11,7 +12,7 @@ describe('TypingMessage', () => {
isTyping: true, isTyping: true,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.typingMessage).to.have.property('action', SignalService.TypingMessage.Action.STARTED); expect(decoded.typingMessage).to.have.property('action', SignalService.TypingMessage.Action.STARTED);
}); });
@ -21,7 +22,7 @@ describe('TypingMessage', () => {
isTyping: false, isTyping: false,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
expect(decoded.typingMessage).to.have.property('action', SignalService.TypingMessage.Action.STOPPED); expect(decoded.typingMessage).to.have.property('action', SignalService.TypingMessage.Action.STOPPED);
}); });
@ -32,9 +33,8 @@ describe('TypingMessage', () => {
typingTimestamp: 111111111, typingTimestamp: 111111111,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
const typingTimestamp = decoded.typingMessage.timestamp.toNumber(); expect(decoded.typingMessage?.timestamp).to.have.property('low', 111111111);
expect(typingTimestamp).to.be.equal(111111111);
}); });
it('has typingTimestamp set with Date.now() if value not passed', () => { it('has typingTimestamp set with Date.now() if value not passed', () => {
@ -43,9 +43,13 @@ describe('TypingMessage', () => {
isTyping: true, isTyping: true,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
const typingTimestamp = decoded.typingMessage.timestamp.toNumber(); let timestamp = decoded.typingMessage?.timestamp;
expect(typingTimestamp).to.be.approximately(Date.now(), 10); if (timestamp instanceof Long) {
timestamp = timestamp.toNumber();
}
expect(timestamp).to.be.approximately(Date.now(), 10);
}); });
it('has groupId set if a value given', () => { it('has groupId set if a value given', () => {
@ -56,10 +60,10 @@ describe('TypingMessage', () => {
groupId, groupId,
}); });
const plainText = message.plainTextBuffer(); const plainText = message.plainTextBuffer();
const decoded = SignalService.Content.toObject(SignalService.Content.decode(plainText)); const decoded = SignalService.Content.decode(plainText);
const manuallyEncodedGroupId = new TextEncoder().encode(groupId); const manuallyEncodedGroupId = new TextEncoder().encode(groupId);
expect(decoded.typingMessage.groupId).to.be.deep.equal(manuallyEncodedGroupId); expect(decoded.typingMessage?.groupId).to.be.deep.equal(manuallyEncodedGroupId);
}); });
it('ttl of 1 minute', () => { it('ttl of 1 minute', () => {

Loading…
Cancel
Save