Skip to content

Commit

Permalink
Re-work dependencies to hande targetMigrationEnabled flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsnyder committed Sep 19, 2022
1 parent 285a86d commit d324204
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 101 deletions.
5 changes: 2 additions & 3 deletions src/components/Personalization/createComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { AUTHORING_ENABLED } from "./constants/loggerMessage";
import validateApplyPropositionsOptions from "./validateApplyPropositionsOptions";

export default ({
config,
logger,
fetchDataHandler,
viewChangeHandler,
Expand All @@ -26,12 +25,12 @@ export default ({
viewCache,
showContainers,
applyPropositions,
setMigrationEnabled
setTargetMigration
}) => {
return {
lifecycle: {
onBeforeRequest({ request }) {
setMigrationEnabled(config, request);
setTargetMigration(request);
return Promise.resolve();
},
onBeforeEvent({
Expand Down
9 changes: 6 additions & 3 deletions src/components/Personalization/createFetchDataHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

export default ({ config, responseHandler, hideContainers, mergeQuery }) => {
export default ({
prehidingStyle,
responseHandler,
hideContainers,
mergeQuery
}) => {
return ({ decisionsDeferred, personalizationDetails, event, onResponse }) => {
const { prehidingStyle } = config;

if (personalizationDetails.isRenderDecisions()) {
hideContainers(prehidingStyle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

export default (config, request) => {
const { targetMigrationEnabled } = config;
import { noop } from "../../utils";

export default ({ targetMigrationEnabled }) => {
if (targetMigrationEnabled) {
request.getPayload().mergeMeta({ target: { migration: true } });
return request => {
request.getPayload().mergeMeta({ target: { migration: true } });
};
}
return noop;
};
11 changes: 7 additions & 4 deletions src/components/Personalization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import createRedirectHandler from "./createRedirectHandler";
import createAutorenderingHandler from "./createAutoRenderingHandler";
import createNonRenderingHandler from "./createNonRenderingHandler";
import createApplyPropositions from "./createApplyPropositions";
import setMigrationEnabled from "./migration/setMigrationEnabled";
import createSetTargetMigration from "./createSetTargetMigration";

const createPersonalization = ({ config, logger, eventManager }) => {
const { targetMigrationEnabled, prehidingStyle } = config;
const collect = createCollect({ eventManager, mergeDecisionsMeta });

const {
Expand Down Expand Up @@ -72,7 +73,7 @@ const createPersonalization = ({ config, logger, eventManager }) => {
showContainers
});
const fetchDataHandler = createFetchDataHandler({
config,
prehidingStyle,
responseHandler,
hideContainers,
mergeQuery
Expand All @@ -89,8 +90,10 @@ const createPersonalization = ({ config, logger, eventManager }) => {
executeDecisions,
viewCache
});
const setTargetMigration = createSetTargetMigration({
targetMigrationEnabled
});
return createComponent({
config,
logger,
fetchDataHandler,
viewChangeHandler,
Expand All @@ -100,7 +103,7 @@ const createPersonalization = ({ config, logger, eventManager }) => {
viewCache,
showContainers,
applyPropositions,
setMigrationEnabled
setTargetMigration
});
};

Expand Down
22 changes: 8 additions & 14 deletions src/core/createCookieTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { endsWith, isLegacyCookieName, isNamespacedCookieName } from "../utils";
import { endsWith } from "../utils";

const STATE_STORE_HANDLE_TYPE = "state:store";

export default ({ cookieJar, config, apexDomain, dateProvider }) => {
const { orgId } = config;
export default ({
cookieJar,
shouldTransferCookie,
apexDomain,
dateProvider
}) => {
return {
/**
* When sending to a third-party endpoint, the endpoint won't be able to
Expand All @@ -37,17 +41,7 @@ export default ({ cookieJar, config, apexDomain, dateProvider }) => {
const cookies = cookieJar.get();

const entries = Object.keys(cookies)
.filter(name => {
// We have a contract with the server that we will pass
// all cookies whose names are namespaced according to the
// logic in isNamespacedCookieName as well as any legacy
// cookie names (so that the server can handle migrating
// identities on websites previously using Visitor.js)
return (
isNamespacedCookieName(orgId, name) ||
isLegacyCookieName(name, config)
);
})
.filter(shouldTransferCookie)
.map(qualifyingCookieName => {
return {
key: qualifyingCookieName,
Expand Down
9 changes: 7 additions & 2 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import injectSendBeaconRequest from "./network/requestMethods/injectSendBeaconRe
import createLogger from "./createLogger";
import createEventManager from "./createEventManager";
import createCookieTransfer from "./createCookieTransfer";
import injectShouldTransferCookie from "./injectShouldTransferCookie";
import {
createDataCollectionRequest,
createDataCollectionRequestPayload
Expand Down Expand Up @@ -90,10 +91,14 @@ export const createExecuteCommand = ({
logger,
setDebugEnabled
});
const { orgId } = config;
const { orgId, targetMigrationEnabled } = config;
const shouldTransferCookie = injectShouldTransferCookie({
orgId,
targetMigrationEnabled
});
const cookieTransfer = createCookieTransfer({
cookieJar: loggingCookieJar,
config,
shouldTransferCookie,
apexDomain,
dateProvider: () => new Date()
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import { isNamespacedCookieName } from "../utils";
import { AT_QA_MODE, MBOX } from "../constants/legacyCookies";

export default (name, config) => {
const { targetMigrationEnabled } = config;
return name === AT_QA_MODE || (name === MBOX && targetMigrationEnabled);
export default ({ orgId, targetMigrationEnabled }) => name => {
// We have a contract with the server that we will pass
// all cookies whose names are namespaced according to the
// logic in isNamespacedCookieName as well as any legacy
// cookie names (so that the server can handle migrating
// identities on websites previously using Visitor.js)
return (
isNamespacedCookieName(orgId, name) ||
name === AT_QA_MODE ||
(targetMigrationEnabled && name === MBOX)
);
};
1 change: 0 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export { default as isEmptyObject } from "./isEmptyObject";
export { default as isFunction } from "./isFunction";
export { default as isInteger } from "./isInteger";
export { default as isNamespacedCookieName } from "./isNamespacedCookieName";
export { default as isLegacyCookieName } from "./isLegacyCookieName";
export { default as isNonEmptyArray } from "./isNonEmptyArray";
export { default as isNonEmptyString } from "./isNonEmptyString";
export { default as isNil } from "./isNil";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ describe("Personalization", () => {
let mergeQuery;
let event;
let personalizationComponent;
let setMigrationEnabled;
let setTargetMigration;

const build = () => {
personalizationComponent = createComponent({
config: {},
logger,
fetchDataHandler,
viewChangeHandler,
Expand All @@ -36,7 +35,7 @@ describe("Personalization", () => {
mergeQuery,
viewCache,
showContainers,
setMigrationEnabled
setTargetMigration
});
};

Expand All @@ -59,7 +58,7 @@ describe("Personalization", () => {
"isInitialized",
"storeViews"
]);
setMigrationEnabled = jasmine.createSpy("setMigrationEnabled");
setTargetMigration = jasmine.createSpy("setTargetMigration");

build();
});
Expand Down Expand Up @@ -162,13 +161,13 @@ describe("Personalization", () => {
});

describe("onBeforeRequest", () => {
it("should always call setMigrationEnabled during onBeforeRequest", () => {
it("should always call setTargetMigration during onBeforeRequest", () => {
const request = jasmine.createSpyObj("request", ["getPayload"]);
personalizationComponent.lifecycle.onBeforeRequest({
request
});

expect(setMigrationEnabled).toHaveBeenCalled();
expect(setTargetMigration).toHaveBeenCalledOnceWith(request);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,33 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import setMigrationEnabled from "../../../../../../src/components/Personalization/migration/setMigrationEnabled";
import createSetTargetMigration from "../../../../../src/components/Personalization/createSetTargetMigration";

describe("Personalization::setMigrationEnabled", () => {
it("adds to request meta if targetMigrationEnabled=true is configured", () => {
const request = {
getPayload: jasmine
.createSpy("getPayload")
.and.returnValue(jasmine.createSpyObj("getPayloadObj", ["mergeMeta"]))
};
const config = {
targetMigrationEnabled: true
};
describe("Personalization::createSetTargetMigration", () => {
let request;
let payload;

setMigrationEnabled(config, request);
beforeEach(() => {
request = jasmine.createSpyObj("request", ["getPayload"]);
payload = jasmine.createSpyObj("payload", ["mergeMeta"]);
request.getPayload.and.returnValue(payload);
});

expect(request.getPayload).toHaveBeenCalled();
it("adds to request meta if targetMigrationEnabled=true is configured", () => {
const setTargetMigration = createSetTargetMigration({
targetMigrationEnabled: true
});
setTargetMigration(request);
expect(payload.mergeMeta).toHaveBeenCalledOnceWith({
target: { migration: true }
});
});

it("does not add to request meta if targetMigrationEnabled is not configured", () => {
const request = {
getPayload: jasmine.createSpy("getPayload")
};
const config = {};

setMigrationEnabled(config, request);

expect(request.getPayload).not.toHaveBeenCalled();
const setTargetMigration = createSetTargetMigration({
targetMigrationEnabled: false
});
setTargetMigration(request);
expect(payload.mergeMeta).not.toHaveBeenCalled();
});
});
8 changes: 5 additions & 3 deletions test/unit/specs/core/createCookieTransfer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ governing permissions and limitations under the License.
import createCookieTransfer from "../../../../src/core/createCookieTransfer";

describe("createCookieTransfer", () => {
const orgId = "ABC@CustomOrg";
const apexDomain = "example.com";
const endpointDomain = "thirdparty.com";
let shouldTransferCookie;
let payload;
let cookieJar;
let cookieTransfer;
const date = new Date();
const dateProvider = () => date;
const config = { orgId };

beforeEach(() => {
shouldTransferCookie = jasmine.createSpy("shouldTransferCookie");
shouldTransferCookie.and.returnValue(false);
payload = jasmine.createSpyObj("payload", ["mergeState"]);
cookieJar = jasmine.createSpyObj("cookieJar", ["get", "set"]);
cookieTransfer = createCookieTransfer({
cookieJar,
config,
shouldTransferCookie,
apexDomain,
dateProvider
});
Expand Down Expand Up @@ -60,6 +61,7 @@ describe("createCookieTransfer", () => {
at_qa_mode:
'{"token":"QATokenString","listedActivitiesOnly":true,"evaluateAsTrueAudienceIds":["2480042"],"previewIndexes":[{"activityIndex":1,"experienceIndex":1}]}'
});
shouldTransferCookie.and.returnValues(true, false, true, true);
cookieTransfer.cookiesToPayload(payload, endpointDomain);
expect(payload.mergeState).toHaveBeenCalledWith({
domain: apexDomain,
Expand Down
58 changes: 58 additions & 0 deletions test/unit/specs/core/injectShouldTransferCookie.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2022 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

import injectShouldTransferCookie from "../../../../src/core/injectShouldTransferCookie";

describe("shouldTransferCookie", () => {
let targetMigrationEnabled;
let orgId;
let shouldTransferCookie;

beforeEach(() => {
targetMigrationEnabled = false;
orgId = "ABC@CustomOrg";
shouldTransferCookie = null;
});
const build = () => {
shouldTransferCookie = injectShouldTransferCookie({
targetMigrationEnabled,
orgId
});
};

it("returns true if it's at_qa_mode cookie", () => {
build();
expect(shouldTransferCookie("at_qa_mode")).toBeTrue();
});

it("returns true if it's mbox cookie and targetMigrationEnabled=true", () => {
targetMigrationEnabled = true;
build();
expect(shouldTransferCookie("mbox")).toBeTrue();
});

it("returns false if it's mbox cookie and targetMigrationEnabled=false", () => {
build();
expect(shouldTransferCookie("mbox")).toBeFalse();
});

it("returns false if it's not a legacy cookie name", () => {
targetMigrationEnabled = true;
build();
expect(shouldTransferCookie("foo")).toBeFalse();
});

it("returns true for kndctr cookies", () => {
build();
expect(shouldTransferCookie("kndctr_ABC_CustomOrg_mynewcookie")).toBeTrue();
});
});
Loading

0 comments on commit d324204

Please sign in to comment.