Skip to content

Commit

Permalink
added tests for tcf v2
Browse files Browse the repository at this point in the history
  • Loading branch information
smenzer committed Jul 10, 2020
1 parent f952ed3 commit d48599c
Showing 1 changed file with 158 additions and 5 deletions.
163 changes: 158 additions & 5 deletions test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {liveIntentIdSubmodule} from 'modules/liveIntentIdSystem.js';
import {netIdSubmodule} from 'modules/netIdSystem.js';
import {sharedIdSubmodule} from 'modules/sharedIdSystem.js';
import {server} from 'test/mocks/xhr.js';
import {setEnforcementConfig} from 'modules/gdprEnforcement.js';

let assert = require('chai').assert;
let expect = require('chai').expect;
Expand Down Expand Up @@ -1436,14 +1437,15 @@ describe('User ID', function() {
name: 'mockId',
getId: mockGetId,
decode: mockDecode,
extendId: mockExtendId
extendId: mockExtendId,
gvlid: 1
};
const userIdConfig = {
userSync: {
userIds: [{
name: 'mockId',
storage: {
name: 'MOCKID',
name: mockIdCookieName,
type: 'cookie',
refreshInSeconds: 30
}
Expand All @@ -1453,15 +1455,14 @@ describe('User ID', function() {
};

let cmpStub;
let testConsentData;
const consentConfig = {
cmpApi: 'iab',
timeout: 7500,
allowAuctionWithoutConsent: false
};

const sharedBeforeFunction = function() {
// clear cookies
// cookies
expStr = (new Date(Date.now() + 25000).toUTCString());
coreStorage.setCookie(mockIdCookieName, '', EXPIRED_COOKIE_DATE);
coreStorage.setCookie(`${mockIdCookieName}_last`, '', EXPIRED_COOKIE_DATE);
Expand All @@ -1487,7 +1488,7 @@ describe('User ID', function() {
};

describe('TCF v1', function() {
testConsentData = {
let testConsentData = {
gdprApplies: true,
consentData: 'xyz',
apiVersion: 1
Expand Down Expand Up @@ -1623,5 +1624,157 @@ describe('User ID', function() {
expect(userIdStoredConsent.consentString).to.equal(testConsentData.consentData);
});
});

describe('TCF v2', function() {
let testConsentData = {
apiVersion: 2,
gdprApplies: true,
tcString: 'xyz',
purposeOneTreatment: false,
eventStatus: 'tcloaded',
};

beforeEach(function () {
sharedBeforeFunction();

// init gdpr enforcement to allow user id module
setEnforcementConfig({
gdpr: {
rules: [{
purpose: 'storage',
enforcePurpose: false,
enforceVendor: false,
vendorExceptions: []
}]
}
});

// init v2 consent management
delete window.__cmp;
window.__tcfapi = function () { };
cmpStub = sinon.stub(window, '__tcfapi').callsFake((...args) => {
args[2](testConsentData, true);
});
setConsentConfig(consentConfig);
});

afterEach(function() {
sharedAfterFunction();
});

it('does not call getId if no stored consent data and refresh is not needed', function () {
coreStorage.setCookie(mockIdCookieName, JSON.stringify({ id: '1234' }), expStr);
coreStorage.setCookie(`${mockIdCookieName}_last`, (new Date(Date.now() - 1 * 1000).toUTCString()), expStr);

let innerAdUnits;
consentManagementRequestBidsHook(() => { }, {});
requestBidsHook((config) => { innerAdUnits = config.adUnits }, { adUnits });

sinon.assert.notCalled(mockGetId);
sinon.assert.calledOnce(mockDecode);
sinon.assert.calledOnce(mockExtendId);

let consent = gdprDataHandler.getConsentData();
let userIdStoredConsent = JSON.parse(coreStorage.getCookie(CONSENT_LOCAL_STORAGE_NAME));
expect(userIdStoredConsent.gdprApplies).to.equal(consent.gdprApplies);
expect(userIdStoredConsent.gdprApplies).to.equal(testConsentData.gdprApplies);
expect(userIdStoredConsent.consentString).to.equal(consent.consentString);
expect(userIdStoredConsent.consentString).to.equal(testConsentData.tcString);
});

it('calls getId if no stored consent data but refresh is needed', function () {
coreStorage.setCookie(mockIdCookieName, JSON.stringify({ id: '1234' }), expStr);
coreStorage.setCookie(`${mockIdCookieName}_last`, (new Date(Date.now() - 60 * 1000).toUTCString()), expStr);

let innerAdUnits;
consentManagementRequestBidsHook(() => { }, {});
requestBidsHook((config) => { innerAdUnits = config.adUnits }, { adUnits });

sinon.assert.calledOnce(mockGetId);
sinon.assert.calledOnce(mockDecode);
sinon.assert.notCalled(mockExtendId);

let consent = gdprDataHandler.getConsentData();
let userIdStoredConsent = JSON.parse(coreStorage.getCookie(CONSENT_LOCAL_STORAGE_NAME));
expect(userIdStoredConsent.gdprApplies).to.equal(consent.gdprApplies);
expect(userIdStoredConsent.gdprApplies).to.equal(testConsentData.gdprApplies);
expect(userIdStoredConsent.consentString).to.equal(consent.consentString);
expect(userIdStoredConsent.consentString).to.equal(testConsentData.tcString);
});

it('calls getId if empty stored consent and refresh not needed', function () {
coreStorage.setCookie(mockIdCookieName, JSON.stringify({ id: '1234' }), expStr);
coreStorage.setCookie(`${mockIdCookieName}_last`, (new Date(Date.now() - 1 * 1000).toUTCString()), expStr);

setStoredConsentData();

let innerAdUnits;
consentManagementRequestBidsHook(() => { }, {});
requestBidsHook((config) => { innerAdUnits = config.adUnits }, { adUnits });

sinon.assert.calledOnce(mockGetId);
sinon.assert.calledOnce(mockDecode);
sinon.assert.notCalled(mockExtendId);

let consent = gdprDataHandler.getConsentData();
let userIdStoredConsent = JSON.parse(coreStorage.getCookie(CONSENT_LOCAL_STORAGE_NAME));
expect(userIdStoredConsent.gdprApplies).to.equal(consent.gdprApplies);
expect(userIdStoredConsent.gdprApplies).to.equal(testConsentData.gdprApplies);
expect(userIdStoredConsent.consentString).to.equal(consent.consentString);
expect(userIdStoredConsent.consentString).to.equal(testConsentData.tcString);
});

it('calls getId if stored consent does not match current consent and refresh not needed', function () {
coreStorage.setCookie(mockIdCookieName, JSON.stringify({ id: '1234' }), expStr);
coreStorage.setCookie(`${mockIdCookieName}_last`, (new Date(Date.now() - 1 * 1000).toUTCString()), expStr);

setStoredConsentData({
gdprApplies: testConsentData.gdprApplies,
consentString: 'abc',
apiVersion: testConsentData.apiVersion
});

let innerAdUnits;
consentManagementRequestBidsHook(() => { }, {});
requestBidsHook((config) => { innerAdUnits = config.adUnits }, { adUnits });

sinon.assert.calledOnce(mockGetId);
sinon.assert.calledOnce(mockDecode);
sinon.assert.notCalled(mockExtendId);

let consent = gdprDataHandler.getConsentData();
let userIdStoredConsent = JSON.parse(coreStorage.getCookie(CONSENT_LOCAL_STORAGE_NAME));
expect(userIdStoredConsent.gdprApplies).to.equal(consent.gdprApplies);
expect(userIdStoredConsent.gdprApplies).to.equal(testConsentData.gdprApplies);
expect(userIdStoredConsent.consentString).to.equal(consent.consentString);
expect(userIdStoredConsent.consentString).to.equal(testConsentData.tcString);
});

it('does not call getId if stored consent matches current consent and refresh not needed', function () {
coreStorage.setCookie(mockIdCookieName, JSON.stringify({ id: '1234' }), expStr);
coreStorage.setCookie(`${mockIdCookieName}_last`, (new Date(Date.now() - 1 * 1000).toUTCString()), expStr);

setStoredConsentData({
gdprApplies: testConsentData.gdprApplies,
consentString: testConsentData.tcString,
apiVersion: testConsentData.apiVersion
});

let innerAdUnits;
consentManagementRequestBidsHook(() => { }, {});
requestBidsHook((config) => { innerAdUnits = config.adUnits }, { adUnits });

sinon.assert.notCalled(mockGetId);
sinon.assert.calledOnce(mockDecode);
sinon.assert.calledOnce(mockExtendId);

let consent = gdprDataHandler.getConsentData();
let userIdStoredConsent = JSON.parse(coreStorage.getCookie(CONSENT_LOCAL_STORAGE_NAME));
expect(userIdStoredConsent.gdprApplies).to.equal(consent.gdprApplies);
expect(userIdStoredConsent.gdprApplies).to.equal(testConsentData.gdprApplies);
expect(userIdStoredConsent.consentString).to.equal(consent.consentString);
expect(userIdStoredConsent.consentString).to.equal(testConsentData.tcString);
});
});
});
});

0 comments on commit d48599c

Please sign in to comment.