/
track.test.js
336 lines (306 loc) · 12.3 KB
/
track.test.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
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
/* globals isolate */
describe('ravelin.track', function() {
var r;
// afterEach(cleanup) seems to invoke cleanup() after tests return, which is
// before async tests actually complete.
beforeEach(cleanup);
after(cleanup);
function cleanup() {
xhook.destroy();
if (r) {
r.track._detach();
r = null;
}
}
describe('#load', function() {
it('sends upon instantiation', function(done) {
var key = this.test.fullTitle();
xhook.before(function(req) {
if (!keysMatch(req, key)) return {status: 204};
r.core.ids().then(function(ids) {
var loadEvent = JSON.parse(req.body).events[0];
expect(loadEvent).to.have.property('eventType', 'track');
expect(loadEvent).to.have.property('libVer', '1.3.0-ravelinjs');
expect(loadEvent.eventData).to.eql({eventName: 'PAGE_LOADED'});
expect(loadEvent.eventMeta.trackingSource).to.be('browser');
expect(loadEvent.eventMeta.ravelinDeviceId).to.be(ids.device);
expect(loadEvent.eventMeta.ravelinSessionId).to.be(ids.session);
}).then(done, done);
return {status: 204};
});
r = new Ravelin(isolate({key: key, api: '/'}));
});
});
describe('#event', function() {
it('sends custom events', function(done) {
var key = this.test.fullTitle();
xhook.before(function(req) {
if (!keysMatch(req, key)) return {status: 204};
r.core.ids().then(function(ids) {
var e = JSON.parse(req.body).events[0];
expect(e).to.have.property('eventType', 'track');
expect(e).to.have.property('libVer', '1.3.0-ravelinjs');
expect(e.eventData).to.eql({eventName: 'custom-event'});
expect(e.eventMeta.trackingSource).to.be('browser');
expect(e.eventMeta.ravelinDeviceId).to.be(ids.device);
expect(e.eventMeta.ravelinSessionId).to.be(ids.session);
}).then(done, done);
return {status: 204};
});
r = new Ravelin(isolate({key: key, api: '/', init: false}));
r.track.event('custom-event');
});
it('sends custom events with properties', function(done) {
var key = this.test.fullTitle();
xhook.before(function(req) {
if (!keysMatch(req, key)) return {status: 204};
r.core.ids().then(function(ids) {
var e = JSON.parse(req.body).events[0];
expect(e).to.have.property('eventType', 'track');
expect(e).to.have.property('libVer', '1.3.0-ravelinjs');
expect(e.eventData).to.eql({eventName: 'custom-event', properties: {extra: true}});
expect(e.eventMeta.trackingSource).to.be('browser');
expect(e.eventMeta.ravelinDeviceId).to.be(ids.device);
expect(e.eventMeta.ravelinSessionId).to.be(ids.session);
}).then(done, done);
return {status: 204};
});
r = new Ravelin(isolate({key: key, api: '/', init: false}));
r.track.event('custom-event', {extra: true});
});
});
describe('#paste', function() {
// IE8 doesn't support createEvent or paste handling at all.
var capable = !!document.createEvent;
// Safari is difficult to craft a paste event for?
var capableContent = !!fakePasteEvent('text/plain', 'test').clipboardData;
$([
{
paste: '4234 5678 901',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 000', panCleaned: false}
},
{
paste: '4234 5678 9012',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000', panCleaned: true}
},
{
paste: '4234 5678 9012 3',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000 0', panCleaned: true}
},
{
paste: '4234 5678 9012 3',
into: '<input name=name />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000 0'}
},
{
paste: '尺卂ᐯ乇ㄥ丨几',
into: '<input name=name />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: 'XXXXXXX'}
},
{
paste: '4234 5678 9012 34',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000 00', panCleaned: true}
},
{
paste: '4234 5678 9012 345',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000 000', panCleaned: true}
},
{
paste: '4234 5678 9012-3456',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000-0000', panCleaned: true}
},
{
paste: '4234 5678-9012 3456 7',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000-0000 0000 0', panCleaned: true}
},
{
paste: '4234 5678 9012 3456 78',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000 0000 00', panCleaned: true}
},
{
paste: '4234 5678 9012 3456 789',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000 0000 000', panCleaned: true}
},
{
paste: '4234 5678 9012 3456 7890',
into: '<input name=name data-rvn-pan />',
props: {fieldName: 'name', selectionStart: 0, selectionEnd: 0, pastedValue: '0000 0000 0000 0000 0000', panCleaned: false}
},
{
paste: '4234 5678-9012 3456 7',
into: '<input name=name data-rvn-pan data-rvn-sensitive />',
props: {fieldName: 'name', panCleaned: true}
},
{
paste: '4234 5678-9012 3456 7',
into: '<input name=name data-rvn-sensitive />',
props: {fieldName: 'name'}
},
{
paste: '4234 5678-9012 3456 7',
into: '<div data-rvn-sensitive><input name=name></div>',
props: {fieldName: 'name'}
},
{
paste: 'h3ll0, wor1d.',
into: '<form action=/ name="form-name"><input name=action></form>',
props: {fieldName: 'action', formName: 'form-name', formAction: '/', selectionStart: 0, selectionEnd: 0, pastedValue: 'X0XX0, XXX0X.'}
},
{
paste: 'hello',
into: '<form action=/ name="form-name"><input type=password name=action></form>',
props: {fieldName: 'action', formName: 'form-name', formAction: '/'}
},
{
paste: 'hello',
into: '<form action=/ name="form-name"><input type=password name=action></form>',
props: {fieldName: 'action', formName: 'form-name', formAction: '/'}
}
]).each(function(n, test) {
it('sends redacted paste events of ' + JSON.stringify(test), function(done) {
if (!capable) this.skip();
var key = this.test.fullTitle();
xhook.before(function(req) {
if (!keysMatch(req, key)) return {status: 204};
var event = JSON.parse(req.body);
if (!event || !event.events || !event.events[0] || event.events[0].eventType !== 'paste') {
return {status: 204};
}
if (!capableContent) {
delete test.props.pastedValue;
// If we can't check the paste contents then we assume all pastes
// into pan fields contain pans.
if ('panCleaned' in test.props) test.props.panCleaned = true;
}
// Validate the event.
r.core.ids().then(function(ids) {
event = event.events[0];
expect(event).to.have.property('eventType', 'paste');
expect(event).to.have.property('libVer', '1.3.0-ravelinjs');
expect(event.eventMeta.trackingSource).to.be('browser');
expect(event.eventMeta.ravelinDeviceId).to.be(ids.device);
expect(event.eventMeta.ravelinSessionId).to.be(ids.session);
expect(event.eventData).to.eql({
eventName: 'paste',
properties: test.props
});
}).then(done, done);
return {status: 204};
});
r = new Ravelin(isolate({key: key, api: '/'}));
var input = $(test.into).appendTo(document.body);
input = input.find('input')[0] || input[0];
input.dispatchEvent(fakePasteEvent('text/plain', test.paste));
});
});
it('sends empty paste events from sensitive fields', function(done) {
if (!capable) this.skip();
var key = this.test.fullTitle();
xhook.before(function(req) {
if (!keysMatch(req, key)) return {status: 204};
var event = JSON.parse(req.body);
if (!event || !event.events || !event.events[0] || event.events[0].eventType !== 'paste') {
return {status: 204};
}
// Validate the event.
r.core.ids().then(function(ids) {
event = event.events[0];
expect(event).to.have.property('eventType', 'paste');
expect(event).to.have.property('libVer', '1.3.0-ravelinjs');
expect(event.eventMeta.trackingSource).to.be('browser');
expect(event.eventMeta.ravelinDeviceId).to.be(ids.device);
expect(event.eventMeta.ravelinSessionId).to.be(ids.session);
expect(event.eventData).to.eql({
eventName: 'paste',
properties: {
fieldName: 'hello',
formName: 'form-name',
formAction: '/'
}
});
}).then(done, done);
return {status: 204};
});
r = new Ravelin(isolate({key: key, api: '/'}));
var input = $('<form action=/ name="form-name"><input name=hello data-rvn-sensitive=true></form>')
.appendTo(document.body)
.find("input")[0];
input.dispatchEvent(fakePasteEvent('text/plain', 'hello'));
});
it('sends empty paste events from password fields', function(done) {
if (!capable) this.skip();
var key = this.test.fullTitle();
xhook.before(function(req) {
if (!keysMatch(req, key)) return {status: 204};
var event = JSON.parse(req.body);
if (!event || !event.events || !event.events[0] || event.events[0].eventType !== 'paste') {
return {status: 204};
}
// Validate the event.
r.core.ids().then(function(ids) {
event = event.events[0];
expect(event).to.have.property('eventType', 'paste');
expect(event).to.have.property('libVer', '1.3.0-ravelinjs');
expect(event.eventMeta.trackingSource).to.be('browser');
expect(event.eventMeta.ravelinDeviceId).to.be(ids.device);
expect(event.eventMeta.ravelinSessionId).to.be(ids.session);
expect(event.eventData).to.eql({
eventName: 'paste',
properties: {
fieldName: 'action',
formName: 'form-name',
formAction: '/'
}
});
}).then(done, done);
return {status: 204};
});
r = new Ravelin(isolate({key: key, api: '/'}));
var input = $('<form action=/ name="form-name"><input type=password name=action></form>')
.appendTo(document.body)
.find("input")[0];
input.dispatchEvent(fakePasteEvent('text/plain', 'hello'));
});
});
});
/**
* fakePasteEvent generates a ClipboardEvent as if the user pasted on the
* browser. Not guaranteed to be the same as a real clipboard event - just
* enough to kick our handler in.
* @param {string} type
* @param {string} content
* @returns {Event}
*/
function fakePasteEvent(type, content) {
try {
var d = new DataTransfer();
d.setData(type, content);
return new ClipboardEvent('paste', {
bubbles: true,
cancelable: true,
// Firefox.
dataType: type,
data: content,
// Chrome.
clipboardData: d
});
} catch(e) {}
var e = document.createEvent('CustomEvent');
e.initEvent('paste', true, true);
e.dataType = type;
e.clipboardData = {
getData: function(type) {
return content;
}
};
return e;
}