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

Don't send anonymous consent to the ConsentProxy #670

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 18 additions & 28 deletions components/x-privacy-manager/src/__tests__/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('../../typings/x-privacy-manager').BasePrivacyManagerProps} BasePrivacyManagerProps
*/

const React = require('react')

// eslint-disable-next-line no-unused-vars
Expand All @@ -10,49 +14,35 @@ export const CONSENT_PROXY_HOST = 'https://consent.ft.com'
export const CONSENT_PROXY_ENDPOINT = 'https://consent.ft.com/__consent/consent-record/FTPINK/abcde'

/**
*
* @param {{
* setConsentCookie: boolean,
* consent: boolean
* }}
* @returns
*/
export const buildPayload = ({ setConsentCookie, consent }) => {
const categoryPayload = {
onsite: {
fow: 'privacyCCPA/H0IeyQBalorD.6nTqqzhNTKECSgOPJCG',
lbi: true,
source: 'consuming-app',
status: consent
}
}
return {
setConsentCookie,
consentSource: 'consuming-app',
data: {
behaviouralAds: {
onsite: {
fow: 'privacyCCPA/H0IeyQBalorD.6nTqqzhNTKECSgOPJCG',
lbi: true,
source: 'consuming-app',
status: consent
}
},
demographicAds: {
onsite: {
fow: 'privacyCCPA/H0IeyQBalorD.6nTqqzhNTKECSgOPJCG',
lbi: true,
source: 'consuming-app',
status: consent
}
},
programmaticAds: {
onsite: {
fow: 'privacyCCPA/H0IeyQBalorD.6nTqqzhNTKECSgOPJCG',
lbi: true,
source: 'consuming-app',
status: consent
}
}
behaviouralAds: categoryPayload,
demographicAds: categoryPayload,
programmaticAds: categoryPayload
},
cookieDomain: '.ft.com',
formOfWordsId: 'privacyCCPA'
}
}

/** @type {XPrivacyManager.BasePrivacyManagerProps} */
/** @type {BasePrivacyManagerProps} */
export const defaultProps = {
userId: 'abcde',
legislationId: 'ccpa',
Expand All @@ -75,11 +65,11 @@ export const defaultProps = {
* - Handlers for submit events
* - Post-submission callbacks
*
* @param {Partial<XPrivacyManager.BasePrivacyManagerProps>} propOverrides
* @param {Partial<BasePrivacyManagerProps>} propOverrides
*
* @returns {{
* subject: ReactWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;
* callbacks: XPrivacyManager.OnSaveCallback[] | jest.Mock<any, any>[];
* callbacks: OnSaveCallback[] | jest.Mock<any, any>[];
* submitConsent(value: boolean): Promise<void>;
* }}
*/
Expand Down
6 changes: 3 additions & 3 deletions components/x-privacy-manager/src/__tests__/state.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ describe('x-privacy-manager', () => {
expect(callbacks[0]).toHaveBeenCalledWith(null, { payload: expectedPayload, consent: true })
expect(callbacks[1]).toHaveBeenCalledWith(null, { payload: expectedPayload, consent: true })

// Verify that confimatory nmessage is displayed
const message = subject.find('[data-o-component="o-message"]').first()
const link = message.find('[data-component="redirect-link"]')
// Verify that confimatory message is displayed
const message = await subject.find('[data-o-component="o-message"]').first()
const link = await message.find('[data-component="redirect-link"]')
expect(message).toHaveClassName('o-message--success')
expect(link).toHaveProp('href', '/')
expect(optInInput).toHaveProp('checked', true)
Expand Down
87 changes: 86 additions & 1 deletion components/x-privacy-manager/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
const { getTrackingKeys, getConsentProxyEndpoints } = require('../utils')
const { getTrackingKeys, getConsentProxyEndpoints, getPayload, onConsentSavedFn } = require('../utils')

const fixtures = {
fow: { id: 'xxxx', version: 1 },
consentSource: 'test-source',
getCategoryPayload(status) {
return {
onsite: {
lbi: true,
status,
source: fixtures.consentSource,
fow: `${fixtures.fow.id}/${fixtures.fow.version}`
}
}
}
}

function getExpectedPayload({ consent }) {
const input = {
consent,
consentSource: fixtures.consentSource,
fow: fixtures.fow,
setConsentCookie: true
}

const expectedCategoryPayload = fixtures.getCategoryPayload(input.consent)
return {
setConsentCookie: input.setConsentCookie,
formOfWordsId: fixtures.fow.id,
consentSource: fixtures.consentSource,
data: {
behaviouralAds: expectedCategoryPayload,
demographicAds: expectedCategoryPayload,
programmaticAds: expectedCategoryPayload
}
}
}

describe('getTrackingKeys', () => {
it('Creates legislation-specific tracking event names', () => {
Expand Down Expand Up @@ -45,4 +81,53 @@ describe('getConsentProxyEndpoints', () => {
createOrUpdateRecord: defaultEndpoint
})
})

it('generates a payload', () => {
const input = {
fow: fixtures.fow,
consentSource: fixtures.consentSource,
consent: true,
setConsentCookie: false
}

const expectedCategoryPayload = fixtures.getCategoryPayload(input.consent)

const expected = {
setConsentCookie: input.setConsentCookie,
formOfWordsId: fixtures.fow.id,
consentSource: fixtures.consentSource,
data: {
behaviouralAds: expectedCategoryPayload,
demographicAds: expectedCategoryPayload,
programmaticAds: expectedCategoryPayload
}
}

expect(getPayload(input)).toEqual(expected)
})

describe('Runs callbacks with user input', () => {
test.each([
['{ payload: null }', { consent: false, payload: null }],
['{ consent: true }', { consent: true, payload: getExpectedPayload({ consent: true }) }],
['{ consent: false }', { consent: false, payload: getExpectedPayload({ consent: false }) }],
[
"{ err: 'error', ok: false }",
{ consent: true, err: 'error', ok: false, payload: getExpectedPayload({ consent: true }) }
]
])('onConsentSaved(%s)', (_label, input) => {
const fnA = jest.fn()
const fnB = jest.fn()
const fnC = jest.fn()
const onConsentSaved = onConsentSavedFn([fnA, fnB, fnC])

const { consent, payload, err = null, ok = true } = input
const { _response } = onConsentSaved(input)

expect(fnA).toHaveBeenCalledWith(err, { consent, payload })
expect(fnB).toHaveBeenCalledWith(err, { consent, payload })
expect(fnC).toHaveBeenCalledWith(err, { consent, payload })
expect(_response).toEqual({ ok })
})
})
})
68 changes: 21 additions & 47 deletions components/x-privacy-manager/src/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
/**
* @typedef {import('../typings/x-privacy-manager').SendConsentProps} SendConsentProps
* @typedef {import('../typings/x-privacy-manager').SendConsentResponse} SendConsentResponse
*/

import { withActions } from '@financial-times/x-interaction'
import { getPayload } from './utils'

function onConsentChange(consent) {
return () => ({ consent })
Expand All @@ -9,41 +15,24 @@ function onConsentChange(consent) {
* - consentSource: (e.g. 'next-control-centre')
* - cookieDomain: (e.g. '.thebanker.com')
*
* @param {XPrivacyManager.SendConsentProps} args
* @returns {({ isLoading, consent }: { isLoading: boolean, consent: boolean }) => Promise<{_response: _Response}>}
* @param {SendConsentProps} props
* @returns {SendConsentResponse}
*/
function sendConsent({
setConsentCookie,
consentApiUrl,
onConsentSavedCallbacks,
consentSource,
cookieDomain,
fow
}) {
function sendConsent({ setConsentCookie, consentApiUrl, onConsentSaved, consentSource, cookieDomain, fow }) {
let res

return async ({ isLoading, consent }) => {
if (isLoading) return

const categoryPayload = {
onsite: {
status: consent,
lbi: true,
source: consentSource,
fow: `${fow.id}/${fow.version}`
}
/**
* FoW will be undefined if a user is anonymous (i.e. not logged in)
* In this case there is no need to send anything to the ConsentProxy
*/
if (typeof fow === 'undefined') {
return onConsentSaved({ consent })
}

const payload = {
setConsentCookie,
formOfWordsId: fow.id,
consentSource,
data: {
behaviouralAds: categoryPayload,
demographicAds: categoryPayload,
programmaticAds: categoryPayload
}
}
const payload = getPayload({ fow, consent, consentSource, setConsentCookie })

if (cookieDomain) {
// Optionally specify the domain for the cookie to set on the Consent API
Expand All @@ -53,33 +42,18 @@ function sendConsent({
try {
res = await fetch(consentApiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload),
credentials: 'include'
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})

// On response call any externally defined handlers following Node's convention:
// 1. Either an error object or `null` as the first argument
// 2. An object containing `consent` and `payload` as the second
// Allows callbacks to decide how to handle a failure scenario

if (res.ok === false) {
throw new Error(res.statusText || String(res.status))
}

for (const fn of onConsentSavedCallbacks) {
fn(null, { consent, payload })
}

return { _response: { ok: true } }
return onConsentSaved({ consent, payload })
} catch (err) {
for (const fn of onConsentSavedCallbacks) {
fn(err, { consent, payload })
}

return { _response: { ok: false } }
return onConsentSaved({ consent, payload, err, ok: false })
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions components/x-privacy-manager/src/privacy-manager.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @typedef {import('../typings/x-privacy-manager').BasePrivacyManagerProps} BasePrivacyManagerProps
* @typedef {import('../typings/x-privacy-manager').FormProps} FormProps
*/

import { h } from '@financial-times/x-engine'

import { renderLoggedOutWarning, renderMessage } from './components/messages'
Expand All @@ -13,7 +18,7 @@ const defaultButtonText = {
}

/**
* @param {XPrivacyManager.BasePrivacyManagerProps} Props
* @param {BasePrivacyManagerProps} Props
*/
export function BasePrivacyManager({
userId,
Expand Down Expand Up @@ -58,7 +63,9 @@ export function BasePrivacyManager({
onChange: onConsentChange
})

/** @type {XPrivacyManager.FormProps} */
const onConsentSaved = utils.onConsentSavedFn(onConsentSavedCallbacks)

/** @type {FormProps} */
const formProps = {
consent,
consentApiUrl,
Expand All @@ -68,7 +75,7 @@ export function BasePrivacyManager({
return sendConsent({
setConsentCookie: legislationId === 'gdpr',
consentApiUrl,
onConsentSavedCallbacks,
onConsentSaved,
consentSource,
cookieDomain,
fow
Expand Down
Loading