Skip to content

Commit

Permalink
Change config value from CCPA to USP and USP timer
Browse files Browse the repository at this point in the history
  • Loading branch information
tjeastmond committed Nov 26, 2019
1 parent fa627d0 commit b02dacf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
44 changes: 25 additions & 19 deletions modules/consentManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import strIncludes from 'core-js/library/fn/string/includes';

const DEFAULT_CMP = 'iab';
const DEFAULT_CONSENT_TIMEOUT = 10000;
const DEFAULT_CONSENT_TIMEOUT_USP = 50;
const DEFAULT_ALLOW_AUCTION_WO_CONSENT = true;

export let userCMP;
Expand All @@ -22,19 +23,16 @@ export let staticConsentData;
let consentData;
let addedConsentHook = false;

// ccpa constants
export let userCCPA;
export let consentTimeoutCCPA;

// ccpa globals
let consentDataCCPA;
let addedConsentHookCCPA = false;
// usp
export let consentTimeoutUSP;
let addedConsentHookUSP = false;

// add new CMPs here, with their dedicated lookup function
const cmpCallMap = {
'iab': lookupIabConsent,
'gdpr': lookupIabConsent,
'ccpa': lookupCcpaConsent,
'ccpa': lookupUspConsent,
'usp': lookupUspConsent,
'static': lookupStaticConsentData
};

Expand Down Expand Up @@ -192,7 +190,7 @@ function lookupIabConsent(cmpSuccess, cmpError, hookConfig) {
}
}

function lookupCcpaConsent(ccpaSucess, cmpError, hookConfig) {
function lookupUspConsent(ccpaSucess, cmpError, hookConfig) {
function handleCmpResponseCallbacks() {
const ccpaResponse = {};

Expand Down Expand Up @@ -332,13 +330,13 @@ export function requestCcpaBidsHook(next, reqBidsConfigObj) {
* @param {object} reqBidsConfigObj required; This is the same param that's used in pbjs.requestBids.
* @param {function} fn required; The next function in the chain, used by hook.js
*/
export function requestBidsHook(fn, reqBidsConfigObj, isCCPA = false) {
export function requestBidsHook(fn, reqBidsConfigObj, isUSP = false) {
let userModule = userCMP;
let processFn = processCmpData;

if (isCCPA) {
userModule = 'ccpa';
processFn = processCcpaData; // @TJ
if (isUSP) {
userModule = 'usp';
processFn = processCcpaData;
}

// preserves all module related variables for the current auction instance (used primiarily for concurrent auctions)
Expand Down Expand Up @@ -367,10 +365,11 @@ export function requestBidsHook(fn, reqBidsConfigObj, isCCPA = false) {

// only let this code run if module is still active (ie if the callbacks used by CMPs haven't already finished)
if (!hookConfig.haveExited) {
if (consentTimeout === 0) {
let timeout = userModule === 'usp' ? consentTimeoutUSP : consentTimeout;
if (timeout === 0) {
processFn(undefined, hookConfig);
} else {
hookConfig.timer = setTimeout(cmpTimedOut.bind(null, hookConfig), consentTimeout);
hookConfig.timer = setTimeout(cmpTimedOut.bind(null, hookConfig), timeout);
}
}
}
Expand Down Expand Up @@ -520,13 +519,20 @@ export function setConsentConfig(config, consentModule) {
utils.logInfo(`consentManagement config did not specify cmp. Using system default setting (${DEFAULT_CMP}).`);
}

if (utils.isNumber(config.timeout)) {
if (utils.isNumber(config.timeout)) {
consentTimeout = config.timeout;
} else {
consentTimeout = DEFAULT_CONSENT_TIMEOUT;
utils.logInfo(`consentManagement config did not specify timeout. Using system default setting (${DEFAULT_CONSENT_TIMEOUT}).`);
}

if (utils.isNumber(config.uspTimeout)) {
consentTimeoutUSP = config.uspTimeout
} else {
consentTimeoutUSP = DEFAULT_CONSENT_TIMEOUT_USP;
utils.logInfo(`consentManagement config did not specify timeout f or USP. Using system default setting: (${DEFAULT_CONSENT_TIMEOUT_USP}).`);
}

if (typeof config.allowAuctionWithoutConsent === 'boolean') {
allowAuction = config.allowAuctionWithoutConsent;
} else {
Expand All @@ -545,12 +551,12 @@ export function setConsentConfig(config, consentModule) {
}
}

if (consentModule === 'ccpa' && !addedConsentHookCCPA) {
if (consentModule === 'usp' && !addedConsentHookUSP) {
$$PREBID_GLOBAL$$.requestBids.before(requestCcpaBidsHook, 50);
addedConsentHookCCPA = true;
addedConsentHookUSP = true;
}

if (!addedConsentHook && consentModule !== 'ccpa') {
if (!addedConsentHook && consentModule !== 'usp') {
$$PREBID_GLOBAL$$.requestBids.before(requestBidsHook, 50);
addedConsentHook = true;
}
Expand Down
30 changes: 13 additions & 17 deletions test/spec/modules/consentManagement_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
consentTimeout,
allowAuction,
staticConsentData,
consentTimeoutUSP
} from 'modules/consentManagement';
import {gdprDataHandler, ccpaDataHandler} from 'src/adapterManager';
import * as utils from 'src/utils';
Expand All @@ -14,7 +15,7 @@ import { config } from 'src/config';
let assert = require('chai').assert;
let expect = require('chai').expect;

describe('consentManagement', function () {
describe.only('consentManagement', function () {
describe('setConsentConfig tests:', function () {
describe('empty setConsentConfig value', function () {
beforeEach(function () {
Expand All @@ -31,7 +32,13 @@ describe('consentManagement', function () {
expect(userCMP).to.be.equal('iab');
expect(consentTimeout).to.be.equal(10000);
expect(allowAuction).to.be.true;
sinon.assert.callCount(utils.logInfo, 4);
sinon.assert.callCount(utils.logInfo, 5);
});

it('should use system default values for USP', function () {
setConsentConfig({}, 'usp');
expect(consentTimeoutUSP).to.be.equal(50);
sinon.assert.callCount(utils.logInfo, 5);
});
});

Expand All @@ -51,17 +58,6 @@ describe('consentManagement', function () {
expect(userCMP).to.be.equal('iab');
expect(consentTimeout).to.be.equal(7500);
expect(allowAuction).to.be.false;

setConsentConfig({
'allowAuction': true
});
});
});

describe('valid setConfigConsent value for CCPA', function() {
afterEach(function () {
config.resetConfig();
$$PREBID_GLOBAL$$.requestBids.removeAll();
});
});

Expand Down Expand Up @@ -765,7 +761,7 @@ describe('consentManagement', function () {
}

let _goodConfigWithAllowAuction = { ...goodConfigWithAllowAuction };
_goodConfigWithAllowAuction.consentAPIs = ['ccpa'];
_goodConfigWithAllowAuction.consentAPIs = ['usp'];

// Run tests with JSON response and String response
// from CMP window postMessage listener.
Expand All @@ -775,7 +771,7 @@ describe('consentManagement', function () {
function testIFramedPage(testName, messageFormatString) {
it(`should return the consent string from a postmessage + addEventListener response - ${testName}`, (done) => {
stringifyResponse = messageFormatString;
setConsentConfig(_goodConfigWithAllowAuction, 'ccpa');
setConsentConfig(_goodConfigWithAllowAuction, 'usp');
requestBidsHook(() => {
let consent = ccpaDataHandler.getConsentData();
sinon.assert.notCalled(utils.logWarn);
Expand Down Expand Up @@ -841,9 +837,9 @@ describe('consentManagement', function () {
});

let _goodConfigWithAllowAuction = { ...goodConfigWithAllowAuction };
_goodConfigWithAllowAuction.consentAPIs = ['ccpa'];
_goodConfigWithAllowAuction.consentAPIs = ['usp'];

setConsentConfig(_goodConfigWithAllowAuction, 'ccpa');
setConsentConfig(_goodConfigWithAllowAuction, 'usp');

requestBidsHook(() => {
didHookReturn = true;
Expand Down

0 comments on commit b02dacf

Please sign in to comment.