diff --git a/.changeset/twenty-parrots-help.md b/.changeset/twenty-parrots-help.md new file mode 100644 index 000000000..54d7f0d9a --- /dev/null +++ b/.changeset/twenty-parrots-help.md @@ -0,0 +1,5 @@ +--- +'@guardian/libs': patch +--- + +Use applicableSections and supportedAPI to get consent state in the US diff --git a/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.test.js b/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.test.js index a3bca6514..d9f5c171f 100644 --- a/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.test.js +++ b/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.test.js @@ -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); + }); }); diff --git a/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.ts b/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.ts index 899b72396..abfa83093 100644 --- a/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.ts +++ b/libs/@guardian/libs/src/consent-management-platform/usnat/getConsentState.ts @@ -4,12 +4,28 @@ import { getGPPData } from './api'; export const getConsentState: () => Promise = 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 {