|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { v4 } from 'uuid'; |
| 3 | + |
| 4 | +import { SignalService } from '../../../../protobuf'; |
| 5 | +import { Constants } from '../../../../session'; |
| 6 | +import { MessageRequestResponse } from '../../../../session/messages/outgoing/controlMessage/MessageRequestResponse'; |
| 7 | +// tslint:disable: no-unused-expression |
| 8 | + |
| 9 | +// tslint:disable-next-line: max-func-body-length |
| 10 | +describe('MessageRequestResponse', () => { |
| 11 | + let message: MessageRequestResponse | undefined; |
| 12 | + it('correct ttl', () => { |
| 13 | + message = new MessageRequestResponse({ |
| 14 | + timestamp: Date.now(), |
| 15 | + }); |
| 16 | + |
| 17 | + expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.TTL_MAX); |
| 18 | + }); |
| 19 | + |
| 20 | + it('has an identifier', () => { |
| 21 | + message = new MessageRequestResponse({ |
| 22 | + timestamp: Date.now(), |
| 23 | + }); |
| 24 | + |
| 25 | + expect(message.identifier).to.not.equal(null, 'identifier cannot be null'); |
| 26 | + expect(message.identifier).to.not.equal(undefined, 'identifier cannot be undefined'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('has an identifier matching if given', () => { |
| 30 | + const identifier = v4(); |
| 31 | + message = new MessageRequestResponse({ |
| 32 | + timestamp: Date.now(), |
| 33 | + identifier, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(message.identifier).to.not.equal(identifier, 'identifier should match'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('isApproved is always true', () => { |
| 40 | + message = new MessageRequestResponse({ |
| 41 | + timestamp: Date.now(), |
| 42 | + }); |
| 43 | + const plainText = message.plainTextBuffer(); |
| 44 | + const decoded = SignalService.Content.decode(plainText); |
| 45 | + expect(decoded.messageRequestResponse) |
| 46 | + .to.have.property('isApproved') |
| 47 | + .to.be.eq(true, 'isApproved is true'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('can create response without lokiProfile', () => { |
| 51 | + message = new MessageRequestResponse({ |
| 52 | + timestamp: Date.now(), |
| 53 | + }); |
| 54 | + const plainText = message.plainTextBuffer(); |
| 55 | + const decoded = SignalService.Content.decode(plainText); |
| 56 | + expect(decoded.messageRequestResponse) |
| 57 | + .to.have.property('profile') |
| 58 | + .to.be.eq(null, 'no profile field if no profile given'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('can create response with display name only', () => { |
| 62 | + message = new MessageRequestResponse({ |
| 63 | + timestamp: Date.now(), |
| 64 | + lokiProfile: { displayName: 'Jane', profileKey: null }, |
| 65 | + }); |
| 66 | + const plainText = message.plainTextBuffer(); |
| 67 | + const decoded = SignalService.Content.decode(plainText); |
| 68 | + |
| 69 | + expect(decoded.messageRequestResponse?.profile?.displayName).to.be.deep.eq('Jane'); |
| 70 | + expect(decoded.messageRequestResponse?.profile?.profilePicture).to.be.empty; |
| 71 | + expect(decoded.messageRequestResponse?.profileKey).to.be.empty; |
| 72 | + }); |
| 73 | + |
| 74 | + it('empty profileKey does not get included', () => { |
| 75 | + message = new MessageRequestResponse({ |
| 76 | + timestamp: Date.now(), |
| 77 | + lokiProfile: { displayName: 'Jane', profileKey: new Uint8Array(0) }, |
| 78 | + }); |
| 79 | + const plainText = message.plainTextBuffer(); |
| 80 | + const decoded = SignalService.Content.decode(plainText); |
| 81 | + |
| 82 | + expect(decoded.messageRequestResponse?.profile?.displayName).to.be.eq('Jane'); |
| 83 | + |
| 84 | + expect(decoded.messageRequestResponse?.profile?.profilePicture).to.be.empty; |
| 85 | + expect(decoded.messageRequestResponse?.profileKey).to.be.empty; |
| 86 | + }); |
| 87 | + |
| 88 | + it('can create response with display name and profileKey and profileImage', () => { |
| 89 | + message = new MessageRequestResponse({ |
| 90 | + timestamp: Date.now(), |
| 91 | + lokiProfile: { |
| 92 | + displayName: 'Jane', |
| 93 | + profileKey: new Uint8Array([1, 2, 3, 4, 5, 6]), |
| 94 | + avatarPointer: 'https://somevalidurl.com', |
| 95 | + }, |
| 96 | + }); |
| 97 | + const plainText = message.plainTextBuffer(); |
| 98 | + const decoded = SignalService.Content.decode(plainText); |
| 99 | + |
| 100 | + expect(decoded.messageRequestResponse?.profile?.displayName).to.be.deep.eq('Jane'); |
| 101 | + |
| 102 | + expect(decoded.messageRequestResponse?.profileKey).to.be.not.empty; |
| 103 | + |
| 104 | + if (!decoded.messageRequestResponse?.profileKey?.buffer) { |
| 105 | + throw new Error('decoded.messageRequestResponse?.profileKey?.buffer should be set'); |
| 106 | + } |
| 107 | + expect(decoded.messageRequestResponse?.profile?.profilePicture).to.be.eq( |
| 108 | + 'https://somevalidurl.com' |
| 109 | + ); |
| 110 | + // don't ask me why deep.eq ([1,2,3, ...]) gives nothing interesting but a 8192 buffer not matching |
| 111 | + expect(decoded.messageRequestResponse?.profileKey.length).to.be.eq(6); |
| 112 | + expect(decoded.messageRequestResponse?.profileKey[0]).to.be.eq(1); |
| 113 | + expect(decoded.messageRequestResponse?.profileKey[1]).to.be.eq(2); |
| 114 | + expect(decoded.messageRequestResponse?.profileKey[2]).to.be.eq(3); |
| 115 | + expect(decoded.messageRequestResponse?.profileKey[3]).to.be.eq(4); |
| 116 | + expect(decoded.messageRequestResponse?.profileKey[4]).to.be.eq(5); |
| 117 | + expect(decoded.messageRequestResponse?.profileKey[5]).to.be.eq(6); |
| 118 | + }); |
| 119 | + |
| 120 | + it('profileKey not included if profileUrl not set', () => { |
| 121 | + message = new MessageRequestResponse({ |
| 122 | + timestamp: Date.now(), |
| 123 | + lokiProfile: { displayName: 'Jane', profileKey: new Uint8Array([1, 2, 3, 4, 5, 6]) }, |
| 124 | + }); |
| 125 | + const plainText = message.plainTextBuffer(); |
| 126 | + const decoded = SignalService.Content.decode(plainText); |
| 127 | + |
| 128 | + expect(decoded.messageRequestResponse?.profile?.displayName).to.be.deep.eq('Jane'); |
| 129 | + |
| 130 | + if (!decoded.messageRequestResponse?.profileKey?.buffer) { |
| 131 | + throw new Error('decoded.messageRequestResponse?.profileKey?.buffer should be set'); |
| 132 | + } |
| 133 | + |
| 134 | + expect(decoded.messageRequestResponse?.profile?.profilePicture).to.be.empty; |
| 135 | + expect(decoded.messageRequestResponse?.profileKey).to.be.empty; |
| 136 | + }); |
| 137 | + |
| 138 | + it('url not included if profileKey not set', () => { |
| 139 | + message = new MessageRequestResponse({ |
| 140 | + timestamp: Date.now(), |
| 141 | + lokiProfile: { |
| 142 | + displayName: 'Jane', |
| 143 | + profileKey: null, |
| 144 | + avatarPointer: 'https://somevalidurl.com', |
| 145 | + }, |
| 146 | + }); |
| 147 | + const plainText = message.plainTextBuffer(); |
| 148 | + const decoded = SignalService.Content.decode(plainText); |
| 149 | + |
| 150 | + expect(decoded.messageRequestResponse?.profile?.displayName).to.be.deep.eq('Jane'); |
| 151 | + |
| 152 | + if (!decoded.messageRequestResponse?.profileKey?.buffer) { |
| 153 | + throw new Error('decoded.messageRequestResponse?.profileKey?.buffer should be set'); |
| 154 | + } |
| 155 | + |
| 156 | + expect(decoded.messageRequestResponse?.profile?.displayName).to.be.eq('Jane'); |
| 157 | + expect(decoded.messageRequestResponse?.profile?.profilePicture).to.be.empty; |
| 158 | + expect(decoded.messageRequestResponse?.profileKey).to.be.empty; |
| 159 | + }); |
| 160 | +}); |
0 commit comments