-
Notifications
You must be signed in to change notification settings - Fork 78
/
ResultLinkTest.ts
449 lines (372 loc) · 17.8 KB
/
ResultLinkTest.ts
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import * as Mock from '../MockEnvironment';
import { ResultLink } from '../../src/ui/ResultLink/ResultLink';
import { IQueryResult } from '../../src/rest/QueryResult';
import { HighlightUtils } from '../../src/utils/HighlightUtils';
import { $$ } from '../../src/utils/Dom';
import { IResultLinkOptions } from '../../src/ui/ResultLink/ResultLinkOptions';
import { analyticsActionCauseList } from '../../src/ui/Analytics/AnalyticsActionListMeta';
import { OS_NAME } from '../../src/utils/OSUtils';
import { FakeResults } from '../Fake';
import { Initialization } from '../../src/Core';
export function ResultLinkTest() {
describe('ResultLink', function () {
let test: Mock.IBasicComponentSetup<ResultLink>;
let fakeResult: IQueryResult;
function buildFakeResult(): IQueryResult {
let fakeResult = FakeResults.createFakeResult();
fakeResult.title = 'A test title';
fakeResult.titleHighlights = [{ offset: 2, length: 4 }];
fakeResult.clickUri = 'http://www.coveo.com';
return fakeResult;
}
function initResultLink() {
test = Mock.advancedResultComponentSetup<ResultLink>(ResultLink, fakeResult, undefined);
}
function htmlContainingXSS() {
return '<IMG SRC=/ onerror="alert(String.fromCharCode(88,83,83))"></img>';
}
beforeEach(() => {
fakeResult = buildFakeResult();
initResultLink();
spyOn(test.cmp, 'openLink');
spyOn(window, 'open');
});
afterEach(function () {
test = null;
fakeResult = null;
});
it('should have its tabindex value set to 0', () => {
expect(test.cmp.element.getAttribute('tabindex')).toBe('0');
});
describe(`when the template contains two ${ResultLink.ID} elements`, () => {
let template: HTMLElement;
function buildResultWithTwoResultLinks() {
const result = $$('div', { className: 'CoveoResult' }).el;
$$(result).append(buildResultLink());
$$(result).append(buildResultLink());
return result;
}
function buildResultLink() {
return $$('div', { className: 'CoveoResultLink' }).el;
}
beforeEach(() => {
template = buildResultWithTwoResultLinks();
const result = FakeResults.createFakeResult();
Initialization.automaticallyCreateComponentsInsideResult(template, result);
});
});
it('should hightlight the result title', () => {
expect(test.cmp.element.innerHTML).toEqual(
HighlightUtils.highlightString(fakeResult.title, fakeResult.titleHighlights, null, 'coveo-highlight')
);
});
it(`when the title contains a html element, it does not render the element to prevent XSS`, () => {
fakeResult.title = htmlContainingXSS();
initResultLink();
expect(test.cmp.element.innerHTML).toBe(
'<I<span class="coveo-highlight">MG S</span>RC=/ onerror="alert(String.fromCharCode(88,83,83))"></img>'
);
});
it('should set the title attribute to the result title when no options are provided', () => {
expect(test.cmp.element.title).toEqual(fakeResult.title);
});
it('should contain the clickUri if the result has no title', () => {
fakeResult.title = undefined;
fakeResult.clickUri = 'https://www.google.com?q=hello&geo=world';
initResultLink();
const encodedUri = fakeResult.clickUri.replace('&', '&');
expect(test.cmp.element.innerHTML).toEqual(encodedUri);
});
it(`when the title is empty and the clickuri contains a html element,
it does not render the element to prevent XSS`, () => {
fakeResult.title = '';
fakeResult.clickUri = htmlContainingXSS();
initResultLink();
expect(test.cmp.element.children.length).toBe(0);
expect(test.cmp.element.innerHTML).toBe('<IMG SRC=/ onerror="alert(String.fromCharCode(88,83,83))"></img>');
});
it('can receive an onClick option to execute', done => {
test = Mock.advancedResultComponentSetup<ResultLink>(
ResultLink,
fakeResult,
new Mock.AdvancedComponentSetupOptions($$('div').el, {
onClick: () => {
expect(true).toBe(true);
done();
}
})
);
$$(test.cmp.element).trigger('click');
});
it('sends an analytic event on click', () => {
$$(test.cmp.element).trigger('click');
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledTimes(1);
});
describe('exposes hrefTemplate', () => {
it('when the href starts with a protocol that enables XSS, it returns an empty string', () => {
const hrefTemplate = 'javascript:alert(1)';
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { hrefTemplate: hrefTemplate }, fakeResult);
test.cmp.openLinkInNewWindow();
expect(window.open).toHaveBeenCalledWith('', jasmine.anything());
});
it('should not modify the href template if there are no field specified', () => {
let hrefTemplate = 'http://test';
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { hrefTemplate: hrefTemplate }, fakeResult);
test.cmp.openLinkInNewWindow();
expect(window.open).toHaveBeenCalledWith(hrefTemplate, jasmine.anything());
});
it('should replace fields in the href template by the results equivalent', () => {
let hrefTemplate = 'http://${title}';
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { hrefTemplate: hrefTemplate }, fakeResult);
test.cmp.openLinkInNewWindow();
expect(window.open).toHaveBeenCalledWith(`http://${fakeResult.title}`, jasmine.anything());
});
it('should support nested values in result', () => {
let hrefTemplate = 'http://${raw.number}';
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { hrefTemplate: hrefTemplate }, fakeResult);
test.cmp.openLinkInNewWindow();
expect(window.open).toHaveBeenCalledWith(`http://${fakeResult.raw['number'].toString()}`, jasmine.anything());
});
it('should not parse standalone accolades', () => {
let hrefTemplate = 'http://${raw.number}{test}';
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { hrefTemplate: hrefTemplate }, fakeResult);
test.cmp.openLinkInNewWindow();
expect(window.open).toHaveBeenCalledWith(`http://${fakeResult.raw['number']}{test}`, jasmine.anything());
});
it('should support external fields', () => {
window['Coveo']['test'] = 'testExternal';
let hrefTemplate = 'http://${Coveo.test}';
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { hrefTemplate: hrefTemplate }, fakeResult);
test.cmp.openLinkInNewWindow();
expect(window.open).toHaveBeenCalledWith('http://testExternal', jasmine.anything());
window['Coveo']['test'] = undefined;
});
it('should support nested external fields with more than 2 keys', () => {
window['Coveo']['test'] = { key: 'testExternal' };
let hrefTemplate = 'http://${Coveo.test.key}';
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { hrefTemplate: hrefTemplate }, fakeResult);
test.cmp.openLinkInNewWindow();
expect(window.open).toHaveBeenCalledWith('http://testExternal', jasmine.anything());
window['Coveo']['test'] = undefined;
});
});
describe('exposes the titleTemplate', () => {
let titleTemplate = '';
afterEach(() => {
titleTemplate = '';
});
function initResultLinkWithTitleTemplate() {
test = Mock.optionsResultComponentSetup<ResultLink, IResultLinkOptions>(ResultLink, { titleTemplate }, fakeResult);
}
it('should set the title attribute to the text version of the displayed title', () => {
titleTemplate = 'foo ${clickUri}';
initResultLinkWithTitleTemplate();
expect(test.cmp.element.title).toEqual(`foo ${fakeResult.clickUri}`);
});
it('should replaces fields in the title template by the results equivalent', () => {
titleTemplate = '${clickUri}';
initResultLinkWithTitleTemplate();
expect($$(test.cmp.element).text()).toEqual(fakeResult.clickUri);
});
it(`when the field referenced in the title template contains XSS html,
it escapes the html to prevent XSS`, () => {
titleTemplate = '${clickUri}';
fakeResult.clickUri = htmlContainingXSS();
initResultLinkWithTitleTemplate();
expect(test.cmp.element.innerHTML).toEqual('<IMG SRC=/ onerror="alert(String.fromCharCode(88,83,83))"></img>');
});
it('should support nested values in result', () => {
titleTemplate = '${raw.number}';
initResultLinkWithTitleTemplate();
expect(test.cmp.element.innerHTML).toEqual(fakeResult.raw['number'].toString());
});
it('should not parse standalone accolades', () => {
titleTemplate = '${raw.number}{test}';
initResultLinkWithTitleTemplate();
expect(test.cmp.element.innerHTML).toEqual(fakeResult.raw['number'].toString() + '{test}');
});
it('should support external fields', () => {
window['Coveo']['test'] = 'testExternal';
titleTemplate = '${Coveo.test}';
initResultLinkWithTitleTemplate();
expect(test.cmp.element.innerHTML).toEqual('testExternal');
window['Coveo']['test'] = undefined;
});
it('should support external fields with more than 2 keys', () => {
window['Coveo']['test'] = { key: 'testExternal' };
titleTemplate = '${Coveo.test.key}';
initResultLinkWithTitleTemplate();
expect(test.cmp.element.innerHTML).toEqual('testExternal');
window['Coveo']['test'] = undefined;
});
it('should print the title if the option is set but the template is empty', () => {
titleTemplate = '';
initResultLinkWithTitleTemplate();
expect($$(test.cmp.element).text()).toEqual(fakeResult.title);
});
it('should print the template if the key used in the template is undefined', () => {
titleTemplate = '${doesNotExist}';
initResultLinkWithTitleTemplate();
expect($$(test.cmp.element).text()).toEqual('${doesNotExist}');
});
});
it('sends an analytics event on context menu', () => {
$$(test.cmp.element).trigger('contextmenu');
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledTimes(1);
});
it('sends an analytics event on mouseup', () => {
$$(test.cmp.element).trigger('mouseup');
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledTimes(1);
});
it('sends an analytics event on mousedown', () => {
$$(test.cmp.element).trigger('mousedown');
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledTimes(1);
});
it('does not send multiple analytics events with multiple mouse events', () => {
$$(test.cmp.element).trigger('mousedown');
$$(test.cmp.element).trigger('click');
$$(test.cmp.element).trigger('mouseup');
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledTimes(1);
});
it('sends an event 1s after a long press on mobile', done => {
$$(test.cmp.element).trigger('touchstart');
expect(test.cmp.usageAnalytics.logClickEvent).not.toHaveBeenCalled();
setTimeout(() => {
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledTimes(1);
done();
}, 1100);
});
it('does not send an event if a touchend event occurs before a 1s delay', done => {
$$(test.cmp.element).trigger('touchstart');
setTimeout(() => {
$$(test.cmp.element).trigger('touchend');
}, 300);
setTimeout(() => {
expect(test.cmp.usageAnalytics.logClickEvent).not.toHaveBeenCalled();
done();
}, 1100);
});
describe('when logging the analytic event', () => {
it('should use the href if set', () => {
let element = $$('a');
let href = 'javascript:void(0)';
element.setAttribute('href', href);
test = Mock.advancedResultComponentSetup<ResultLink>(ResultLink, fakeResult, new Mock.AdvancedComponentSetupOptions(element.el));
spyOn(test.cmp, 'openLink');
$$(test.cmp.element).trigger('click');
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledWith(
analyticsActionCauseList.documentOpen,
jasmine.objectContaining({ documentURL: href }),
fakeResult,
test.cmp.root
);
});
it('should use the clickUri if the href is empty', () => {
$$(test.cmp.element).trigger('click');
expect(test.cmp.usageAnalytics.logClickEvent).toHaveBeenCalledWith(
analyticsActionCauseList.documentOpen,
jasmine.objectContaining({ documentURL: fakeResult.clickUri }),
fakeResult,
test.cmp.root
);
});
});
describe('when the element is a hyperlink', () => {
function initHyperLink(options?: IResultLinkOptions) {
test = Mock.advancedResultComponentSetup<ResultLink>(
ResultLink,
fakeResult,
new Mock.AdvancedComponentSetupOptions($$('a').el, options)
);
}
beforeEach(() => {
initHyperLink();
});
it('should set the href to the result click uri', () => {
expect(test.cmp.element.getAttribute('href')).toEqual(fakeResult.clickUri);
});
it(`when the href contains "&" characters,
should set the href the unescaped the result click uri`, () => {
fakeResult.clickUri =
'https://testing.com/supportcenter/portal?DataSource=Solutions&eventSubmit_doNavigate=&PageNumber=Prev&tab=Solutions&ts=6';
initHyperLink();
expect(test.cmp.element.getAttribute('href')).toEqual(fakeResult.clickUri);
});
it('when the clickUri is a relative url (starts with slash), it sets the href to the uri', () => {
fakeResult.clickUri = '/casemgmt/sc_KnowledgeArticle?sfdcid=ka32C0000009t9CQAQ&type=Solution';
initHyperLink();
expect(test.cmp.element.getAttribute('href')).toEqual(fakeResult.clickUri);
});
it('when the clickUri is a string containing but not starting with a slash, it sets the href to an empty string', () => {
fakeResult.clickUri = 'casemgmt/sc_KnowledgeArticle';
initHyperLink();
expect(test.cmp.element.getAttribute('href')).toEqual('');
});
it(`when the uri (clickUri) defined in the results contains the javascript protocol,
it clears the value to prevent XSS`, () => {
fakeResult.clickUri = 'JavaScript:void(0)';
initHyperLink();
expect(test.cmp.element.getAttribute('href')).toEqual('');
});
it(`when the field option is defined and the field contains the javascript protocol,
it clears the value to prevent XSS`, () => {
fakeResult.raw['test'] = 'javascript:void(0)';
initHyperLink({ field: '@test' });
expect(test.cmp.element.getAttribute('href')).toEqual('');
});
it('should not override the href if it is set before the initialization', () => {
let element = $$('a');
let href = 'javascript:void(0)';
element.setAttribute('href', href);
test = Mock.advancedResultComponentSetup<ResultLink>(ResultLink, fakeResult, new Mock.AdvancedComponentSetupOptions(element.el));
expect(test.cmp.element.getAttribute('href')).toEqual(href);
});
describe('and the result has the outlookfield', () => {
beforeEach(() => {
fakeResult.raw['outlookuri'] = 'mailto:test@coveo.comm';
fakeResult.raw['outlookformacuri'] = 'mailto:test.mac@coveo.comm';
});
it('should generate the correct href if the os is windows and the option is openInOutlook', () => {
test = Mock.advancedResultComponentSetup<ResultLink>(
ResultLink,
fakeResult,
new Mock.AdvancedComponentSetupOptions($$('a').el, { openInOutlook: true }, (env: Mock.MockEnvironmentBuilder) => {
return env.withOs(OS_NAME.WINDOWS);
})
);
expect(test.cmp.element.getAttribute('href')).toEqual(fakeResult.raw['outlookuri']);
});
it('should generate the correct href if the os is windows and the option is not openInOutlook', () => {
test = Mock.advancedResultComponentSetup<ResultLink>(
ResultLink,
fakeResult,
new Mock.AdvancedComponentSetupOptions($$('a').el, { openInOutlook: false }, (env: Mock.MockEnvironmentBuilder) => {
return env.withOs(OS_NAME.WINDOWS);
})
);
expect(test.cmp.element.getAttribute('href')).not.toEqual(fakeResult.raw['outlookuri']);
});
it('should generate the correct href if the os is mac and the option is openInOutlook', () => {
test = Mock.advancedResultComponentSetup<ResultLink>(
ResultLink,
fakeResult,
new Mock.AdvancedComponentSetupOptions($$('a').el, { openInOutlook: true }, (env: Mock.MockEnvironmentBuilder) => {
return env.withOs(OS_NAME.MACOSX);
})
);
expect(test.cmp.element.getAttribute('href')).toEqual(fakeResult.raw['outlookformacuri']);
});
it('should generate the correct href if the os is mac and the option is not openInOutlook', () => {
test = Mock.advancedResultComponentSetup<ResultLink>(
ResultLink,
fakeResult,
new Mock.AdvancedComponentSetupOptions($$('a').el, { openInOutlook: false }, (env: Mock.MockEnvironmentBuilder) => {
return env.withOs(OS_NAME.MACOSX);
})
);
expect(test.cmp.element.getAttribute('href')).not.toEqual(fakeResult.raw['outlookformacuri']);
});
});
});
});
}