-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathfides-string.test.ts
206 lines (191 loc) · 5.71 KB
/
fides-string.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { CmpApi } from "@iabgpp/cmpapi";
import {
decodeFidesString,
formatFidesStringWithGpp,
idsFromAcString,
} from "../../src/lib/fides-string";
describe("fidesString", () => {
beforeAll(() => {
window.Fides = {
options: { tcfEnabled: false },
fides_string: undefined,
} as any;
window.fidesDebugger = () => {};
// eslint-disable-next-line no-underscore-dangle
window.__gpp = () => {};
});
beforeEach(() => {
window.Fides.options.tcfEnabled = false;
window.Fides.fides_string = undefined;
});
describe("decodeFidesString", () => {
it.each([
// Empty string
{
fidesString: "",
expected: { tc: "", ac: "", gpp: "" },
},
// TC string only
{
fidesString: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA",
expected: {
tc: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA",
ac: "",
gpp: "",
},
},
// Without vendors disclosed
{
fidesString: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA",
expected: {
tc: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA",
ac: "",
gpp: "",
},
},
// Invalid case of only AC string- need core TC string
{
fidesString: ",1~2.3.4",
expected: { tc: "", ac: "", gpp: "" },
},
// Both TC and AC string
{
fidesString:
"CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA,1~2.3.4",
expected: {
tc: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA",
ac: "1~2.3.4",
gpp: "",
},
},
// GPP string only
{
fidesString: ",,DBABLA~BVAUAAAAAWA.QA",
expected: { tc: "", ac: "", gpp: "DBABLA~BVAUAAAAAWA.QA" },
},
// TC + GPP string
{
fidesString:
"CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA,,DBABLA~BVAUAAAAAWA.QA",
expected: {
tc: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA",
ac: "",
gpp: "DBABLA~BVAUAAAAAWA.QA",
},
},
// Complete string (TC + AC + GPP)
{
fidesString:
"CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA,1~2.3.4,DBABLA~BVAUAAAAAWA.QA",
expected: {
tc: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA",
ac: "1~2.3.4",
gpp: "DBABLA~BVAUAAAAAWA.QA",
},
},
// No trailing comma when no GPP
{
fidesString:
"CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA,1~2.3.4",
expected: {
tc: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA",
ac: "1~2.3.4",
gpp: "",
},
},
// With extra unexpected stuff
{
fidesString:
"CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA,1~2.3.4,DBABLA~BVAUAAAAAWA.QA,extrastuff",
expected: {
tc: "CPzvOIAPzvOIAGXABBENAUEAAACAAAAAAAAAAAAAAAAA.IAAA",
ac: "1~2.3.4",
gpp: "DBABLA~BVAUAAAAAWA.QA",
},
},
])(
"can decode a fides string of varying formats",
({ fidesString, expected }) => {
const result = decodeFidesString(fidesString);
expect(result).toEqual(expected);
},
);
});
describe("idsFromAcString", () => {
it.each([
// Empty string
{
acString: "",
expected: [],
},
// String without ids
{
acString: "1~",
expected: [],
},
// Invalid string
{
acString: "invalid",
expected: [],
},
// Proper string
{
acString: "1~1.2.3",
expected: ["gacp.1", "gacp.2", "gacp.3"],
},
])(
"can decode a fides string of varying formats",
({ acString, expected }) => {
const result = idsFromAcString(acString);
expect(result).toEqual(expected);
},
);
});
describe("formatFidesStringWithGpp", () => {
describe("in a non-TCF experience", () => {
beforeEach(() => {
window.Fides.options.tcfEnabled = false;
});
it("formats GPP string with empty TC and AC parts", () => {
const cmpApi = new CmpApi(1, 1);
// Mock the GPP string
jest
.spyOn(cmpApi, "getGppString")
.mockReturnValue("DBABLA~BVAUAAAAAWA.QA");
const result = formatFidesStringWithGpp(cmpApi);
expect(result).toBe(",,DBABLA~BVAUAAAAAWA.QA");
});
});
describe("in a TCF experience", () => {
beforeEach(() => {
window.Fides.options.tcfEnabled = true;
});
it("appends GPP string to existing TC and AC strings", () => {
const cmpApi = new CmpApi(1, 1);
window.Fides.fides_string = "CPzvOIA.IAAA,1~2.3.4";
jest
.spyOn(cmpApi, "getGppString")
.mockReturnValue("DBABLA~BVAUAAAAAWA.QA");
const result = formatFidesStringWithGpp(cmpApi);
expect(result).toBe("CPzvOIA.IAAA,1~2.3.4,DBABLA~BVAUAAAAAWA.QA");
});
it("appends GPP string to TC string when AC string is empty", () => {
const cmpApi = new CmpApi(1, 1);
window.Fides.fides_string = "CPzvOIA.IAAA,";
jest
.spyOn(cmpApi, "getGppString")
.mockReturnValue("DBABLA~BVAUAAAAAWA.QA");
const result = formatFidesStringWithGpp(cmpApi);
expect(result).toBe("CPzvOIA.IAAA,,DBABLA~BVAUAAAAAWA.QA");
});
it("appends GPP string when no existing fides_string", () => {
const cmpApi = new CmpApi(1, 1);
jest
.spyOn(cmpApi, "getGppString")
.mockReturnValue("DBABLA~BVAUAAAAAWA.QA");
const result = formatFidesStringWithGpp(cmpApi);
expect(result).toBe(",,DBABLA~BVAUAAAAAWA.QA");
});
});
});
});