@microsoft/applicationinsights-properties-js 2.7.2-nightly.2111-08 → 2.7.2-nightly.2111-09

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/Tests/Unit/src/SessionManager.Tests.ts +375 -0
  2. package/Tests/Unit/src/prop.tests.ts +11 -0
  3. package/Tests/Unit/src/properties.tests.ts +697 -0
  4. package/Tests/Unit/src/propertiesSize.tests.ts +59 -0
  5. package/Tests/UnitTests.html +53 -0
  6. package/Tests/tsconfig.json +13 -0
  7. package/api-extractor.json +361 -0
  8. package/applicationinsights-properties-js.build.error.log +18 -0
  9. package/applicationinsights-properties-js.build.log +365 -0
  10. package/browser/applicationinsights-properties-js.integrity.json +9 -9
  11. package/browser/applicationinsights-properties-js.js +2 -2
  12. package/browser/applicationinsights-properties-js.js.map +1 -1
  13. package/browser/applicationinsights-properties-js.min.js +2 -2
  14. package/browser/applicationinsights-properties-js.min.js.map +1 -1
  15. package/dist/applicationinsights-properties-js.api.json +1 -1
  16. package/dist/applicationinsights-properties-js.d.ts +1 -1
  17. package/dist/applicationinsights-properties-js.js +2 -2
  18. package/dist/applicationinsights-properties-js.js.map +1 -1
  19. package/dist/applicationinsights-properties-js.min.js +2 -2
  20. package/dist/applicationinsights-properties-js.min.js.map +1 -1
  21. package/dist/applicationinsights-properties-js.rollup.d.ts +1 -1
  22. package/dist-esm/Context/Application.js +1 -1
  23. package/dist-esm/Context/Device.js +1 -1
  24. package/dist-esm/Context/Internal.js +2 -2
  25. package/dist-esm/Context/Internal.js.map +1 -1
  26. package/dist-esm/Context/Location.js +1 -1
  27. package/dist-esm/Context/Session.js +1 -1
  28. package/dist-esm/Context/TelemetryTrace.js +1 -1
  29. package/dist-esm/Context/User.js +1 -1
  30. package/dist-esm/Interfaces/IPropTelemetryContext.js +1 -1
  31. package/dist-esm/Interfaces/ITelemetryConfig.js +1 -1
  32. package/dist-esm/PropertiesPlugin.js +1 -1
  33. package/dist-esm/TelemetryContext.js +1 -1
  34. package/dist-esm/applicationinsights-properties-js.js +1 -1
  35. package/package.json +3 -3
  36. package/rollup.config.js +139 -0
  37. package/src/Context/Internal.ts +1 -1
  38. package/temp/applicationinsights-properties-js.api.md +180 -0
  39. package/tslint.json +8 -0
  40. package/types/tsdoc-metadata.json +1 -1
@@ -0,0 +1,697 @@
1
+ import { Assert, AITestClass } from "@microsoft/ai-test-framework";
2
+ import { AppInsightsCore, IConfiguration, DiagnosticLogger, ITelemetryItem, createCookieMgr, newId, strTrim } from "@microsoft/applicationinsights-core-js";
3
+ import PropertiesPlugin from "../../../src/PropertiesPlugin";
4
+ import { ITelemetryConfig } from "../../../src/Interfaces/ITelemetryConfig";
5
+ import { TelemetryContext } from "../../../src/TelemetryContext";
6
+ import { TelemetryTrace } from "../../../src/Context/TelemetryTrace";
7
+ import { IConfig } from "@microsoft/applicationinsights-common";
8
+
9
+ export class PropertiesTests extends AITestClass {
10
+ private properties: PropertiesPlugin;
11
+ private core: AppInsightsCore;
12
+ private _cookies: { [name: string ]: string } = {};
13
+
14
+ public testInitialize() {
15
+ let _self = this;
16
+ _self._cookies = {};
17
+ _self.core = new AppInsightsCore();
18
+ _self.core.logger = new DiagnosticLogger();
19
+ _self.core.setCookieMgr(createCookieMgr({
20
+ cookieCfg: {
21
+ setCookie: (name: string, value: string) => _self._setCookie(name, value),
22
+ getCookie: (name: string) => _self._getCookie(name),
23
+ delCookie: (name: string) => _self._delCookie(name)
24
+ }
25
+ }, _self.core.logger))
26
+ _self.properties = new PropertiesPlugin();
27
+ }
28
+
29
+ public testCleanup() {
30
+ this.core = null;
31
+ this.properties = null;
32
+ }
33
+
34
+ public registerTests() {
35
+ this.addConfigTests();
36
+ this.addUserTests();
37
+ this.addDeviceTests();
38
+ this.addTelemetryTraceTests();
39
+ this.addSessionTests();
40
+ }
41
+
42
+ private _setCookie(name: string, value: string) {
43
+ this._cookies[name] = value;
44
+ }
45
+
46
+ private _getCookie(name: string) {
47
+ let cookieValue = this._cookies[name] || "";
48
+ // Remove any cookie attributes added to the cookie
49
+ return cookieValue.split(";")[0];
50
+ }
51
+
52
+ private _delCookie(name: string) {
53
+ if (this._cookies.hasOwnProperty(name)) {
54
+ delete this._cookies[name];
55
+ }
56
+ }
57
+
58
+ private _getNewId(idLength?: number) {
59
+ return newId(idLength);
60
+ }
61
+
62
+ private addTelemetryTraceTests() {
63
+ this.testCase({
64
+ name: 'Trace: default operation.name is grabbed from window pathname, if available',
65
+ test: () => {
66
+ const operation = new TelemetryTrace();
67
+ Assert.ok(operation.name);
68
+ }
69
+ });
70
+
71
+ this.testCase({
72
+ name: 'Trace: operation.name is truncated to max size 1024 if too long',
73
+ test: () => {
74
+ const name = new Array(1234).join("a"); // exceeds max of 1024
75
+ const operation = new TelemetryTrace(undefined, undefined, name, this.core.logger);
76
+ Assert.ok(operation.name);
77
+ Assert.equal(operation.name.length, 1024);
78
+ }
79
+ });
80
+ }
81
+
82
+ private addConfigTests() {
83
+ this.testCase({
84
+ name: 'Properties Configuration: Config options can be passed from root config',
85
+ test: () => {
86
+ this.properties.initialize({
87
+ instrumentationKey: 'instrumentation_key',
88
+ accountId: 'abc',
89
+ samplingPercentage: 15,
90
+ sessionExpirationMs: 99999,
91
+ extensionConfig: {
92
+ [this.properties.identifier]: {
93
+ sessionExpirationMs: 88888
94
+ }
95
+ }
96
+ }, this.core, []);
97
+ const config: ITelemetryConfig = this.properties['_extConfig'];
98
+ Assert.equal(15, config.samplingPercentage(), 'Extension configs can be set via root config (number)');
99
+ Assert.equal('abc', config.accountId(), 'Extension configs can be set via root config (string)');
100
+ Assert.equal(88888, config.sessionExpirationMs(), 'Root config does not override extensionConfig field when both are present')
101
+ Assert.notEqual(99999, config.sessionExpirationMs(), 'extensionConfig overrides root config field when both are present');
102
+ }
103
+
104
+ });
105
+ }
106
+
107
+ private addDeviceTests() {
108
+ this.testCase({
109
+ name: 'Device: device context adds Browser field to ITelemetryItem',
110
+ test: () => {
111
+ this.properties.initialize({
112
+ instrumentationKey: 'key',
113
+ extensionConfig: {}
114
+ }, this.core, []);
115
+ this.properties.context.user.isNewUser = false;
116
+ // Act
117
+ const item: ITelemetryItem = {name: 'item'};
118
+ this.properties.processTelemetry(item);
119
+
120
+ // Assert
121
+ Assert.equal("Browser", item.ext.device.deviceClass);
122
+ Assert.equal("browser", item.ext.device.localId);
123
+ }
124
+ });
125
+ }
126
+
127
+ private addUserTests() {
128
+ this.testCase({
129
+ name: 'User: user context initializes from cookie when possible',
130
+ test: () => {
131
+ // setup
132
+ const id = "someUserId";
133
+ var cookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() => id + "||||");
134
+
135
+ // Act
136
+ Assert.ok(cookieStub.notCalled, 'Cookie not yet grabbed');
137
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
138
+ Assert.ok(cookieStub.called, 'Cookie grabbed');
139
+
140
+ // Assert
141
+ Assert.equal(id, this.properties.context.user.id, 'user id was set from cookie');
142
+ }
143
+ });
144
+
145
+ this.testCase({
146
+ name: 'User: track is triggered if user context is first time initialized',
147
+ test: () => {
148
+ // setup
149
+ var setCookieStub = this.sandbox.stub(this as any, "_setCookie").callsFake(() => {});
150
+ var loggingStub = this.sandbox.stub(this.core.logger, "logInternalMessage");
151
+
152
+ // Act
153
+ Assert.ok(setCookieStub.notCalled, 'Cookie not yet generated');
154
+ Assert.ok(loggingStub.notCalled, 'logInternalMessage is not yet triggered');
155
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
156
+ Assert.ok(setCookieStub.called, 'Cookie generated');
157
+
158
+ // Assert
159
+ Assert.equal(true, this.properties.context.user.isNewUser, 'current user is a new user');
160
+ const item: ITelemetryItem = {name: 'item'};
161
+ this.properties.processTelemetry(item);
162
+ // this.clock.tick(1000);
163
+ Assert.ok(loggingStub.called, 'logInternalMessage is triggered');
164
+ Assert.equal(false, this.properties.context.user.isNewUser, 'current user is not new user with ai_user cookie set')
165
+ }
166
+ });
167
+
168
+ this.testCase({
169
+ name: "ai_user cookie is set with acq date and year expiration",
170
+ test: () => {
171
+ // setup
172
+ var actualCookieName: string;
173
+ var actualCookieValue: string;
174
+
175
+ var newIdStub = this.sandbox.stub(this as any, "_getNewId").callsFake(() => "newId");
176
+ var getCookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() =>"");
177
+ var setCookieStub = this.sandbox.stub(this as any, "_setCookie").callsFake((cookieName, cookieValue) => {
178
+ actualCookieName = cookieName;
179
+ actualCookieValue = cookieValue;
180
+ });
181
+
182
+ // act
183
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
184
+
185
+ // verify
186
+ Assert.equal("ai_user", actualCookieName, "ai_user cookie is set");
187
+ var cookieValueParts = actualCookieValue.split(';');
188
+
189
+ Assert.equal(4, cookieValueParts.length, "ai_user cookie value should have actual value and expiration");
190
+ Assert.equal(2, cookieValueParts[0].split('|').length, "ai_user cookie value before expiration should include user id and acq date");
191
+ Assert.equal("newId", cookieValueParts[0].split('|')[0], "First part of ai_user cookie value should be new user id guid");
192
+ Assert.equal(new Date().toString(), (new Date(cookieValueParts[0].split('|')[1])).toString(), "Second part of ai_user cookie should be parsable as date");
193
+
194
+ var expiration = strTrim(cookieValueParts[1]);
195
+ Assert.equal(true, expiration.substr(0, "expires=".length) === "expires=", "ai_user cookie expiration part should start with expires=");
196
+ var expirationDate = new Date(expiration.substr("expires=".length));
197
+ Assert.equal(true, expirationDate > (new Date), "ai_user cookie expiration should be in the future");
198
+ }
199
+ });
200
+
201
+ this.testCase({
202
+ name: "ai_user cookie uses userCookiePostfix for cookie storage",
203
+ test: () => {
204
+ // setup
205
+ var actualCookieName: string;
206
+ var actualCookieValue: string;
207
+
208
+ var newIdStub = this.sandbox.stub(this as any, "_getNewId").callsFake(() => "newId");
209
+ var getCookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() =>"");
210
+ var setCookieStub = this.sandbox.stub(this as any, "_setCookie").callsFake((cookieName, cookieValue) => {
211
+ actualCookieName = cookieName;
212
+ actualCookieValue = cookieValue;
213
+ });
214
+
215
+ // act
216
+ let config: IConfig & IConfiguration = this.getEmptyConfig();
217
+ config.userCookiePostfix = 'testUserCookieNamePostfix';
218
+ this.properties.initialize(config, this.core, []);
219
+
220
+ // verify
221
+ Assert.equal("ai_usertestUserCookieNamePostfix", actualCookieName, "ai_user cookie is set");
222
+ }
223
+ });
224
+
225
+ this.testCase({
226
+ name: "Ctor: auth and account id initialize from cookie",
227
+ test: () => {
228
+ // setup
229
+ var authId = "bla@bla.com";
230
+ var accountId = "Contoso";
231
+
232
+ var cookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() => authId + "|" + accountId);
233
+
234
+ // act
235
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
236
+
237
+ // verify
238
+ Assert.equal(authId, this.properties.context.user.authenticatedId, "user auth id was set from cookie");
239
+ Assert.equal(accountId, this.properties.context.user.accountId, "user account id was not set from cookie");
240
+ }
241
+ });
242
+
243
+ this.testCase({
244
+ name: "Ctor: auth id initializes from cookie (without account id)",
245
+ test: () => {
246
+ // setup
247
+ var authId = "bla@bla.com";
248
+ var cookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() => authId);
249
+
250
+ // act
251
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
252
+
253
+ // verify
254
+ Assert.equal(authId, this.properties.context.user.authenticatedId, "user auth id was set from cookie");
255
+ }
256
+ });
257
+
258
+ this.testCase({
259
+ name: "Ctor: auth user context handles empty cookie",
260
+ test: () => {
261
+ // setup
262
+ var cookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() => "");
263
+
264
+ // act
265
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
266
+
267
+ // verify
268
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was not set");
269
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was not set");
270
+ }
271
+ });
272
+
273
+ this.testCase({
274
+ name: "Ctor: auth user context handles empty cookie with accountId backward compatibility",
275
+ test: () => {
276
+ // setup
277
+ var config = this.getEmptyConfig();
278
+ config.extensionConfig.AppInsightsPropertiesPlugin.accountId = "account17";
279
+
280
+ var cookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() => null);
281
+
282
+ // act
283
+ this.properties.initialize(config, this.core, []);
284
+
285
+ // verify
286
+ Assert.equal(config.extensionConfig.AppInsightsPropertiesPlugin.accountId, this.properties.context.user.accountId, "user account id was set from back compat");
287
+ }
288
+ });
289
+
290
+ this.testCase({
291
+ name: "setAuthenticatedUserContext: auth id and account id is set (not in the cookie)",
292
+ test: () => {
293
+ // setup
294
+ var authAndAccountId = ['bla@bla.com', 'contoso'];
295
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
296
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
297
+
298
+ // act
299
+ this.properties.context.user.setAuthenticatedUserContext(authAndAccountId[0], authAndAccountId[1]);
300
+
301
+ // verify
302
+ Assert.equal('bla@bla.com', this.properties.context.user.authenticatedId, "user auth id was not set");
303
+ Assert.equal('contoso', this.properties.context.user.accountId, "user account id was not set");
304
+ Assert.equal(cookieStub.notCalled, true, "cookie was not set");
305
+ }
306
+ });
307
+
308
+ this.testCase({
309
+ name: "setAuthenticatedUserContext: auth user set in cookie without account id",
310
+ test: () => {
311
+ // setup
312
+ var authAndAccountId = ["bla@bla.com"];
313
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
314
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
315
+
316
+ // act
317
+ this.properties.context.user.setAuthenticatedUserContext(authAndAccountId[0], null, true);
318
+
319
+ // verify
320
+ Assert.equal(authAndAccountId[0], this.properties.context.user.authenticatedId, "user auth id was set");
321
+ Assert.equal(cookieStub.calledWithExactly('ai_authUser', encodeURI(authAndAccountId.join('|')) + "; path=/"), true, "user auth id and account id cookie was set");
322
+ }
323
+ });
324
+
325
+ this.testCase({
326
+ name: "setAuthenticatedUserContext: auth user and account id set in cookie ",
327
+ test: () => {
328
+ // setup
329
+ var authAndAccountId = ['bla@bla.com', 'contoso'];
330
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
331
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
332
+
333
+ // act
334
+ this.properties.context.user.setAuthenticatedUserContext(authAndAccountId[0], authAndAccountId[1], true);
335
+
336
+ // verify
337
+ Assert.equal(authAndAccountId[0], this.properties.context.user.authenticatedId, "user auth id was set");
338
+ Assert.equal(cookieStub.calledWithExactly('ai_authUser', encodeURI(authAndAccountId.join('|')) + "; path=/"), true, "user auth id cookie was set");
339
+ }
340
+ });
341
+
342
+ this.testCase({
343
+ name: "setAuthenticatedUserContext: handles only auth user id correctly",
344
+ test: () => {
345
+ // setup
346
+ var authAndAccountId = ['bla@bla.com'];
347
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
348
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
349
+
350
+ // act
351
+ this.properties.context.user.setAuthenticatedUserContext(authAndAccountId[0], null, true);
352
+
353
+ // verify
354
+ Assert.equal(authAndAccountId[0], this.properties.context.user.authenticatedId, "user auth id was set");
355
+ Assert.equal(null, this.properties.context.user.accountId, "user account id was not set");
356
+ Assert.equal(cookieStub.calledWithExactly('ai_authUser', encodeURI(authAndAccountId[0]) + "; path=/"), true, "user auth id cookie was set");
357
+ }
358
+ });
359
+
360
+ this.testCase({
361
+ name: "setAuthenticatedUserContext: handles null correctly",
362
+ test: () => {
363
+ // setup
364
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
365
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
366
+ var loggingStub = this.sandbox.stub(this.core.logger, "throwInternal");
367
+ cookieStub.reset();
368
+ loggingStub.reset();
369
+
370
+ // act
371
+ this.properties.context.user.setAuthenticatedUserContext(null);
372
+
373
+ // verify
374
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was not set");
375
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was not set");
376
+ Assert.equal(cookieStub.notCalled, true, "cookie was not set");
377
+ Assert.equal(loggingStub.calledOnce, true, "Warning was logged");
378
+ }
379
+ });
380
+
381
+ this.testCase({
382
+ name: "setAuthenticatedUserContext: handles undefined correctly",
383
+ test: () => {
384
+ // setup
385
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
386
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
387
+ var loggingStub = this.sandbox.stub(this.core.logger, "throwInternal");
388
+ cookieStub.reset();
389
+ loggingStub.reset();
390
+
391
+ // act
392
+ this.properties.context.user.setAuthenticatedUserContext(undefined, undefined);
393
+
394
+ // verify
395
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was not set");
396
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was not set");
397
+ Assert.equal(cookieStub.notCalled, true, "cookie was not set");
398
+ Assert.equal(loggingStub.calledOnce, true, "Warning was logged");
399
+ }
400
+ });
401
+
402
+ this.testCase({
403
+ name: "setAuthenticatedUserContext: handles only accountID correctly",
404
+ test: () => {
405
+ // setup
406
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
407
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
408
+ var loggingStub = this.sandbox.stub(this.core.logger, "throwInternal");
409
+ cookieStub.reset();
410
+ loggingStub.reset();
411
+
412
+ // act
413
+ this.properties.context.user.setAuthenticatedUserContext(undefined, '1234');
414
+
415
+ // verify
416
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was not set");
417
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was not set");
418
+ Assert.equal(cookieStub.notCalled, true, "cookie was not set");
419
+ Assert.equal(loggingStub.calledOnce, true, "Warning was logged");
420
+ }
421
+ });
422
+
423
+ this.testCase({
424
+ name: "setAuthenticatedUserContext: handles authId special characters correctly",
425
+ test: () => {
426
+ // setup
427
+ var authAndAccountId = ['my|||special;id', '1234'];
428
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
429
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
430
+ var loggingStub = this.sandbox.stub(this.core.logger, "throwInternal");
431
+
432
+ // act
433
+ this.properties.context.user.setAuthenticatedUserContext(authAndAccountId[0], authAndAccountId[1], true);
434
+
435
+ // verify
436
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was not set");
437
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was not set");
438
+ Assert.equal(cookieStub.notCalled, true, "cookie was not set");
439
+ Assert.equal(loggingStub.calledOnce, true, "Warning was logged");
440
+ }
441
+ });
442
+
443
+ this.testCase({
444
+ name: "setAuthenticatedUserContext: handles accountId special characters correctly",
445
+ test: () => {
446
+ // setup
447
+ var authAndAccountId = ['myid', '1234 5678'];
448
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
449
+ this.properties.context.user.clearAuthenticatedUserContext();
450
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
451
+ var loggingStub = this.sandbox.stub(this.core.logger, "throwInternal");
452
+
453
+ // act
454
+ this.properties.context.user.setAuthenticatedUserContext(authAndAccountId[0], authAndAccountId[1]);
455
+
456
+ // verify
457
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was not set");
458
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was not set");
459
+ Assert.equal(cookieStub.notCalled, true, "cookie was not set");
460
+ Assert.equal(loggingStub.calledOnce, true, "Warning was logged");
461
+ }
462
+ });
463
+
464
+ this.testCase({
465
+ name: "setAuthenticatedUserContext: handles non-ascii unicode characters correctly",
466
+ test: () => {
467
+ // setup
468
+ var authAndAccountId = ["\u05D0", "\u05D1"]; // Hebrew characters
469
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
470
+ var cookieStub = this.sandbox.stub(this as any, "_setCookie");
471
+ var loggingStub = this.sandbox.stub(this.core.logger, "throwInternal");
472
+
473
+ // act
474
+ this.properties.context.user.setAuthenticatedUserContext(authAndAccountId[0], authAndAccountId[1], true);
475
+
476
+ // verify
477
+ Assert.equal(authAndAccountId[0], this.properties.context.user.authenticatedId, "user auth id was set");
478
+ Assert.equal(authAndAccountId[1], this.properties.context.user.accountId, "user account id was set");
479
+ Assert.equal(cookieStub.calledWithExactly('ai_authUser', encodeURI(authAndAccountId.join('|')) + "; path=/"), true, "user auth id cookie was set");
480
+ Assert.equal(loggingStub.notCalled, true, "No warnings");
481
+ }
482
+ });
483
+
484
+ this.testCase({
485
+ name: "clearAuthenticatedUserContext: auth user and account cleared in context and cookie ",
486
+ test: () => {
487
+ // setup
488
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
489
+ this.properties.context.user.setAuthenticatedUserContext("bla", "123");
490
+ var cookieStub = this.sandbox.stub(this as any, "_delCookie");
491
+
492
+ // act
493
+ this.properties.context.user.clearAuthenticatedUserContext();
494
+
495
+ // verify
496
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was cleared");
497
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was cleared");
498
+ Assert.equal(cookieStub.calledWithExactly('ai_authUser'), true, "cookie was deleted");
499
+ }
500
+ });
501
+
502
+ this.testCase({
503
+ name: "clearAuthenticatedUserContext: works correctly when auth id and account id were never set",
504
+ test: () => {
505
+ // setup
506
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
507
+ var cookieStub = this.sandbox.stub(this as any, "_delCookie");
508
+
509
+ // act
510
+ this.properties.context.user.clearAuthenticatedUserContext();
511
+
512
+ // verify
513
+ Assert.equal(undefined, this.properties.context.user.authenticatedId, "user auth id was cleared");
514
+ Assert.equal(undefined, this.properties.context.user.accountId, "user account id was cleared");
515
+ Assert.equal(cookieStub.calledWithExactly('ai_authUser'), true, "cookie was deleted");
516
+ }
517
+ });
518
+
519
+ this.testCase({
520
+ name: "Validate telemetrycontext sets up web extension properties on TelemetryItem",
521
+ test: () => {
522
+ // setup
523
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
524
+
525
+ let context = new TelemetryContext(this.core, this.getTelemetryConfig());
526
+ context.web = {
527
+ domain: "www.bing.com",
528
+ userConsent: true,
529
+ screenRes: "1024x768",
530
+ browser: "internet explorer",
531
+ browserVer: "48.0",
532
+ isManual: true,
533
+ browserLang: "EN"
534
+ };
535
+
536
+ let telemetyItem: ITelemetryItem = {
537
+ name: "test",
538
+ time: new Date("2018-06-12").toISOString(),
539
+ iKey: "iKey",
540
+ ext: {},
541
+ baseType: "RemoteDependencyData",
542
+ baseData: {
543
+ id: 'some id',
544
+ name: "/test/name",
545
+ success: true,
546
+ responseCode: 200,
547
+ duration: 123,
548
+ type: 'Fetch',
549
+ data: 'some data',
550
+ target: 'https://example.com/test/name'
551
+ },
552
+ data: {
553
+ property1: "val1",
554
+ measurement1: 50.0,
555
+ }
556
+ };
557
+
558
+ context.applyWebContext(telemetyItem);
559
+ let ext = telemetyItem.ext;
560
+ Assert.ok(ext);
561
+ Assert.equal("www.bing.com", ext.web.domain);
562
+ Assert.equal("1024x768", ext.web.screenRes);
563
+ Assert.equal(true, ext.web.userConsent);
564
+ Assert.equal("48.0", ext.web.browserVer);
565
+ Assert.equal("EN", ext.web.browserLang);
566
+ Assert.equal("internet explorer", ext.web.browser);
567
+ Assert.equal(true, ext.web.isManual);
568
+ }
569
+ });
570
+
571
+ this.testCase({
572
+ name: "validate telemetrycontext cleanup sets empty extensions to undefined",
573
+ test: () => {
574
+ // setup
575
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
576
+
577
+ const telemetyItem: ITelemetryItem = {
578
+ name: "test",
579
+ time: new Date("2018-06-12").toISOString(),
580
+ iKey: "iKey",
581
+ ext: {
582
+ "user" : {
583
+ "localId": "TestId",
584
+ "authId": "AuthenticatedId",
585
+ "id": "TestId"
586
+ },
587
+ "web": {}
588
+ },
589
+ tags: [{"user.accountId": "TestAccountId"}],
590
+ baseType: "RemoteDependencyData",
591
+ baseData: {
592
+ id: 'some id',
593
+ name: "/test/name",
594
+ success: true,
595
+ responseCode: 200,
596
+ duration: 123,
597
+ type: 'Fetch',
598
+ data: 'some data',
599
+ target: 'https://example.com/test/name'
600
+ },
601
+ data: {
602
+ property1: "val1",
603
+ property2: "val2",
604
+ measurement1: 50.0,
605
+ measurement2: 1.3
606
+ }
607
+ }
608
+
609
+ // act
610
+ const telemetrycontext = new TelemetryContext(this.core, this.getTelemetryConfig());
611
+ telemetrycontext.cleanUp(telemetyItem);
612
+
613
+ // verify
614
+ Assert.equal(undefined, telemetyItem.ext.web, "web was cleared");
615
+ Assert.notEqual(undefined, telemetyItem.ext.user, "user was not cleared");
616
+ }
617
+ });
618
+ }
619
+
620
+ private addSessionTests() {
621
+ this.testCase({
622
+ name: 'Session: automaticSession session id is stored in sesId if customer does not provide session info',
623
+ test: () => {
624
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
625
+
626
+ // Assert
627
+ const item: ITelemetryItem = {name: 'item'};
628
+ this.properties.processTelemetry(item);
629
+ Assert.ok(this.properties.context.getSessionId(), 'session id is stored in sesId');
630
+ Assert.equal(this.properties.context.getSessionId(), this.properties.context.sessionManager.automaticSession.id, 'automaticSession is stored in sesId')
631
+ }
632
+ });
633
+
634
+ this.testCase({
635
+ name: 'Session: customer session id is stored in sesId if customer provides session info',
636
+ test: () => {
637
+ this.properties.initialize(this.getEmptyConfig(), this.core, []);
638
+
639
+ // Assert
640
+ const item: ITelemetryItem = {name: 'item'};
641
+ this.properties.context.session.id = 'random id';
642
+ this.properties.processTelemetry(item);
643
+ Assert.ok(this.properties.context.getSessionId(), 'session id is stored in sesId');
644
+ Assert.equal(this.properties.context.getSessionId(), 'random id', 'automaticSession is stored in sesId')
645
+ }
646
+ });
647
+ }
648
+
649
+ private getEmptyConfig(): IConfiguration {
650
+ return {
651
+ instrumentationKey: 'key',
652
+
653
+ extensionConfig: {
654
+ AppInsightsPropertiesPlugin: {
655
+ accountId: null,
656
+ sessionRenewalMs: null,
657
+ sessionExpirationMs: null,
658
+ samplingPercentage: null,
659
+ endpointUrl: null,
660
+ cookieDomain: null,
661
+ emitLineDelimitedJson: null,
662
+ maxBatchSizeInBytes: null,
663
+ maxBatchInterval: null,
664
+ disableTelemetry: null,
665
+ enableSessionStorageBuffer: null,
666
+ isRetryDisabled: null,
667
+ isBeaconApiDisabled: null,
668
+ sdkExtension: null,
669
+ isBrowserLinkTrackingEnabled: null,
670
+ appId: null,
671
+ sesId: null,
672
+ getNewId: (idLength?: number) => this._getNewId(idLength)
673
+ }
674
+ }
675
+ };
676
+ }
677
+
678
+ private getTelemetryConfig(): ITelemetryConfig {
679
+ return {
680
+ instrumentationKey: () => "",
681
+ accountId: () => "",
682
+ sessionRenewalMs: () => 1000,
683
+ samplingPercentage: () => 0,
684
+ sessionExpirationMs: () => 1000,
685
+ cookieDomain: () => null,
686
+ sdkExtension: () => "",
687
+ isBrowserLinkTrackingEnabled: () => true,
688
+ appId: () => "",
689
+ getSessionId: () => "",
690
+ namePrefix: () => "",
691
+ sessionCookiePostfix: () => "",
692
+ userCookiePostfix: () => "",
693
+ idLength: () => 22,
694
+ getNewId: () => this._getNewId
695
+ }
696
+ }
697
+ }