Skip to content

Commit

Permalink
address CR concerns, small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
eastandwestwind committed Oct 19, 2023
1 parent 3c13082 commit b1e1278
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 56 deletions.
33 changes: 33 additions & 0 deletions clients/fides-js/src/lib/consent-constants.ts
Original file line number Diff line number Diff line change
@@ -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: /(.*)/,
},
];
38 changes: 0 additions & 38 deletions clients/fides-js/src/lib/consent-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion clients/fides-js/src/lib/consent-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions clients/fides-js/src/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
ConsentMechanism,
ConsentMethod,
EmptyExperience,
FIDES_OVERRIDE_OPTIONS_VALIDATOR_MAP,
FidesConfig,
FidesOptionOverrides,
FidesOptions,
Expand All @@ -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;
Expand Down Expand Up @@ -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<FidesOptionOverrides> => {
const overrideOptions: Partial<FidesOptionOverrides> = {};
if (typeof window !== "undefined") {
const params = new URLSearchParams(document.location.search);
Expand All @@ -172,7 +172,7 @@ export const getOverrideFidesOptions = (): FidesOptionOverrides => {
}
);
}
return <FidesOptionOverrides>overrideOptions;
return overrideOptions;
};

/**
Expand Down
25 changes: 11 additions & 14 deletions clients/privacy-center/cypress/e2e/consent-banner-tcf.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
{
Expand Down

0 comments on commit b1e1278

Please sign in to comment.