-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathdecoder.test.ts
150 lines (135 loc) · 5.17 KB
/
decoder.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
import {
decodeGppString,
getGppField,
hasGppSection,
} from "../../../src/lib/gpp/decoder";
import { makeStub } from "../../../src/lib/gpp/stub";
import mockDecodedTCFJSON from "../../__fixtures__/mock_decoded_tcf.json";
// Mock fidesDebugger
(globalThis as any).fidesDebugger = jest.fn();
describe("GPP Decoder", () => {
describe("decodeGppString", () => {
it("should decode a US National GPP string", () => {
makeStub();
const gppString = "DBABLA~BVAoAAAAAABk.QA";
const decoded = decodeGppString(gppString);
expect(decoded).toHaveLength(1);
const section = decoded[0];
expect(section).toEqual({
id: 7,
name: "usnat",
data: {
Version: 1,
SharingNotice: 1,
SaleOptOutNotice: 1,
SharingOptOutNotice: 1,
TargetedAdvertisingOptOutNotice: 0,
SensitiveDataProcessingOptOutNotice: 0,
KnownChildSensitiveDataConsents: [0, 0, 0],
PersonalDataConsents: 0,
MspaCoveredTransaction: 1,
MspaOptOutOptionMode: 2,
MspaServiceProviderMode: 1,
SaleOptOut: 2,
SharingOptOut: 2,
TargetedAdvertisingOptOut: 0,
SensitiveDataProcessing: Array(16).fill(0),
Gpc: false,
GpcSegmentType: 1,
SensitiveDataLimitUseNotice: 0,
},
});
});
it("should decode a tcfeuv2 GPP string", () => {
const gppString =
"DBABMA~CQNb38AQNb38AGXABBENBeFgALAAAENAAAAAFyQAQFyAXJABAXIAAAAA";
const decoded = decodeGppString(gppString);
const mockDecodedTCF = mockDecodedTCFJSON as any;
// format the date strings as Date objects
mockDecodedTCF[0].data.Created = new Date(mockDecodedTCF[0].data.Created);
mockDecodedTCF[0].data.LastUpdated = new Date(
mockDecodedTCF[0].data.LastUpdated,
);
expect(decoded).toEqual(mockDecodedTCF);
});
it("should decode a US State (California) GPP string", () => {
const gppString = "DBABBg~BUoAAABg.Q";
const decoded = decodeGppString(gppString);
expect(decoded).toHaveLength(1);
const section = decoded[0];
expect(section).toEqual({
id: 8,
name: "usca",
data: {
Version: 1,
SaleOptOut: 2,
SharingOptOut: 2,
SensitiveDataProcessing: Array(9).fill(0),
KnownChildSensitiveDataConsents: [0, 0],
PersonalDataConsents: 0,
MspaCoveredTransaction: 1,
MspaOptOutOptionMode: 2,
MspaServiceProviderMode: 0,
SaleOptOutNotice: 1,
SharingOptOutNotice: 1,
SensitiveDataLimitUseNotice: 0,
Gpc: false,
GpcSegmentType: 1,
},
});
});
it("should decode a GPP string with multiple sections", () => {
// This string contains both US National and US California sections
const gppString = "DBABrw~BVAUAAAAAABk.QA~BAAAAABA.QA";
const decoded = decodeGppString(gppString);
expect(decoded).toHaveLength(2);
// Check first section (US National)
const usnatSection = decoded[0] as any;
expect(usnatSection.id).toBe(7);
expect(usnatSection.name).toBe("usnat");
expect(usnatSection.data.Version).toBe(1);
expect(usnatSection.data.SharingNotice).toBe(1);
expect(usnatSection.data.SaleOptOut).toBe(1);
expect(usnatSection.data.SharingOptOut).toBe(1);
// Check second section (US California)
const uscaSection = decoded[1] as any;
expect(uscaSection.id).toBe(8);
expect(uscaSection.name).toBe("usca");
expect(uscaSection.data.Version).toBe(1);
expect(uscaSection.data.SaleOptOut).toBe(0);
expect(uscaSection.data.SharingOptOut).toBe(0);
});
it("should handle invalid GPP strings", () => {
expect(() => decodeGppString("invalid")).toThrow();
expect(() => decodeGppString("")).toThrow("GPP string cannot be empty");
expect(() => decodeGppString("DBAA")).not.toThrow(); // Empty but valid GPP string
});
});
describe("getGppField", () => {
it("should get a field value from US National section", () => {
const gppString = "DBABLA~BVAoAAAAAABk.QA";
expect(getGppField(gppString, "usnat", "Version")).toBe(1);
expect(getGppField(gppString, "usnat", "SharingNotice")).toBe(1);
expect(getGppField(gppString, "usnat", "NonexistentField")).toBeNull();
});
it("should handle invalid section names", () => {
const gppString = "DBABLA~BVAoAAAAAABk.QA";
expect(getGppField(gppString, "nonexistent", "Version")).toBeNull();
});
it("should handle invalid GPP strings", () => {
expect(getGppField("invalid", "usnat", "Version")).toBeNull();
expect(getGppField("", "usnat", "Version")).toBeNull();
});
});
describe("hasGppSection", () => {
it("should check for section existence", () => {
const gppString = "DBABBg~BUoAAABg.Q";
expect(hasGppSection(gppString, "usca")).toBe(true);
expect(hasGppSection(gppString, "nonexistent")).toBe(false);
});
it("should handle invalid GPP strings", () => {
expect(hasGppSection("invalid", "usnat")).toBe(false);
expect(hasGppSection("", "usnat")).toBe(false);
});
});
});