Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ai_user cookie should use userCookiePostfix for user cookie storage #1587

Merged
merged 6 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropertiesPlugin from "../../src/PropertiesPlugin";
import { ITelemetryConfig } from "../../src/Interfaces/ITelemetryConfig";
import { TelemetryContext } from "../../src/TelemetryContext";
import { TelemetryTrace } from "../../src/Context/TelemetryTrace";
import { IConfig } from "@microsoft/applicationinsights-common";

export class PropertiesTests extends AITestClass {
private properties: PropertiesPlugin;
Expand Down Expand Up @@ -196,6 +197,30 @@ export class PropertiesTests extends AITestClass {
}
});

this.testCase({
name: "ai_user cookie uses name prefix for cookie storage",
test: () => {
// setup
var actualCookieName: string;
var actualCookieValue: string;

var newIdStub = this.sandbox.stub(this as any, "_getNewId").callsFake(() => "newId");
var getCookieStub = this.sandbox.stub(this as any, "_getCookie").callsFake(() =>"");
var setCookieStub = this.sandbox.stub(this as any, "_setCookie").callsFake((cookieName, cookieValue) => {
actualCookieName = cookieName;
actualCookieValue = cookieValue;
});

// act
let config: IConfig & IConfiguration = this.getEmptyConfig();
config.namePrefix = 'testNamePrefix';
this.properties.initialize(config, this.core, []);

// verify
Assert.equal("ai_usertestNamePrefix", actualCookieName, "ai_user cookie is set");
}
});

this.testCase({
name: "Ctor: auth and account id initialize from cookie",
test: () => {
Expand Down
10 changes: 6 additions & 4 deletions extensions/applicationinsights-properties-js/src/Context/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ export class User implements IUserContext {
constructor(config: ITelemetryConfig, core: IAppInsightsCore) {
let _logger = safeGetLogger(core);
let _cookieManager: ICookieMgr = safeGetCookieMgr(core);
let _storageNamePrefix: () => string;

dynamicProto(User, this, (_self) => {
_self.config = config;
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
_storageNamePrefix = () => _self.config.namePrefix && _self.config.namePrefix() ? User.userCookieName + _self.config.namePrefix() : User.userCookieName;
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved

// get userId or create new one if none exists
const cookie = _cookieManager.get(User.userCookieName);
const cookie = _cookieManager.get(_storageNamePrefix());
if (cookie) {
_self.isNewUser = false;
const params = cookie.split(User.cookieSeparator);
Expand All @@ -73,8 +77,6 @@ export class User implements IUserContext {
}
}

_self.config = config;

if (!_self.id) {
let theConfig = (config || {}) as ITelemetryConfig;
let getNewId = (theConfig.getNewId ? theConfig.getNewId() : null) || newId;
Expand All @@ -88,7 +90,7 @@ export class User implements IUserContext {
_self.isNewUser = true;
const newCookie = [_self.id, acqStr];

_cookieManager.set(User.userCookieName, newCookie.join(User.cookieSeparator), oneYear);
_cookieManager.set(_storageNamePrefix(), newCookie.join(User.cookieSeparator), oneYear);

// If we have an config.namePrefix() + ai_session in local storage this means the user actively removed our cookies.
// We should respect their wishes and clear ourselves from local storage
Expand Down