-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HJ-166: Support BlueConic objectives (#5479)
Co-authored-by: Neville Samuell <neville@ethyca.com>
- Loading branch information
1 parent
7ff28bf
commit e660788
Showing
8 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
clients/fides-js/__tests__/integrations/blueconic.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { FidesGlobal } from "../../src/fides"; | ||
import { blueconic } from "../../src/integrations/blueconic"; | ||
import { MARKETING_CONSENT_KEYS } from "../../src/lib/consent-constants"; | ||
|
||
const getBlueConicEvent = () => | ||
({ | ||
subscribe: () => {}, | ||
}) as const; | ||
|
||
const setupBlueConicClient = ( | ||
initialized: "initialized" | "uinitialized" = "initialized", | ||
) => { | ||
const client = { | ||
profile: { | ||
setConsentedObjectives: jest.fn(), | ||
setRefusedObjectives: jest.fn(), | ||
updateProfile: jest.fn(), | ||
}, | ||
event: initialized === "initialized" ? getBlueConicEvent() : undefined, | ||
} as const satisfies typeof window.blueConicClient; | ||
|
||
window.blueConicClient = client; | ||
|
||
return client; | ||
}; | ||
|
||
const setupFidesWithConsent = (key: string, optInStatus: boolean) => { | ||
window.Fides = { | ||
consent: { | ||
[key]: optInStatus, | ||
}, | ||
} as any as FidesGlobal; | ||
}; | ||
|
||
describe("blueconic", () => { | ||
afterEach(() => { | ||
window.blueConicClient = undefined; | ||
window.Fides = undefined as any; | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test("that other modes are not supported", () => { | ||
expect(() => blueconic({ approach: "other mode" as "onetrust" })).toThrow(); | ||
}); | ||
|
||
test("that nothing happens when blueconic and fides are not initialized", () => { | ||
setupBlueConicClient("uinitialized"); | ||
|
||
blueconic(); | ||
|
||
expect( | ||
window.blueConicClient?.profile?.setConsentedObjectives, | ||
).not.toHaveBeenCalled(); | ||
expect( | ||
window.blueConicClient?.profile?.setConsentedObjectives, | ||
).not.toHaveBeenCalled(); | ||
expect( | ||
window.blueConicClient?.profile?.updateProfile, | ||
).not.toHaveBeenCalled(); | ||
}); | ||
|
||
describe.each(MARKETING_CONSENT_KEYS)( | ||
"when consent is set via the %s key", | ||
(key) => { | ||
test.each([ | ||
[ | ||
"opted in", | ||
true, | ||
["iab_purpose_1", "iab_purpose_2", "iab_purpose_3", "iab_purpose_4"], | ||
[], | ||
], | ||
[ | ||
"opted out", | ||
false, | ||
["iab_purpose_1"], | ||
["iab_purpose_2", "iab_purpose_3", "iab_purpose_4"], | ||
], | ||
])( | ||
"that a user who has %s gets the correct consented and refused objectives", | ||
(_, optInStatus, consented, refused) => { | ||
const blueConicClient = setupBlueConicClient(); | ||
setupFidesWithConsent(key, optInStatus); | ||
|
||
blueconic(); | ||
|
||
expect( | ||
blueConicClient.profile.setConsentedObjectives, | ||
).toHaveBeenCalledWith(consented); | ||
expect( | ||
blueConicClient.profile.setRefusedObjectives, | ||
).toHaveBeenCalledWith(refused); | ||
expect(blueConicClient.profile.updateProfile).toHaveBeenCalled(); | ||
}, | ||
); | ||
}, | ||
); | ||
|
||
test.each(["FidesInitialized", "FidesUpdated", "onBlueConicLoaded"])( | ||
"that %s event can cause objectives to be set", | ||
(eventName) => { | ||
const spy = jest.spyOn(window, "addEventListener"); | ||
blueconic(); | ||
expect(spy).toHaveBeenCalledWith(eventName, expect.any(Function)); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { MARKETING_CONSENT_KEYS } from "../lib/consent-constants"; | ||
|
||
declare global { | ||
interface Window { | ||
blueConicClient?: { | ||
profile?: { | ||
setConsentedObjectives: (objectives: string[]) => void; | ||
setRefusedObjectives: (objectives: string[]) => void; | ||
updateProfile: () => void; | ||
}; | ||
event?: { | ||
subscribe: any; | ||
}; | ||
}; | ||
} | ||
} | ||
|
||
// https://support.blueconic.com/hc/en-us/articles/202605221-JavaScript-front-end-API | ||
const blueConicLoaded = () => | ||
typeof window.blueConicClient !== "undefined" && | ||
typeof window.blueConicClient.event !== "undefined" && | ||
typeof window.blueConicClient.event.subscribe !== "undefined"; | ||
|
||
const configureObjectives = () => { | ||
if (!blueConicLoaded() || !window.blueConicClient?.profile) { | ||
return; | ||
} | ||
|
||
const profile = window.blueConicClient?.profile; | ||
const { consent } = window.Fides; | ||
const optedIn = MARKETING_CONSENT_KEYS.some((key) => consent[key]); | ||
if (optedIn) { | ||
profile.setConsentedObjectives([ | ||
"iab_purpose_1", | ||
"iab_purpose_2", | ||
"iab_purpose_3", | ||
"iab_purpose_4", | ||
]); | ||
profile.setRefusedObjectives([]); | ||
} else { | ||
profile.setConsentedObjectives(["iab_purpose_1"]); | ||
profile.setRefusedObjectives([ | ||
"iab_purpose_2", | ||
"iab_purpose_3", | ||
"iab_purpose_4", | ||
]); | ||
} | ||
|
||
profile.updateProfile(); | ||
}; | ||
|
||
export const blueconic = ( | ||
{ approach }: { approach: "onetrust" } = { approach: "onetrust" }, | ||
) => { | ||
if (approach !== "onetrust") { | ||
throw new Error("Unsupported approach"); | ||
} | ||
|
||
window.addEventListener("FidesInitialized", configureObjectives); | ||
window.addEventListener("FidesUpdated", configureObjectives); | ||
window.addEventListener("onBlueConicLoaded", configureObjectives); | ||
|
||
configureObjectives(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters