Skip to content

Commit

Permalink
Use applicableSections and supportedAPIs to get consentState (#1818)
Browse files Browse the repository at this point in the history
* Use applicableSections and supportedAPIs to get consentState

* Add changeset

* Update @types/node to @types/node

* Update comments

* Change const from applicableSections to applicableSection

Co-authored-by: Ravi <[email protected]>

* Change const from applicableSections to applicableSection

* Extend test to cover failed api

* Update function to find supportedAPI

* Add more tests for getConsentState

---------

Co-authored-by: Ravi <[email protected]>
  • Loading branch information
akinsola-guardian and arelra authored Dec 2, 2024
1 parent c3a02d4 commit 4f6efbd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
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

0 comments on commit 4f6efbd

Please sign in to comment.