@financial-times/privacy-us-privacy 1.2.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +20 -12
- package/dist/cjs/main.d.ts +0 -26
- package/dist/cjs/main.js +0 -94
- package/dist/esm/main.d.ts +0 -26
- package/dist/esm/main.js +0 -86
- package/src/main.ts +0 -114
- package/src/test/main.test.ts +0 -230
- package/tsconfig.json +0 -6
package/package.json
CHANGED
|
@@ -1,25 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@financial-times/privacy-us-privacy",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "
|
|
5
|
-
"main": "dist/cjs/main.js",
|
|
6
|
-
"module": "dist/esm/main.js",
|
|
7
|
-
"types": "dist/cjs/main.d.ts",
|
|
4
|
+
"version": "2.0.2",
|
|
8
5
|
"private": false,
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./dist/main.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/privacy-us-privacy.js",
|
|
14
|
+
"require": "./dist/privacy-us-privacy.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
9
17
|
"devDependencies": {
|
|
10
|
-
"@types/js-cookie": "^3.0.
|
|
18
|
+
"@types/js-cookie": "^3.0.5",
|
|
11
19
|
"js-cookie": "^3.0.5",
|
|
12
|
-
"
|
|
20
|
+
"jsdom": "^22.1.0",
|
|
21
|
+
"npm-run-all": "4.1.5",
|
|
22
|
+
"vite": "^4.5.0",
|
|
23
|
+
"vite-plugin-dts": "^3.6.0",
|
|
24
|
+
"vitest": "^0.34.6"
|
|
13
25
|
},
|
|
14
26
|
"dependencies": {
|
|
15
27
|
"@financial-times/privacy-legislation-client": "file:../legislation-client"
|
|
16
28
|
},
|
|
17
29
|
"scripts": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"build:browser": "npx tsc --module es2015 --outDir ./dist/esm",
|
|
21
|
-
"build:node": "npx tsc --module commonjs --outDir ./dist/cjs",
|
|
22
|
-
"prebuild": "npm run clean",
|
|
23
|
-
"build": "run-p build:*"
|
|
30
|
+
"build": "vite build",
|
|
31
|
+
"test": "vitest"
|
|
24
32
|
}
|
|
25
33
|
}
|
package/dist/cjs/main.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export declare const NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
2
|
-
declare global {
|
|
3
|
-
interface Window {
|
|
4
|
-
__uspapi?: (command: string, version: number, callback?: Function) => Promise<unknown>;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export declare function getUsPrivacyCookie(): string | null;
|
|
8
|
-
/**
|
|
9
|
-
* @return {string}
|
|
10
|
-
*/
|
|
11
|
-
export declare function getUsPrivacyForTracking(): string;
|
|
12
|
-
export declare function __uspapi(command: string, version: number, callback?: Function): Promise<void>;
|
|
13
|
-
/**
|
|
14
|
-
* Expose a javascript function on the global `window` object that can be called
|
|
15
|
-
* by third party scripts to determine if the current user has opted out of CCPA
|
|
16
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/USP%20API.md
|
|
17
|
-
*/
|
|
18
|
-
export declare function setUSPrivacy(): void;
|
|
19
|
-
/**
|
|
20
|
-
* Set a cookie based on the IAB US Privacy spec
|
|
21
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/US%20Privacy%20String.md
|
|
22
|
-
*
|
|
23
|
-
* @param consent - Does the user have consent for CCPA
|
|
24
|
-
* @param domain - An optional explicit domain for the cookie
|
|
25
|
-
*/
|
|
26
|
-
export declare function setUSPrivacyCookie(consent: boolean, domain?: string): void;
|
package/dist/cjs/main.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.setUSPrivacyCookie = exports.setUSPrivacy = exports.__uspapi = exports.getUsPrivacyForTracking = exports.getUsPrivacyCookie = exports.NO_PRIVACY_COOKIE = void 0;
|
|
5
|
-
const privacy_legislation_client_1 = require("@financial-times/privacy-legislation-client");
|
|
6
|
-
exports.NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
7
|
-
function getUsPrivacyCookie() {
|
|
8
|
-
const uspCookie = document.cookie.split("; ").find((row) => row.startsWith("usprivacy"));
|
|
9
|
-
if (!uspCookie) {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
return uspCookie.split("=")[1];
|
|
13
|
-
}
|
|
14
|
-
exports.getUsPrivacyCookie = getUsPrivacyCookie;
|
|
15
|
-
/**
|
|
16
|
-
* @return {string}
|
|
17
|
-
*/
|
|
18
|
-
function getUsPrivacyForTracking() {
|
|
19
|
-
return getUsPrivacyCookie() || exports.NO_PRIVACY_COOKIE;
|
|
20
|
-
}
|
|
21
|
-
exports.getUsPrivacyForTracking = getUsPrivacyForTracking;
|
|
22
|
-
async function __uspapi(command, version, callback) {
|
|
23
|
-
if (typeof callback !== "function") {
|
|
24
|
-
console.error("Please provide a valid callback to the __uspapi() function. __uspapi(command, version, callback)"); //eslint-disable-line no-console
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
if (version !== 1 || command !== "getUSPData") {
|
|
28
|
-
console.error("Invalid version or command specified for the __uspapi() function"); //eslint-disable-line no-console
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
const uspCookie = getUsPrivacyCookie();
|
|
33
|
-
// User has visited the CCPA page and toggled CCPA on/off.
|
|
34
|
-
// Values in the usprivacy cookie should be either '1YYN' or '1YNN'
|
|
35
|
-
if (uspCookie) {
|
|
36
|
-
callback({
|
|
37
|
-
version,
|
|
38
|
-
uspString: uspCookie,
|
|
39
|
-
}, true);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
// No cookie, assume user hasn't ever visited the CCPA page
|
|
43
|
-
else {
|
|
44
|
-
const { legislation } = await (0, privacy_legislation_client_1.fetchLegislation)();
|
|
45
|
-
if (legislation.has("ccpa")) {
|
|
46
|
-
callback({
|
|
47
|
-
version,
|
|
48
|
-
uspString: "1NNN",
|
|
49
|
-
}, true);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
// CCPA does not apply
|
|
54
|
-
callback({
|
|
55
|
-
version,
|
|
56
|
-
uspString: "1---",
|
|
57
|
-
}, true);
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
63
|
-
callback(undefined, false);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
exports.__uspapi = __uspapi;
|
|
67
|
-
/**
|
|
68
|
-
* Expose a javascript function on the global `window` object that can be called
|
|
69
|
-
* by third party scripts to determine if the current user has opted out of CCPA
|
|
70
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/USP%20API.md
|
|
71
|
-
*/
|
|
72
|
-
function setUSPrivacy() {
|
|
73
|
-
if (typeof window.__uspapi === "function") {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
window.__uspapi = __uspapi;
|
|
77
|
-
}
|
|
78
|
-
exports.setUSPrivacy = setUSPrivacy;
|
|
79
|
-
/**
|
|
80
|
-
* Set a cookie based on the IAB US Privacy spec
|
|
81
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/US%20Privacy%20String.md
|
|
82
|
-
*
|
|
83
|
-
* @param consent - Does the user have consent for CCPA
|
|
84
|
-
* @param domain - An optional explicit domain for the cookie
|
|
85
|
-
*/
|
|
86
|
-
function setUSPrivacyCookie(consent, domain) {
|
|
87
|
-
// The problem of removing any subdomain from a hostname is relatively involved,
|
|
88
|
-
// and we are not trying to make it this work for every possible subdomain yet
|
|
89
|
-
domain = domain || (document.location.hostname || "").replace("www.", "");
|
|
90
|
-
const uspString = `1Y${consent ? "N" : "Y"}N`;
|
|
91
|
-
const maxAge = 60 * 60 * 24 * 365 * 5;
|
|
92
|
-
document.cookie = `usprivacy=${uspString};max-age=${maxAge};path=/;domain=${domain}`;
|
|
93
|
-
}
|
|
94
|
-
exports.setUSPrivacyCookie = setUSPrivacyCookie;
|
package/dist/esm/main.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export declare const NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
2
|
-
declare global {
|
|
3
|
-
interface Window {
|
|
4
|
-
__uspapi?: (command: string, version: number, callback?: Function) => Promise<unknown>;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export declare function getUsPrivacyCookie(): string | null;
|
|
8
|
-
/**
|
|
9
|
-
* @return {string}
|
|
10
|
-
*/
|
|
11
|
-
export declare function getUsPrivacyForTracking(): string;
|
|
12
|
-
export declare function __uspapi(command: string, version: number, callback?: Function): Promise<void>;
|
|
13
|
-
/**
|
|
14
|
-
* Expose a javascript function on the global `window` object that can be called
|
|
15
|
-
* by third party scripts to determine if the current user has opted out of CCPA
|
|
16
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/USP%20API.md
|
|
17
|
-
*/
|
|
18
|
-
export declare function setUSPrivacy(): void;
|
|
19
|
-
/**
|
|
20
|
-
* Set a cookie based on the IAB US Privacy spec
|
|
21
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/US%20Privacy%20String.md
|
|
22
|
-
*
|
|
23
|
-
* @param consent - Does the user have consent for CCPA
|
|
24
|
-
* @param domain - An optional explicit domain for the cookie
|
|
25
|
-
*/
|
|
26
|
-
export declare function setUSPrivacyCookie(consent: boolean, domain?: string): void;
|
package/dist/esm/main.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
-
import { fetchLegislation } from "@financial-times/privacy-legislation-client";
|
|
3
|
-
export const NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
4
|
-
export function getUsPrivacyCookie() {
|
|
5
|
-
const uspCookie = document.cookie.split("; ").find((row) => row.startsWith("usprivacy"));
|
|
6
|
-
if (!uspCookie) {
|
|
7
|
-
return null;
|
|
8
|
-
}
|
|
9
|
-
return uspCookie.split("=")[1];
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* @return {string}
|
|
13
|
-
*/
|
|
14
|
-
export function getUsPrivacyForTracking() {
|
|
15
|
-
return getUsPrivacyCookie() || NO_PRIVACY_COOKIE;
|
|
16
|
-
}
|
|
17
|
-
export async function __uspapi(command, version, callback) {
|
|
18
|
-
if (typeof callback !== "function") {
|
|
19
|
-
console.error("Please provide a valid callback to the __uspapi() function. __uspapi(command, version, callback)"); //eslint-disable-line no-console
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
if (version !== 1 || command !== "getUSPData") {
|
|
23
|
-
console.error("Invalid version or command specified for the __uspapi() function"); //eslint-disable-line no-console
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
try {
|
|
27
|
-
const uspCookie = getUsPrivacyCookie();
|
|
28
|
-
// User has visited the CCPA page and toggled CCPA on/off.
|
|
29
|
-
// Values in the usprivacy cookie should be either '1YYN' or '1YNN'
|
|
30
|
-
if (uspCookie) {
|
|
31
|
-
callback({
|
|
32
|
-
version,
|
|
33
|
-
uspString: uspCookie,
|
|
34
|
-
}, true);
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
// No cookie, assume user hasn't ever visited the CCPA page
|
|
38
|
-
else {
|
|
39
|
-
const { legislation } = await fetchLegislation();
|
|
40
|
-
if (legislation.has("ccpa")) {
|
|
41
|
-
callback({
|
|
42
|
-
version,
|
|
43
|
-
uspString: "1NNN",
|
|
44
|
-
}, true);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
// CCPA does not apply
|
|
49
|
-
callback({
|
|
50
|
-
version,
|
|
51
|
-
uspString: "1---",
|
|
52
|
-
}, true);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
catch (e) {
|
|
58
|
-
callback(undefined, false);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Expose a javascript function on the global `window` object that can be called
|
|
63
|
-
* by third party scripts to determine if the current user has opted out of CCPA
|
|
64
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/USP%20API.md
|
|
65
|
-
*/
|
|
66
|
-
export function setUSPrivacy() {
|
|
67
|
-
if (typeof window.__uspapi === "function") {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
window.__uspapi = __uspapi;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Set a cookie based on the IAB US Privacy spec
|
|
74
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/US%20Privacy%20String.md
|
|
75
|
-
*
|
|
76
|
-
* @param consent - Does the user have consent for CCPA
|
|
77
|
-
* @param domain - An optional explicit domain for the cookie
|
|
78
|
-
*/
|
|
79
|
-
export function setUSPrivacyCookie(consent, domain) {
|
|
80
|
-
// The problem of removing any subdomain from a hostname is relatively involved,
|
|
81
|
-
// and we are not trying to make it this work for every possible subdomain yet
|
|
82
|
-
domain = domain || (document.location.hostname || "").replace("www.", "");
|
|
83
|
-
const uspString = `1Y${consent ? "N" : "Y"}N`;
|
|
84
|
-
const maxAge = 60 * 60 * 24 * 365 * 5;
|
|
85
|
-
document.cookie = `usprivacy=${uspString};max-age=${maxAge};path=/;domain=${domain}`;
|
|
86
|
-
}
|
package/src/main.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
-
|
|
3
|
-
import { fetchLegislation } from "@financial-times/privacy-legislation-client";
|
|
4
|
-
|
|
5
|
-
export const NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
6
|
-
|
|
7
|
-
declare global {
|
|
8
|
-
interface Window {
|
|
9
|
-
__uspapi?: (command: string, version: number, callback?: Function) => Promise<unknown>;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function getUsPrivacyCookie(): string | null {
|
|
14
|
-
const uspCookie = document.cookie.split("; ").find((row) => row.startsWith("usprivacy"));
|
|
15
|
-
if (!uspCookie) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
return uspCookie.split("=")[1];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @return {string}
|
|
23
|
-
*/
|
|
24
|
-
export function getUsPrivacyForTracking() {
|
|
25
|
-
return getUsPrivacyCookie() || NO_PRIVACY_COOKIE;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function __uspapi(
|
|
29
|
-
command: string,
|
|
30
|
-
version: number,
|
|
31
|
-
callback?: Function
|
|
32
|
-
): Promise<void> {
|
|
33
|
-
if (typeof callback !== "function") {
|
|
34
|
-
console.error(
|
|
35
|
-
"Please provide a valid callback to the __uspapi() function. __uspapi(command, version, callback)"
|
|
36
|
-
); //eslint-disable-line no-console
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (version !== 1 || command !== "getUSPData") {
|
|
41
|
-
console.error("Invalid version or command specified for the __uspapi() function"); //eslint-disable-line no-console
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const uspCookie = getUsPrivacyCookie();
|
|
47
|
-
// User has visited the CCPA page and toggled CCPA on/off.
|
|
48
|
-
// Values in the usprivacy cookie should be either '1YYN' or '1YNN'
|
|
49
|
-
if (uspCookie) {
|
|
50
|
-
callback(
|
|
51
|
-
{
|
|
52
|
-
version,
|
|
53
|
-
uspString: uspCookie,
|
|
54
|
-
},
|
|
55
|
-
true
|
|
56
|
-
);
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
// No cookie, assume user hasn't ever visited the CCPA page
|
|
60
|
-
else {
|
|
61
|
-
const { legislation } = await fetchLegislation();
|
|
62
|
-
if (legislation.has("ccpa")) {
|
|
63
|
-
callback(
|
|
64
|
-
{
|
|
65
|
-
version,
|
|
66
|
-
uspString: "1NNN",
|
|
67
|
-
},
|
|
68
|
-
true
|
|
69
|
-
);
|
|
70
|
-
return;
|
|
71
|
-
} else {
|
|
72
|
-
// CCPA does not apply
|
|
73
|
-
callback(
|
|
74
|
-
{
|
|
75
|
-
version,
|
|
76
|
-
uspString: "1---",
|
|
77
|
-
},
|
|
78
|
-
true
|
|
79
|
-
);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
} catch (e) {
|
|
84
|
-
callback(undefined, false);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Expose a javascript function on the global `window` object that can be called
|
|
90
|
-
* by third party scripts to determine if the current user has opted out of CCPA
|
|
91
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/USP%20API.md
|
|
92
|
-
*/
|
|
93
|
-
export function setUSPrivacy(): void {
|
|
94
|
-
if (typeof window.__uspapi === "function") {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
window.__uspapi = __uspapi;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Set a cookie based on the IAB US Privacy spec
|
|
102
|
-
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/US%20Privacy%20String.md
|
|
103
|
-
*
|
|
104
|
-
* @param consent - Does the user have consent for CCPA
|
|
105
|
-
* @param domain - An optional explicit domain for the cookie
|
|
106
|
-
*/
|
|
107
|
-
export function setUSPrivacyCookie(consent: boolean, domain?: string): void {
|
|
108
|
-
// The problem of removing any subdomain from a hostname is relatively involved,
|
|
109
|
-
// and we are not trying to make it this work for every possible subdomain yet
|
|
110
|
-
domain = domain || (document.location.hostname || "").replace("www.", "");
|
|
111
|
-
const uspString = `1Y${consent ? "N" : "Y"}N`;
|
|
112
|
-
const maxAge = 60 * 60 * 24 * 365 * 5;
|
|
113
|
-
document.cookie = `usprivacy=${uspString};max-age=${maxAge};path=/;domain=${domain}`;
|
|
114
|
-
}
|
package/src/test/main.test.ts
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { RegionalCompliance } from "../../../legislation-client/src/main";
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
setUSPrivacy,
|
|
9
|
-
setUSPrivacyCookie,
|
|
10
|
-
getUsPrivacyForTracking,
|
|
11
|
-
getUsPrivacyCookie,
|
|
12
|
-
NO_PRIVACY_COOKIE,
|
|
13
|
-
} from "../main";
|
|
14
|
-
import * as legislationClient from "@financial-times/privacy-legislation-client";
|
|
15
|
-
import Cookies from "js-cookie";
|
|
16
|
-
|
|
17
|
-
const mockFetchLegislationCcpa = (): Promise<RegionalCompliance> =>
|
|
18
|
-
Promise.resolve({
|
|
19
|
-
region: "CA",
|
|
20
|
-
legislation: new Set(["ccpa"]),
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const mockFetchLegislationNotCcpa = (): Promise<RegionalCompliance> =>
|
|
24
|
-
Promise.resolve({
|
|
25
|
-
region: "GB",
|
|
26
|
-
legislation: new Set(["gdpr"]),
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe("US Privacy", () => {
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
jest.spyOn(console, "error").mockImplementation((arg) => arg);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
afterEach(() => {
|
|
35
|
-
jest.resetAllMocks();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("should not overwrite an existing window.__uspapi?.() function", async () => {
|
|
39
|
-
window.__uspapi = (): Promise<string> => Promise.resolve("just testing");
|
|
40
|
-
await setUSPrivacy();
|
|
41
|
-
expect(await window.__uspapi?.("test command", 1)).toEqual("just testing");
|
|
42
|
-
delete window.__uspapi;
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
describe("window.__uspapi?.()", () => {
|
|
46
|
-
test("should console.error and return if called with command != `getUSPData`", () => {
|
|
47
|
-
jest.spyOn(legislationClient, "fetchLegislation");
|
|
48
|
-
const cb = jest.fn();
|
|
49
|
-
setUSPrivacy();
|
|
50
|
-
window.__uspapi?.("fake-command", 1, cb);
|
|
51
|
-
expect(console.error).toBeCalled();
|
|
52
|
-
expect(legislationClient.fetchLegislation).not.toBeCalled();
|
|
53
|
-
expect(cb).not.toBeCalled();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("should console.error if called with version != 1", () => {
|
|
57
|
-
jest.spyOn(legislationClient, "fetchLegislation");
|
|
58
|
-
const cb = jest.fn();
|
|
59
|
-
setUSPrivacy();
|
|
60
|
-
window.__uspapi?.("getUSPData", 2, cb);
|
|
61
|
-
expect(console.error).toBeCalled();
|
|
62
|
-
expect(legislationClient.fetchLegislation).not.toBeCalled();
|
|
63
|
-
expect(cb).not.toBeCalled();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test("should console.error if callback is not defined", () => {
|
|
67
|
-
jest.spyOn(legislationClient, "fetchLegislation");
|
|
68
|
-
setUSPrivacy();
|
|
69
|
-
window.__uspapi?.("getUSPData", 1);
|
|
70
|
-
expect(console.error).toBeCalled();
|
|
71
|
-
expect(legislationClient.fetchLegislation).not.toBeCalled();
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
describe("When legislation information is not available", () => {
|
|
75
|
-
test("should execute the callback with (undefined, false) as it's arguments", () => {
|
|
76
|
-
jest.spyOn(legislationClient, "fetchLegislation").mockImplementation(() => {
|
|
77
|
-
throw new Error("Failed to get legislation data");
|
|
78
|
-
});
|
|
79
|
-
const cb = jest.fn();
|
|
80
|
-
setUSPrivacy();
|
|
81
|
-
window.__uspapi?.("getUSPData", 1, cb);
|
|
82
|
-
expect(cb).toBeCalledWith(undefined, false);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe("Legislation is NOT CCPA", () => {
|
|
87
|
-
describe("`usprivacy` cookie is set", () => {
|
|
88
|
-
test("should execute the callback with { version: 1, uspString:`1---` }", async () => {
|
|
89
|
-
jest
|
|
90
|
-
.spyOn(legislationClient, "fetchLegislation")
|
|
91
|
-
.mockImplementation(mockFetchLegislationNotCcpa);
|
|
92
|
-
Cookies.set("usprivacy", "fake-value");
|
|
93
|
-
const cb = jest.fn();
|
|
94
|
-
setUSPrivacy();
|
|
95
|
-
await window.__uspapi?.("getUSPData", 1, cb);
|
|
96
|
-
expect(cb).toBeCalledWith(
|
|
97
|
-
{
|
|
98
|
-
version: 1,
|
|
99
|
-
uspString: "fake-value",
|
|
100
|
-
},
|
|
101
|
-
true
|
|
102
|
-
);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
describe("`usprivacy` cookie is NOT set", () => {
|
|
107
|
-
test("should execute the callback with { version: 1, uspString:`1---` }", async () => {
|
|
108
|
-
jest
|
|
109
|
-
.spyOn(legislationClient, "fetchLegislation")
|
|
110
|
-
.mockImplementation(mockFetchLegislationNotCcpa);
|
|
111
|
-
Cookies.remove("usprivacy");
|
|
112
|
-
const cb = jest.fn();
|
|
113
|
-
setUSPrivacy();
|
|
114
|
-
await window.__uspapi?.("getUSPData", 1, cb);
|
|
115
|
-
expect(cb).toBeCalledWith(
|
|
116
|
-
{
|
|
117
|
-
version: 1,
|
|
118
|
-
uspString: "1---",
|
|
119
|
-
},
|
|
120
|
-
true
|
|
121
|
-
);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe("Legislation is CCPA", () => {
|
|
127
|
-
describe("`usprivacy` cookie is set", () => {
|
|
128
|
-
test("should execute the callback with the value of the cookie", async () => {
|
|
129
|
-
jest
|
|
130
|
-
.spyOn(legislationClient, "fetchLegislation")
|
|
131
|
-
.mockImplementation(mockFetchLegislationCcpa);
|
|
132
|
-
const cb = jest.fn();
|
|
133
|
-
Cookies.set("usprivacy", "fake-value");
|
|
134
|
-
setUSPrivacy();
|
|
135
|
-
await window.__uspapi?.("getUSPData", 1, cb);
|
|
136
|
-
expect(cb).toBeCalledWith(
|
|
137
|
-
{
|
|
138
|
-
version: 1,
|
|
139
|
-
uspString: "fake-value",
|
|
140
|
-
},
|
|
141
|
-
true
|
|
142
|
-
);
|
|
143
|
-
expect(cb.mock.calls.length).toEqual(1);
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
describe("`usprivacy` cookie is NOT set", () => {
|
|
147
|
-
test("should execute the callback with us privacy string `1NNN`", async () => {
|
|
148
|
-
jest
|
|
149
|
-
.spyOn(legislationClient, "fetchLegislation")
|
|
150
|
-
.mockImplementation(mockFetchLegislationCcpa);
|
|
151
|
-
const cb = jest.fn();
|
|
152
|
-
Cookies.remove("usprivacy");
|
|
153
|
-
setUSPrivacy();
|
|
154
|
-
await window.__uspapi?.("getUSPData", 1, cb);
|
|
155
|
-
expect(cb).toBeCalledWith(
|
|
156
|
-
{
|
|
157
|
-
version: 1,
|
|
158
|
-
uspString: "1NNN",
|
|
159
|
-
},
|
|
160
|
-
true
|
|
161
|
-
);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
describe("Set and Get CCPA consent cookie", () => {
|
|
168
|
-
beforeEach(() => Cookies.remove("usprivacy", { path: "/", domain: "ft.com" }));
|
|
169
|
-
|
|
170
|
-
describe("User has given CCPA consent (opten in)", () => {
|
|
171
|
-
test("should set the `usprivacy` cookie to `1YNN`", () => {
|
|
172
|
-
const consentGivenCode = "1YNN";
|
|
173
|
-
setUSPrivacyCookie(true);
|
|
174
|
-
expect(Cookies.get("usprivacy")).toEqual(consentGivenCode);
|
|
175
|
-
expect(getUsPrivacyCookie()).toEqual(consentGivenCode);
|
|
176
|
-
expect(getUsPrivacyForTracking()).toEqual(consentGivenCode);
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
describe("User has NOT given CCPA consent (opted out)", () => {
|
|
181
|
-
test("should set the `usprivacy` cookie to `1YYN`", () => {
|
|
182
|
-
const consentNotGivenCode = "1YYN";
|
|
183
|
-
setUSPrivacyCookie(false);
|
|
184
|
-
expect(Cookies.get("usprivacy")).toEqual(consentNotGivenCode);
|
|
185
|
-
expect(getUsPrivacyCookie()).toEqual(consentNotGivenCode);
|
|
186
|
-
expect(getUsPrivacyForTracking()).toEqual(consentNotGivenCode);
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
describe("User has neither accepted nor denied CCPA consent (cookie not found)", () => {
|
|
191
|
-
test("`usprivacy` has not ben set", () => {
|
|
192
|
-
//expect(Cookies.get("usprivacy")).toEqual(null);
|
|
193
|
-
expect(getUsPrivacyCookie()).toEqual(null);
|
|
194
|
-
expect(getUsPrivacyForTracking()).toEqual(NO_PRIVACY_COOKIE);
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
describe("when providing an explicit cookie domain", () => {
|
|
199
|
-
let setCookieSpy: jest.SpyInstance;
|
|
200
|
-
|
|
201
|
-
beforeAll(() => {
|
|
202
|
-
setCookieSpy = jest.spyOn(document, "cookie", "set");
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
afterAll(() => {
|
|
206
|
-
setCookieSpy.mockRestore();
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
describe("User has given CCPA consent (opted in)", () => {
|
|
210
|
-
test("should set the `usprivacy` cookie to `1YNN` under the specified domain", () => {
|
|
211
|
-
setUSPrivacyCookie(true, ".ftadviser.com");
|
|
212
|
-
expect(setCookieSpy).toHaveBeenCalledWith(
|
|
213
|
-
// max-age should be 31536000 seconds (1 year)
|
|
214
|
-
"usprivacy=1YNN;max-age=157680000;path=/;domain=.ftadviser.com"
|
|
215
|
-
);
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
describe("User has NOT given CCPA consent (opted out)", () => {
|
|
220
|
-
test("should set the `usprivacy` cookie to `1YYN` under the specified domain", () => {
|
|
221
|
-
setUSPrivacyCookie(false, ".ftadviser.com");
|
|
222
|
-
// max-age should be 31536000 seconds (1 year)
|
|
223
|
-
expect(setCookieSpy).toHaveBeenCalledWith(
|
|
224
|
-
"usprivacy=1YYN;max-age=157680000;path=/;domain=.ftadviser.com"
|
|
225
|
-
);
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
});
|