Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use applicableSections and supportedAPIs to get consentState #1818

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/twenty-parrots-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/libs': patch
---

Use applicableSections and supportedAPI to get consent state in the US
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,48 @@ describe('getConsentState', () => {
expect(getGPPData).toHaveBeenCalledTimes(1);
expect(doNotSell).toBe(false);
});

it('should return false if the applicableSections is not correctly set - doNotSell is false', async () => {
let applicableSectionsIncorrect = gppDataCanSell;
applicableSectionsIncorrect.applicableSections = [-1];
getGPPData.mockResolvedValue(applicableSectionsIncorrect);

const { doNotSell } = await getConsentState();

expect(getGPPData).toHaveBeenCalledTimes(1);
expect(doNotSell).toBe(false);
});

it('should return false if the supportedAPIs has an incorrect format - doNotSell is false', async () => {
let applicableSectionsIncorrect = gppDataCanSell;
applicableSectionsIncorrect.applicableSections = [-1];
getGPPData.mockResolvedValue(applicableSectionsIncorrect);

const { doNotSell } = await getConsentState();

expect(getGPPData).toHaveBeenCalledTimes(1);
expect(doNotSell).toBe(false);
});

it('should return false if supportedAPIs and parsedSections are different - doNotSell is false', async () => {
let supportedApiParsedSectionMismatch = gppDataCanSell;
supportedApiParsedSectionMismatch.supportedAPIs = ['7:usnat'];
getGPPData.mockResolvedValue(supportedApiParsedSectionMismatch);

const { doNotSell } = await getConsentState();

expect(getGPPData).toHaveBeenCalledTimes(1);
expect(doNotSell).toBe(false);
});

it('should return false if the parsedSections is not correctly set - doNotSell is false', async () => {
let parsedSectionsIncorrect = gppDataCanSell;
parsedSectionsIncorrect.parsedSections = {};
getGPPData.mockResolvedValue(parsedSectionsIncorrect);

const { doNotSell } = await getConsentState();

expect(getGPPData).toHaveBeenCalledTimes(1);
expect(doNotSell).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import { getGPPData } from './api';
export const getConsentState: () => Promise<USNATConsentState> = async () => {
let doNotSell = false; // Opt-Out
const gppData = await getGPPData();
const supportedAPI = Object.values(gppData.parsedSections)[0];

if (supportedAPI) {
// Get applicableSections
const applicableSection = gppData.applicableSections[0]; // e.g. '7' for usnat

// Find the supported API
const supportedAPI = gppData.supportedAPIs.find((api) =>
api.startsWith(`${String(applicableSection)}:`),
); // Find string that contains the applicableSection i.e. (7) in '7:usnat'

// Get parsedSections key and object
const parsedSectionKey = supportedAPI
? supportedAPI.split(':')[1]
: undefined; // i.e. get 'usnat' from '7:usnat'

const parsedSection = parsedSectionKey
? gppData.parsedSections[parsedSectionKey]
: undefined; // Get the gpp consent object with the key

if (parsedSection) {
// https://github.com/InteractiveAdvertisingBureau/Global-Privacy-Platform/blob/main/Sections/US-National/IAB%20Privacy%E2%80%99s%20National%20Privacy%20Technical%20Specification.md
// 0 Not Applicable. SharingOptOutNotice value was not applicable or no notice was provided, 1 Opted Out, 2 Did Not Opt Out
doNotSell = supportedAPI.SaleOptOut !== 2 || supportedAPI.Gpc;
doNotSell = parsedSection.SaleOptOut !== 2 || parsedSection.Gpc;
}

return {
Expand Down