From b1e127897fefeebfaf6cac1d6ea90d6913eec4e2 Mon Sep 17 00:00:00 2001 From: eastandwestwind Date: Thu, 19 Oct 2023 15:39:48 +0200 Subject: [PATCH] address CR concerns, small refactors --- clients/fides-js/src/lib/consent-constants.ts | 33 ++++++++++++++++ clients/fides-js/src/lib/consent-types.ts | 38 ------------------- clients/fides-js/src/lib/consent-utils.ts | 2 +- clients/fides-js/src/lib/initialize.ts | 6 +-- .../cypress/e2e/consent-banner-tcf.cy.ts | 25 ++++++------ 5 files changed, 48 insertions(+), 56 deletions(-) create mode 100644 clients/fides-js/src/lib/consent-constants.ts diff --git a/clients/fides-js/src/lib/consent-constants.ts b/clients/fides-js/src/lib/consent-constants.ts new file mode 100644 index 0000000000..0b0ea7cfba --- /dev/null +++ b/clients/fides-js/src/lib/consent-constants.ts @@ -0,0 +1,33 @@ +import { FidesOptionOverrides, OverrideOptions } from "./consent-types"; + +// Regex to validate a location string, which must: +// 1) Start with a 2-3 character country code (e.g. "US") +// 2) Optionally end with a 2-3 character region code (e.g. "CA") +// 3) Separated by a dash (e.g. "US-CA") +export const VALID_ISO_3166_LOCATION_REGEX = /^\w{2,3}(-\w{2,3})?$/; + +export const FIDES_OVERRIDE_OPTIONS_VALIDATOR_MAP: { + fidesOption: keyof FidesOptionOverrides; + fidesOptionType: "string" | "boolean"; + fidesOverrideKey: keyof OverrideOptions; + validationRegex: RegExp; +}[] = [ + { + fidesOption: "fidesEmbed", + fidesOptionType: "boolean", + fidesOverrideKey: "fides_embed", + validationRegex: /^(true|false)$/, + }, + { + fidesOption: "fidesDisableSaveApi", + fidesOptionType: "boolean", + fidesOverrideKey: "fides_disable_save_api", + validationRegex: /^(true|false)$/, + }, + { + fidesOption: "fidesString", + fidesOptionType: "string", + fidesOverrideKey: "fides_string", + validationRegex: /(.*)/, + }, +]; diff --git a/clients/fides-js/src/lib/consent-types.ts b/clients/fides-js/src/lib/consent-types.ts index 0205c5ef11..3a72a454d6 100644 --- a/clients/fides-js/src/lib/consent-types.ts +++ b/clients/fides-js/src/lib/consent-types.ts @@ -201,12 +201,6 @@ export type UserGeolocation = { region?: string; // "NY" }; -// Regex to validate a location string, which must: -// 1) Start with a 2-3 character country code (e.g. "US") -// 2) Optionally end with a 2-3 character region code (e.g. "CA") -// 3) Separated by a dash (e.g. "US-CA") -export const VALID_ISO_3166_LOCATION_REGEX = /^\w{2,3}(-\w{2,3})?$/; - export type OverrideOptions = { fides_string: string; fides_disable_save_api: boolean; @@ -218,38 +212,6 @@ export type FidesOptionOverrides = Pick< "fidesString" | "fidesDisableSaveApi" | "fidesEmbed" >; -export enum OverrideFidesOption { - FIDES_STRING = "fides_string", - FIDES_DISABLE_SAVE_API = "fides_disable_save_api", - FIDES_EMBED = "fides_embed", -} - -export const FIDES_OVERRIDE_OPTIONS_VALIDATOR_MAP: { - fidesOption: keyof FidesOptionOverrides; - fidesOptionType: "string" | "boolean"; - fidesOverrideKey: keyof OverrideOptions; - validationRegex: RegExp; -}[] = [ - { - fidesOption: "fidesEmbed", - fidesOptionType: "boolean", - fidesOverrideKey: OverrideFidesOption.FIDES_EMBED, - validationRegex: /^(true|false)$/, - }, - { - fidesOption: "fidesDisableSaveApi", - fidesOptionType: "boolean", - fidesOverrideKey: OverrideFidesOption.FIDES_DISABLE_SAVE_API, - validationRegex: /^(true|false)$/, - }, - { - fidesOption: "fidesString", - fidesOptionType: "string", - fidesOverrideKey: OverrideFidesOption.FIDES_STRING, - validationRegex: /(.*)/, - }, -]; - export enum ButtonType { PRIMARY = "primary", SECONDARY = "secondary", diff --git a/clients/fides-js/src/lib/consent-utils.ts b/clients/fides-js/src/lib/consent-utils.ts index 8a543d30c7..e1ef7d029a 100644 --- a/clients/fides-js/src/lib/consent-utils.ts +++ b/clients/fides-js/src/lib/consent-utils.ts @@ -9,10 +9,10 @@ import { PrivacyNotice, UserConsentPreference, UserGeolocation, - VALID_ISO_3166_LOCATION_REGEX, } from "./consent-types"; import { EXPERIENCE_KEYS_WITH_PREFERENCES } from "./tcf/constants"; import { TCFPurposeConsentRecord } from "./tcf/types"; +import { VALID_ISO_3166_LOCATION_REGEX } from "./consent-constants"; /** * Wrapper around 'console.log' that only logs output when the 'debug' banner diff --git a/clients/fides-js/src/lib/initialize.ts b/clients/fides-js/src/lib/initialize.ts index 541223fd85..fcfdf4c3fb 100644 --- a/clients/fides-js/src/lib/initialize.ts +++ b/clients/fides-js/src/lib/initialize.ts @@ -20,7 +20,6 @@ import { ConsentMechanism, ConsentMethod, EmptyExperience, - FIDES_OVERRIDE_OPTIONS_VALIDATOR_MAP, FidesConfig, FidesOptionOverrides, FidesOptions, @@ -43,6 +42,7 @@ import { updateConsentPreferences } from "./preferences"; import { resolveConsentValue } from "./consent-value"; import { initOverlay } from "./consent"; import { TcfCookieConsent } from "./tcf/types"; +import { FIDES_OVERRIDE_OPTIONS_VALIDATOR_MAP } from "./consent-constants"; export type Fides = { consent: CookieKeyConsent; @@ -149,7 +149,7 @@ const automaticallyApplyGPCPreferences = ({ /** * Gets and validates Fides override options provided through URL query params, cookie or window obj. */ -export const getOverrideFidesOptions = (): FidesOptionOverrides => { +export const getOverrideFidesOptions = (): Partial => { const overrideOptions: Partial = {}; if (typeof window !== "undefined") { const params = new URLSearchParams(document.location.search); @@ -172,7 +172,7 @@ export const getOverrideFidesOptions = (): FidesOptionOverrides => { } ); } - return overrideOptions; + return overrideOptions; }; /** diff --git a/clients/privacy-center/cypress/e2e/consent-banner-tcf.cy.ts b/clients/privacy-center/cypress/e2e/consent-banner-tcf.cy.ts index 9919cd4a9e..d021bd3711 100644 --- a/clients/privacy-center/cypress/e2e/consent-banner-tcf.cy.ts +++ b/clients/privacy-center/cypress/e2e/consent-banner-tcf.cy.ts @@ -5,7 +5,6 @@ import { PrivacyExperience, UserConsentPreference, } from "fides-js"; -import { OverrideFidesOption } from "fides-js/src/lib/consent-types"; import { OVERRIDE, stubConfig } from "../support/stubs"; const PURPOSE_2 = { @@ -745,10 +744,8 @@ describe("Fides-js TCF", () => { }); it("skips saving preferences to API when disable save is set via cookie", () => { cy.getCookie(CONSENT_COOKIE_NAME).should("not.exist"); - cy.getCookie(OverrideFidesOption.FIDES_DISABLE_SAVE_API).should( - "not.exist" - ); - cy.setCookie(OverrideFidesOption.FIDES_DISABLE_SAVE_API, "true"); + cy.getCookie("fides_disable_save_api").should("not.exist"); + cy.setCookie("fides_disable_save_api", "true"); cy.fixture("consent/experience_tcf.json").then((experience) => { stubConfig({ options: { @@ -778,7 +775,7 @@ describe("Fides-js TCF", () => { }); }); it("skips saving preferences to API when disable save is set via query param", () => { - cy.getCookie(OverrideFidesOption.FIDES_STRING).should("not.exist"); + cy.getCookie("fides_string").should("not.exist"); cy.fixture("consent/experience_tcf.json").then((experience) => { stubConfig( { @@ -813,7 +810,7 @@ describe("Fides-js TCF", () => { }); }); it("skips saving preferences to API when disable save is set via window obj", () => { - cy.getCookie(OverrideFidesOption.FIDES_STRING).should("not.exist"); + cy.getCookie("fides_string").should("not.exist"); cy.fixture("consent/experience_tcf.json").then((experience) => { stubConfig( { @@ -984,8 +981,8 @@ describe("Fides-js TCF", () => { }); it("automatically renders the second layer when fidesEmbed is set via cookie", () => { cy.getCookie(CONSENT_COOKIE_NAME).should("not.exist"); - cy.getCookie(OverrideFidesOption.FIDES_EMBED).should("not.exist"); - cy.setCookie(OverrideFidesOption.FIDES_EMBED, "true"); + cy.getCookie("fides_embed").should("not.exist"); + cy.setCookie("fides_embed", "true"); cy.fixture("consent/experience_tcf.json").then((experience) => { stubConfig({ options: { @@ -1036,7 +1033,7 @@ describe("Fides-js TCF", () => { }); }); it("automatically renders the second layer when fidesEmbed is set via window obj", () => { - cy.getCookie(OverrideFidesOption.FIDES_STRING).should("not.exist"); + cy.getCookie("fides_string").should("not.exist"); cy.getCookie(CONSENT_COOKIE_NAME).should("not.exist"); cy.fixture("consent/experience_tcf.json").then((experience) => { stubConfig( @@ -1707,10 +1704,10 @@ describe("Fides-js TCF", () => { describe("fides string override options", () => { it("uses TC string when set via cookie", () => { - cy.getCookie(OverrideFidesOption.FIDES_STRING).should("not.exist"); + cy.getCookie("fides_string").should("not.exist"); // this TC string sets purpose 4 to false and purpose 7 to true cy.setCookie( - OverrideFidesOption.FIDES_STRING, + "fides_string", "CPzevcAPzevcAGXABBENATEIAAIAAAAAAAAAAAAAAAAA.IABE" ); cy.fixture("consent/experience_tcf.json").then((experience) => { @@ -1750,7 +1747,7 @@ describe("Fides-js TCF", () => { }); }); it("uses TC string when set via query param", () => { - cy.getCookie(OverrideFidesOption.FIDES_STRING).should("not.exist"); + cy.getCookie("fides_string").should("not.exist"); cy.fixture("consent/experience_tcf.json").then((experience) => { stubConfig( { @@ -1794,7 +1791,7 @@ describe("Fides-js TCF", () => { }); }); it("uses TC string when set via window obj", () => { - cy.getCookie(OverrideFidesOption.FIDES_STRING).should("not.exist"); + cy.getCookie("fides_string").should("not.exist"); cy.fixture("consent/experience_tcf.json").then((experience) => { stubConfig( {