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

Fix support for nested options.customOptionsPath #4491

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions clients/fides-js/__tests__/lib/consent-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ describe("getWindowObjFromPath", () => {
window: windowMock1,
expected: undefined,
},
{
label: "nested path does not exist",
path: ["window", "nonexistent-path", "nested"],
window: windowMock1,
expected: undefined,
},
{
label: "path is one level deep",
path: ["window", "fides_overrides"],
Expand Down
32 changes: 29 additions & 3 deletions clients/fides-js/src/lib/consent-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,42 @@ export const shouldResurfaceConsent = (
};

/**
* Get fides override options from a custom path
* Descend down the provided path on the "window" object and return the nested
* override options object located at the given path.
*
* If any part of the path is invalid, return `undefined`.
*
*
* For example, given a window object like this:
* ```
* window.custom_overrides = { nested_obj: { fides_string: "foo" } } };
* ```
*
* Then expect the following:
* ```
* const overrides = getWindowObjFromPath(["window", "custom_overrides", "nested_obj"])
* console.assert(overrides.fides_string === "foo");
* ```
*/
export const getWindowObjFromPath = (
path: string[]
): OverrideOptions | undefined => {
// Implicitly start from the global "window" object
if (path[0] === "window") {
path.shift();
}
// @ts-ignore
return path.reduce((record, item) => record[item], window);
// Descend down the provided path (starting from `window`)
let record: any = window;
while (path.length > 0) {
const key = path.shift();
// If we ever encounter an invalid key or a non-object value, return undefined
if (typeof key === "undefined" || typeof record[key] !== "object") {
return undefined;
}
// Keep descending!
record = record[key];
}
return record;
};

export const getGpcStatusFromNotice = ({
Expand Down
74 changes: 74 additions & 0 deletions clients/privacy-center/cypress/e2e/consent-banner-tcf.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2635,6 +2635,80 @@ describe("Fides-js TCF", () => {
expect(tcData.vendor.legitimateInterests).to.eql({});
});
});

it("does not error when window obj at custom config path doesn't exist", () => {
cy.fixture("consent/experience_tcf.json").then((experience) => {
stubConfig(
{
options: {
isOverlayEnabled: true,
tcfEnabled: true,
customOptionsPath: "window.nonexistent_object",
},
experience: experience.items[0],
},
null,
null,
null,
{ fides_string: "foo" }
);
});
cy.window().then((win) => {
win.__tcfapi("addEventListener", 2, cy.stub().as("TCFEvent"));
// stubConfig will set window.config.overrides = { fides_string: "foo" }
expect(win).to.have.nested.property("config.overrides"); // defined by TEST_OVERRIDE_WINDOW_PATH
// However, customOptionsPath will *try* to read window.nonexistent_object, which will be undefined
expect(win).not.to.have.property("nonexistent_object");
});
// Open the modal
cy.get("#fides-modal-link").click();

// verify CMP API
cy.get("@TCFEvent")
.its("lastCall.args")
.then(([tcData, success]) => {
expect(success).to.eql(true);
expect(tcData.tcString).to.eql("");
expect(tcData.eventStatus).to.eql("cmpuishown");
});
});

it("does not error when window obj at nested custom config path doesn't exist", () => {
cy.fixture("consent/experience_tcf.json").then((experience) => {
stubConfig(
{
options: {
isOverlayEnabled: true,
tcfEnabled: true,
customOptionsPath: "window.nonexistent_object.nested.path",
},
experience: experience.items[0],
},
null,
null,
null,
{ fides_string: "foo" }
);
});
cy.window().then((win) => {
win.__tcfapi("addEventListener", 2, cy.stub().as("TCFEvent"));
// stubConfig will set window.config.overrides = { fides_string: "foo" }
expect(win).to.have.nested.property("config.overrides"); // defined by TEST_OVERRIDE_WINDOW_PATH
// However, customOptionsPath will *try* to read window.nonexistent_object, which will be undefined
expect(win).not.to.have.property("nonexistent_object");
});
// Open the modal
cy.get("#fides-modal-link").click();

// verify CMP API
cy.get("@TCFEvent")
.its("lastCall.args")
.then(([tcData, success]) => {
expect(success).to.eql(true);
expect(tcData.tcString).to.eql("");
expect(tcData.eventStatus).to.eql("cmpuishown");
});
});
});

describe("ac string", () => {
Expand Down