-
Notifications
You must be signed in to change notification settings - Fork 290
/
EventMapperSpec.js
286 lines (240 loc) · 12.8 KB
/
EventMapperSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Wire
* Copyright (C) 2018 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/
import {Article, LinkPreview, Mention} from '@wireapp/protocol-messaging';
import {createRandomUuid, arrayToBase64} from 'Util/util';
import {ValidationUtilError} from 'Util/ValidationUtil';
import {Conversation} from 'src/script/entity/Conversation';
import {EventMapper} from 'src/script/conversation/EventMapper';
import {AssetType} from 'src/script/assets/AssetType';
import {ClientEvent} from 'src/script/event/Client';
import {MentionEntity} from 'src/script/message/MentionEntity';
describe('Event Mapper', () => {
const test_factory = new TestFactory();
let conversation_et = null;
let event_mapper = null;
beforeAll(() => {
return test_factory.exposeUserActors().then(() => {
wire.app = {
service: {
asset: TestFactory.asset_service,
},
};
});
});
beforeEach(() => {
conversation_et = new Conversation(createRandomUuid());
event_mapper = new EventMapper();
});
describe('mapJsonEvent', () => {
it('maps text messages without link previews', () => {
const event_id = createRandomUuid();
const event = {
conversation: conversation_et.id,
data: {
content: 'foo',
nonce: event_id,
},
from: createRandomUuid,
id: event_id,
time: new Date().toISOString(),
type: ClientEvent.CONVERSATION.MESSAGE_ADD,
};
return event_mapper.mapJsonEvent(event, conversation_et).then(messageEntity => {
expect(messageEntity.get_first_asset().text).toBe(event.data.content);
expect(messageEntity).toBeDefined();
});
});
it('maps text messages with deprecated link preview format', async () => {
const event_id = createRandomUuid();
const article = new Article({
permanentUrl: 'test.com',
summary: 'Test description',
title: 'Test title',
});
const link_preview = new LinkPreview({
article,
url: 'test.com',
urlOffset: 0,
});
const base64LinkPreview = await arrayToBase64(LinkPreview.encode(link_preview).finish());
const event = {
conversation: conversation_et.id,
data: {
content: 'test.com',
nonce: event_id,
previews: [base64LinkPreview],
},
from: createRandomUuid,
id: event_id,
time: new Date().toISOString(),
type: ClientEvent.CONVERSATION.MESSAGE_ADD,
};
const messageEntity = await event_mapper.mapJsonEvent(event, conversation_et);
expect(messageEntity.get_first_asset().text).toBe(event.data.content);
expect(messageEntity.get_first_asset().previews().length).toBe(1);
expect(messageEntity.get_first_asset().previews()[0].url).toBe('test.com');
expect(messageEntity).toBeDefined();
});
it('maps text messages with link preview', async () => {
const event_id = createRandomUuid();
const link_preview = new LinkPreview({
article: null,
permanentUrl: 'test.com/perm',
summary: 'Test description',
title: 'Test title',
url: 'test.com',
urlOffset: 0,
});
const base64Preview = await arrayToBase64(LinkPreview.encode(link_preview).finish());
const event = {
conversation: conversation_et.id,
data: {
content: 'test.com',
nonce: event_id,
previews: [base64Preview],
},
from: createRandomUuid,
id: event_id,
time: new Date().toISOString(),
type: ClientEvent.CONVERSATION.MESSAGE_ADD,
};
const messageEntity = await event_mapper.mapJsonEvent(event, conversation_et);
expect(messageEntity.get_first_asset().text).toBe(event.data.content);
expect(messageEntity.get_first_asset().previews().length).toBe(1);
expect(messageEntity.get_first_asset().previews()[0].url).toBe(link_preview.url);
expect(messageEntity).toBeDefined();
});
it('maps v3 image asset', () => {
// prettier-ignore
/* eslint-disable comma-spacing, key-spacing, sort-keys, quotes */
const event = {"conversation":"6db80f23-fbdf-416f-a123-d279aebff011","from":"9b47476f-974d-481c-af64-13f82ed98a5f","id":"72554b6b-edc3-4dde-a177-5552df09df43","status":1,"time":"2017-05-18T10:32:22.639Z","data":{"content_length":47527,"content_type":"image/jpeg","info":{"height":905,"nonce":"72554b6b-edc3-4dde-a177-5552df09df43","tag":"medium","width":1448},"key":"3-2-dc080e34-72d8-478e-a1bf-fdda44b47872","otr_key":{"0":172,"1":208,"2":234,"3":12,"4":213,"5":105,"6":120,"7":152,"8":41,"9":86,"10":136,"11":107,"12":217,"13":221,"14":198,"15":195,"16":216,"17":152,"18":19,"19":101,"20":192,"21":57,"22":94,"23":22,"24":206,"25":120,"26":95,"27":216,"28":132,"29":190,"30":94,"31":213},"sha256":{"0":24,"1":71,"2":123,"3":151,"4":230,"5":255,"6":224,"7":109,"8":58,"9":157,"10":152,"11":216,"12":196,"13":127,"14":101,"15":137,"16":68,"17":10,"18":56,"19":35,"20":77,"21":223,"22":124,"23":26,"24":96,"25":142,"26":171,"27":208,"28":4,"29":12,"30":118,"31":26},"token":"aV0TGxF3ugpawm3wAYPmew=="},"type":"conversation.asset-add","category":128,"primary_key":94};
/* eslint-enable comma-spacing, key-spacing, sort-keys, quotes */
return event_mapper.mapJsonEvent(event, conversation_et).then(messageEntity => {
expect(messageEntity.get_first_asset().width).toBe(event.data.info.width);
expect(messageEntity.get_first_asset().height).toBe(event.data.info.height);
expect(messageEntity.get_first_asset().file_size).toBe(event.data.content_length);
expect(messageEntity.get_first_asset().file_type).toBe(event.data.content_type);
expect(messageEntity.get_first_asset().type).toBe(AssetType.IMAGE);
expect(messageEntity.get_first_asset().resource().otrKey).toBe(event.data.otr_key);
expect(messageEntity.get_first_asset().resource().sha256).toBe(event.data.sha256);
expect(messageEntity).toBeDefined();
});
});
it('skips messages which cannot be mapped', () => {
// prettier-ignore
/* eslint-disable comma-spacing, key-spacing, sort-keys, quotes */
const good_message = {"conversation":conversation_et.id,"id":"4cec0f75-d963-486d-9401-415240ac2ad8","from":"532af01e-1e24-4366-aacf-33b67d4ee376","time":"2016-08-04T15:12:12.453Z","data":{"content":"Message with timestamp","nonce":"4cec0f75-d963-486d-9401-415240ac2ad8","previews":[]},"type":"conversation.message-add"};
// prettier-ignore
const bad_message = {"conversation":conversation_et.id,"id":"aeac8355-739b-4dfc-a119-891a52c6a8dc","from":"532af01e-1e24-4366-aacf-33b67d4ee376","data":{"content":"Knock, are you there? :)","nonce":"aeac8355-739b-4dfc-a119-891a52c6a8dc"},"type":"conversation.message-add"};
/* eslint-enable comma-spacing, key-spacing, sort-keys, quotes */
return event_mapper.mapJsonEvents([good_message, bad_message], conversation_et).then(messageEntities => {
expect(messageEntities.length).toBe(1);
});
});
it('filters mentions that are out of range', async () => {
const mandy = '@Mandy';
const randy = '@Randy';
const text = `Hi ${mandy} and ${randy}.`;
const validMention = new MentionEntity(text.indexOf('@'), mandy.length, createRandomUuid());
const outOfRangeMention = new MentionEntity(text.length, randy.length, createRandomUuid());
const conversationEntity = new Conversation(createRandomUuid());
const mentionArrays = await Promise.all([
arrayToBase64(Mention.encode(validMention.toProto()).finish()),
arrayToBase64(Mention.encode(outOfRangeMention.toProto()).finish()),
]);
const event = {
category: 16,
conversation: conversationEntity.id,
data: {
content: text,
mentions: mentionArrays,
previews: [],
},
from: createRandomUuid(),
id: createRandomUuid(),
primary_key: 5,
time: '2018-09-27T15:23:14.177Z',
type: 'conversation.message-add',
};
const messageEntity = await event_mapper.mapJsonEvent(event, conversationEntity);
const mentions = messageEntity.get_first_asset().mentions();
expect(mentions.length).toBe(1);
});
it('filters mentions that are overlapping', async () => {
const mandy = '@Mandy';
const randy = '@Randy';
const sandy = '@Sandy';
const text = `Hi ${mandy}, ${randy} and ${sandy}.`;
const mandyStart = text.indexOf(mandy);
const sandyStart = text.indexOf(sandy);
const validMention1 = new MentionEntity(mandyStart, mandy.length, createRandomUuid());
const validMention2 = new MentionEntity(sandyStart, sandy.length, createRandomUuid());
const overlappingStart = mandyStart + mandy.length - 1;
const overlappingMention = new MentionEntity(overlappingStart, randy.length, createRandomUuid());
const conversationEntity = new Conversation(createRandomUuid());
const mentionArrays = await Promise.all([
arrayToBase64(Mention.encode(validMention1.toProto()).finish()),
arrayToBase64(Mention.encode(overlappingMention.toProto()).finish()),
arrayToBase64(Mention.encode(validMention2.toProto()).finish()),
]);
const event = {
category: 16,
conversation: conversationEntity.id,
data: {
content: text,
mentions: mentionArrays,
previews: [],
},
from: createRandomUuid(),
id: createRandomUuid(),
primary_key: 5,
time: '2018-09-27T15:23:14.177Z',
type: 'conversation.message-add',
};
const messageEntity = await event_mapper.mapJsonEvent(event, conversationEntity);
const mentions = messageEntity.get_first_asset().mentions();
expect(mentions.length).toBe(2);
});
});
describe('_mapAssetImage', () => {
it('detects a malformed asset key', () => {
// prettier-ignore
/* eslint-disable comma-spacing, key-spacing, sort-keys, quotes */
const event = {"conversation":"61350a90-e522-4ee5-90b7-f55b648e34da","from":"532af01e-1e24-4366-aacf-33b67d4ee376","id":"51b8e5c5-4088-4177-a5ad-b001fef11eac","status":1,"time":"2017-08-16T16:13:01.168Z","data":{"content_length":73029,"content_type":"image/jpeg","info":{"name":null,"nonce":"51b8e5c5-4088-4177-a5ad-b001fef11eac","height":448,"width":588,"tag":"medium"},"key":"../../../search/contacts","otr_key":{"0":130,"1":255,"2":81,"3":125,"4":202,"5":165,"6":197,"7":175,"8":79,"9":18,"10":2,"11":194,"12":160,"13":122,"14":173,"15":82,"16":36,"17":77,"18":50,"19":186,"20":246,"21":54,"22":36,"23":235,"24":81,"25":19,"26":179,"27":69,"28":127,"29":113,"30":248,"31":74},"sha256":{"0":49,"1":137,"2":43,"3":166,"4":242,"5":222,"6":42,"7":96,"8":44,"9":22,"10":37,"11":35,"12":87,"13":175,"14":205,"15":157,"16":225,"17":173,"18":63,"19":43,"20":133,"21":197,"22":115,"23":195,"24":142,"25":44,"26":98,"27":222,"28":75,"29":56,"30":159,"31":66},"status":"uploaded","token":"hYQytxHS6hSP6DlemD13uQ==&size=100&q=test"},"type":"conversation.asset-add","category":128,"primary_key":6};
/* eslint-enable comma-spacing, key-spacing, sort-keys, quotes */
const asset_et = event_mapper._mapAssetImage(event, true);
try {
asset_et.resource().generateUrl();
} catch (error) {
expect(error).toEqual(jasmine.any(ValidationUtilError));
}
});
});
describe('_mapEventUnableToDecrypt', () => {
it('maps a message from a decrypt error event', () => {
// prettier-ignore
/* eslint-disable comma-spacing, key-spacing, sort-keys, quotes */
const event = {"category": 0, "conversation": "fb1c051a-3ce3-46c5-bbc2-0153b6076af0", "error": "We received a message with session tag 'a8859a310a0c374a3da67e3a0f871145', but we don't have a session for this tag. (c0a70d96aaeb87b6)", "error_code": "205 (c0a70d96aaeb87b6)", "from": "2bde49aa-bdb5-458f-98cf-7d3552b10916", "id": "cb4972e0-9586-42a2-90cc-1798ec0cb648", "primary_key": 9, "time": "2017-04-03T12:58:04.301Z", "type": "conversation.unable-to-decrypt"};
/* eslint-enable comma-spacing, key-spacing, sort-keys, quotes */
const message_et = event_mapper._mapEventUnableToDecrypt(event);
expect(message_et.error_code).toBe('205');
expect(message_et.client_id).toBe('c0a70d96aaeb87b6');
});
});
});