Skip to content

Commit

Permalink
Expose the getCookieMgr() on the snippet proxy and analytics web inst…
Browse files Browse the repository at this point in the history
…ances #1512
  • Loading branch information
MSNev committed Mar 25, 2021
1 parent 2e0706f commit 2caaf9c
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 29 deletions.
100 changes: 92 additions & 8 deletions AISKU/Tests/SnippetInitialization.Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import { Assert } from "./TestFramework/Assert";
import { PollingAssert } from "./TestFramework/PollingAssert";
import { TestClass } from "./TestFramework/TestClass";
import { createSnippetV5 } from "./testSnippet";
import { ITelemetryItem, objForEachKey } from "@microsoft/applicationinsights-core-js";
import { ContextTagKeys, DistributedTracingModes, IDependencyTelemetry, RequestHeaders, Util } from "@microsoft/applicationinsights-common";
import { hasOwnProperty, isNotNullOrUndefined, ITelemetryItem, objForEachKey } from "@microsoft/applicationinsights-core-js";
import { ContextTagKeys, DistributedTracingModes, IConfig, IDependencyTelemetry, RequestHeaders, Util } from "@microsoft/applicationinsights-common";
import { getGlobal } from "@microsoft/applicationinsights-shims";
import { TelemetryContext } from "@microsoft/applicationinsights-properties-js";

const TestInstrumentationKey = 'b7170927-2d1c-44f1-acec-59f4e1751c11';

const _expectedProperties = [
const _expectedBeforeProperties = [
"config",
"cookie"
];

const _expectedAfterProperties = [
"appInsights",
"core",
"context"
Expand All @@ -37,6 +41,10 @@ const _expectedTrackMethods = [
"flush"
];

const _expectedMethodsAfterInitialization = [
"getCookieMgr"
];

function getSnippetConfig(sessionPrefix: string) {
return {
src: "",
Expand All @@ -52,7 +60,7 @@ function getSnippetConfig(sessionPrefix: string) {
enableCorsCorrelation: true,
distributedTracingMode: DistributedTracingModes.AI_AND_W3C,
samplingPercentage: 50
}
} as IConfig
};
};

Expand Down Expand Up @@ -137,6 +145,23 @@ export class SnippetInitializationTests extends TestClass {
Assert.ok(funcSpy.called, "Function [" + method + "] of the appInsights should have been called")
}
});

_expectedMethodsAfterInitialization.forEach(method => {
Assert.ok(theSnippet[method], `${method} exists`);
Assert.equal('function', typeof theSnippet[method], `${method} is a function`);

let funcSpy = this.sandbox.spy(theSnippet.appInsights, method);

try {
theSnippet[method]();
} catch(e) {
// Do nothing
}

if (funcSpy) {
Assert.ok(funcSpy.called, "Function [" + method + "] of the appInsights should have been called")
}
});
}, PollingAssert.createPollingAssert(() => {
try {
Assert.ok(true, "* waiting for scheduled actions to send events " + new Date().toISOString());
Expand Down Expand Up @@ -164,11 +189,66 @@ export class SnippetInitializationTests extends TestClass {
this.testCase({
name: "Check properties exist",
test: () => {
let theSnippet = this._initializeSnippet(snippetCreator(getSnippetConfig(this.sessionPrefix))) as any;
_expectedProperties.forEach(property => {
Assert.ok(theSnippet[property], `${property} exists`);
let preSnippet = snippetCreator(getSnippetConfig(this.sessionPrefix));
_expectedBeforeProperties.forEach(property => {
Assert.ok(hasOwnProperty(preSnippet, property), `${property} has property`);
Assert.ok(isNotNullOrUndefined(preSnippet[property]), `${property} exists`);
});
_expectedAfterProperties.forEach(property => {
Assert.ok(!hasOwnProperty(preSnippet, property), `${property} does not exist`);
});

let theSnippet = this._initializeSnippet(preSnippet) as any;
_expectedAfterProperties.forEach(property => {
Assert.ok(hasOwnProperty(theSnippet, property) , `${property} exists`);
Assert.notEqual('function', typeof theSnippet[property], `${property} is not a function`);
});

Assert.ok(isNotNullOrUndefined(theSnippet.core), "Make sure the core is set");
Assert.ok(isNotNullOrUndefined(theSnippet.appInsights.core), "Make sure the appInsights core is set");
Assert.equal(theSnippet.core, theSnippet.appInsights.core, "Make sure the core instances are actually the same");
}
});

this.testCase({
name: "Check cookie manager access",
test: () => {
let theSnippet = this._initializeSnippet(snippetCreator(getSnippetConfig(this.sessionPrefix))) as any;

let coreCookieMgr = theSnippet.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(coreCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(true, coreCookieMgr.isEnabled(), "Cookies should be enabled")
Assert.equal(coreCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");

let appInsightsCookieMgr = theSnippet.appInsights.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(appInsightsCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(true, appInsightsCookieMgr.isEnabled(), "Cookies should be enabled")
Assert.equal(appInsightsCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");
Assert.equal(coreCookieMgr, appInsightsCookieMgr, "Make sure the cookie managers are the same");

Assert.equal(true, theSnippet.getCookieMgr().isEnabled(), "Cookies should be enabled")
}
});

this.testCase({
name: "Check cookie manager access as disabled",
test: () => {
let theConfig = getSnippetConfig(this.sessionPrefix);
theConfig.cfg.disableCookiesUsage = true;
let theSnippet = this._initializeSnippet(snippetCreator(theConfig)) as any;

let coreCookieMgr = theSnippet.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(coreCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(false, coreCookieMgr.isEnabled(), "Cookies should be disabled")
Assert.equal(coreCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");

let appInsightsCookieMgr = theSnippet.appInsights.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(appInsightsCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(false, appInsightsCookieMgr.isEnabled(), "Cookies should be disabled")
Assert.equal(appInsightsCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");
Assert.equal(coreCookieMgr, appInsightsCookieMgr, "Make sure the cookie managers are the same");

Assert.equal(false, theSnippet.getCookieMgr().isEnabled(), "Cookies should be disabled")
}
});

Expand All @@ -188,7 +268,11 @@ export class SnippetInitializationTests extends TestClass {
Assert.ok(theSnippet[method], `${method} exists`);
Assert.equal('function', typeof theSnippet[method], `${method} is a function`);
});
}
_expectedMethodsAfterInitialization.forEach(method => {
Assert.ok(theSnippet[method], `${method} does exists`);
Assert.equal('function', typeof theSnippet[method], `${method} is a function`);
});
}
});
}

Expand Down
117 changes: 103 additions & 14 deletions AISKU/Tests/SnippetLegacyInitialization.Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { SinonSpy } from "sinon";
import { Assert } from "./TestFramework/Assert";
import { PollingAssert } from "./TestFramework/PollingAssert";
import { TestClass } from "./TestFramework/TestClass";
import { hasOwnProperty, isNotNullOrUndefined } from "@microsoft/applicationinsights-core-js";

const BasicLegacySnippetConfig = {
instrumentationKey: 'b7170927-2d1c-44f1-acec-59f4e1751c11',
disableAjaxTracking: false,
disableFetchTracking: false,
maxBatchInterval: 500
};
function getBasicLegacySnippetConfig() {
return {
instrumentationKey: 'b7170927-2d1c-44f1-acec-59f4e1751c11',
disableAjaxTracking: false,
disableFetchTracking: false,
maxBatchInterval: 500
};
}

const _expectedProperties = [
"config",
Expand All @@ -40,6 +43,10 @@ const _expectedTrackMethods = [
"flush"
];

const _expectedMethodsAfterInitialization = [
"getCookieMgr"
];

export class SnippetLegacyInitializationTests extends TestClass {

// Sinon
Expand All @@ -65,7 +72,7 @@ export class SnippetLegacyInitializationTests extends TestClass {
this.testCase({
name: "Check support for 1.0 apis",
test: () => {
let theSnippet = this._initializeSnippet(createLegacySnippet(BasicLegacySnippetConfig));
let theSnippet = this._initializeSnippet(createLegacySnippet(getBasicLegacySnippetConfig()));
Assert.ok(theSnippet, 'ApplicationInsights SDK exists');
Assert.ok(theSnippet.downloadAndSetup, "The [Legacy] snippet should have the downloadAndSetup"); // has legacy method
}
Expand All @@ -75,8 +82,9 @@ export class SnippetLegacyInitializationTests extends TestClass {
name: "Public Members exist",
stepDelay: 1,
steps: [() => {
let theSnippet = this._initializeSnippet(createLegacySnippet(BasicLegacySnippetConfig)) as any;
let theSnippet = this._initializeSnippet(createLegacySnippet(getBasicLegacySnippetConfig())) as any;
_expectedTrackMethods.forEach(method => {
Assert.ok(hasOwnProperty(theSnippet, method), `${method} exists`);
Assert.ok(theSnippet[method], `${method} exists`);
Assert.equal('function', typeof theSnippet[method], `${method} is a function`);

Expand All @@ -103,6 +111,27 @@ export class SnippetLegacyInitializationTests extends TestClass {
Assert.ok(funcSpy.called, "Function [" + method + "] of the appInsights should have been called")
}
});

_expectedMethodsAfterInitialization.forEach(method => {
Assert.ok(hasOwnProperty(theSnippet, method), `${method} exists`);
Assert.ok(theSnippet[method], `${method} exists`);
Assert.equal('function', typeof theSnippet[method], `${method} is a function`);

let funcSpy = this.sandbox.spy(theSnippet.appInsights, method);
let funcSpyNew = this.sandbox.spy(theSnippet.appInsightsNew, method);

try {
theSnippet[method]();
} catch(e) {
// Do nothing
}

Assert.ok(funcSpyNew.called, "Function [" + method + "] of the new implementation should have been called")
if (funcSpy) {
Assert.ok(funcSpy.called, "Function [" + method + "] of the appInsights should have been called")
}
});

}, PollingAssert.createPollingAssert(() => {
try {
Assert.ok(true, "* waiting for scheduled actions to send events " + new Date().toISOString());
Expand Down Expand Up @@ -130,11 +159,71 @@ export class SnippetLegacyInitializationTests extends TestClass {
this.testCase({
name: "Check properties exist",
test: () => {
let theSnippet = this._initializeSnippet(createLegacySnippet(BasicLegacySnippetConfig)) as any;
let theSnippet = this._initializeSnippet(createLegacySnippet(getBasicLegacySnippetConfig())) as any;
_expectedProperties.forEach(property => {
Assert.ok(hasOwnProperty(theSnippet, property), `${property} has own property`)
Assert.notEqual(undefined, theSnippet[property], `${property} exists`);
Assert.notEqual('function', typeof theSnippet[property], `${property} is not a function`);
});

Assert.ok(isNotNullOrUndefined(theSnippet.core), "Make sure the core is set");
Assert.ok(isNotNullOrUndefined(theSnippet.appInsightsNew.core), "Make sure the appInsights core is set");
Assert.equal(theSnippet.core, theSnippet.appInsightsNew.core, "Make sure the core instances are actually the same");
}
});


this.testCase({
name: "Check cookie manager access",
test: () => {
let theSnippet = this._initializeSnippet(createLegacySnippet(getBasicLegacySnippetConfig())) as any;

let coreCookieMgr = theSnippet.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(coreCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(true, coreCookieMgr.isEnabled(), "Cookies should be enabled")
Assert.equal(coreCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");

let appInsightsCookieMgr = theSnippet.appInsightsNew.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(appInsightsCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(true, appInsightsCookieMgr.isEnabled(), "Cookies should be enabled")
Assert.equal(appInsightsCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");
Assert.equal(coreCookieMgr, appInsightsCookieMgr, "Make sure the cookie managers are the same");

Assert.equal(true, theSnippet.getCookieMgr().isEnabled(), "Cookies should be enabled")

let appInsightsCookieMgr2 = theSnippet.appInsightsNew.appInsights.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(appInsightsCookieMgr2), "Make sure the cookie manager is returned");
Assert.equal(true, appInsightsCookieMgr2.isEnabled(), "Cookies should be enabled")
Assert.equal(appInsightsCookieMgr2, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");
Assert.equal(coreCookieMgr, appInsightsCookieMgr2, "Make sure the cookie managers are the same");
}
});

this.testCase({
name: "Check cookie manager access as disabled",
test: () => {
let theConfig = getBasicLegacySnippetConfig() as any;
theConfig.disableCookiesUsage = true;
let theSnippet = this._initializeSnippet(createLegacySnippet(theConfig)) as any;

let coreCookieMgr = theSnippet.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(coreCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(false, coreCookieMgr.isEnabled(), "Cookies should be disabled")
Assert.equal(coreCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");

let appInsightsCookieMgr = theSnippet.appInsightsNew.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(appInsightsCookieMgr), "Make sure the cookie manager is returned");
Assert.equal(false, appInsightsCookieMgr.isEnabled(), "Cookies should be disabled")
Assert.equal(appInsightsCookieMgr, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");
Assert.equal(coreCookieMgr, appInsightsCookieMgr, "Make sure the cookie managers are the same");

Assert.equal(false, theSnippet.getCookieMgr().isEnabled(), "Cookies should be disabled")

let appInsightsCookieMgr2 = theSnippet.appInsightsNew.appInsights.core.getCookieMgr();
Assert.ok(isNotNullOrUndefined(appInsightsCookieMgr2), "Make sure the cookie manager is returned");
Assert.equal(false, appInsightsCookieMgr2.isEnabled(), "Cookies should be disabled")
Assert.equal(appInsightsCookieMgr2, theSnippet.getCookieMgr(), "Make sure the cookie manager is returned");
Assert.equal(coreCookieMgr, appInsightsCookieMgr2, "Make sure the cookie managers are the same");
}
});

Expand All @@ -146,7 +235,7 @@ export class SnippetLegacyInitializationTests extends TestClass {
name: "[Legacy] trackEvent sends to backend",
stepDelay: 1,
steps: [() => {
let theSnippet = this._initializeSnippet(snippetCreator(BasicLegacySnippetConfig)) as IAppInsightsDeprecated;
let theSnippet = this._initializeSnippet(snippetCreator(getBasicLegacySnippetConfig())) as IAppInsightsDeprecated;
theSnippet.trackEvent("event");
}].concat(this.asserts(1))
});
Expand All @@ -155,7 +244,7 @@ export class SnippetLegacyInitializationTests extends TestClass {
name: "[Legacy] trackTrace sends to backend",
stepDelay: 1,
steps: [() => {
let theSnippet = this._initializeSnippet(snippetCreator(BasicLegacySnippetConfig)) as IAppInsightsDeprecated;
let theSnippet = this._initializeSnippet(snippetCreator(getBasicLegacySnippetConfig())) as IAppInsightsDeprecated;
theSnippet.trackTrace("trace");
}].concat(this.asserts(1))
});
Expand All @@ -165,7 +254,7 @@ export class SnippetLegacyInitializationTests extends TestClass {
stepDelay: 1,
steps: [() => {
let exception: Error = null;
let theSnippet = this._initializeSnippet(snippetCreator(BasicLegacySnippetConfig)) as IAppInsightsDeprecated;
let theSnippet = this._initializeSnippet(snippetCreator(getBasicLegacySnippetConfig())) as IAppInsightsDeprecated;

try {
window['a']['b']();
Expand All @@ -183,7 +272,7 @@ export class SnippetLegacyInitializationTests extends TestClass {
stepDelay: 1,
steps: [
() => {
let theSnippet = this._initializeSnippet(snippetCreator(BasicLegacySnippetConfig)) as IAppInsightsDeprecated;
let theSnippet = this._initializeSnippet(snippetCreator(getBasicLegacySnippetConfig())) as IAppInsightsDeprecated;

console.log("* calling trackMetric " + new Date().toISOString());
for (let i = 0; i < 100; i++) {
Expand All @@ -199,7 +288,7 @@ export class SnippetLegacyInitializationTests extends TestClass {
stepDelay: 1,
steps: [
() => {
let theSnippet = this._initializeSnippet(snippetCreator(BasicLegacySnippetConfig)) as IAppInsightsDeprecated;
let theSnippet = this._initializeSnippet(snippetCreator(getBasicLegacySnippetConfig())) as IAppInsightsDeprecated;
theSnippet.trackPageView(); // sends 2
}
].concat(this.asserts(2))
Expand Down
Loading

0 comments on commit 2caaf9c

Please sign in to comment.