Skip to content

Commit

Permalink
Merge branch 'candidate' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Case committed Oct 11, 2018
2 parents f5da91b + c82740a commit 27dae86
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 39 deletions.
85 changes: 65 additions & 20 deletions src/lib/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default class Cmp {
return this.store.getFullVendorConsentsObject(vendorIds)
.then(consent => {
consent.gdprApplies = this.gdprApplies;
callback(consent);
callback(consent, true);
return consent;
});
},
Expand All @@ -131,12 +131,16 @@ export default class Cmp {
* Get the encoded vendor consent data value.
*/
getConsentData: (_, callback = () => {}) => {
const consentData = {
gdprApplies: this.gdprApplies,
hasGlobalScope: this.config.storeConsentGlobally,
consentData: this.generateConsentString()
};
callback(consentData, true);
return this.store.getFullVendorConsentsObject()
.then(consent => {
const output = {
gdprApplies: this.gdprApplies,
hasGlobalScope: this.config.storeConsentGlobally,
consentData: consent.consentString
};
callback(output, true);
return output;
});
},

/**
Expand Down Expand Up @@ -224,8 +228,9 @@ export default class Cmp {
* Trigger the consent tool UI to be shown
*/
showConsentTool: (_, callback = () => {}) => {
const self = this;
let _command;
switch (this.config.layout) {
switch (self.config.layout) {
case 'footer':
_command = 'toggleFooterConsentToolShowing';
break;
Expand All @@ -236,8 +241,9 @@ export default class Cmp {
_command = 'toggleConsentToolShowing';
}

this.cmpShown = true;
this.store[_command](true);
self.cmpShown = true;
self.store[_command](true);
self.notify("consentToolDisplayed");
callback(true);
},

Expand All @@ -254,21 +260,43 @@ export default class Cmp {
customPurposeList
} = this.store;

const { purposes: customPurposes = [] } = customPurposeList;
const { purposes = [] } = vendorList || {};
let customPurposes = [];
customPurposes = customPurposeList && customPurposeList.purposes;

const { purposes = [] } = vendorList || {};
const { selectedPurposeIds = new Set() } = persistedVendorConsentData || {};
const { selectedCustomPurposeIds = new Set() } = persistedPublisherConsentData || {};

const allowedPurposeIds = new Set(purposes.map(({id}) => id));
const allowedCustomPurposeIds = new Set(customPurposes.map(({id}) => id));
const allowedPurposeIds = new Set();
for (let i in purposes) {
allowedPurposeIds.add(purposes[i].id);
}

const allowedCustomPurposeIds = new Set();
if (customPurposeList) {
for (let j in customPurposes) {
allowedCustomPurposeIds.add(customPurposes[j].id);
}
}

const selectedAllowedPurposeIds = new Set();
Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id)).forEach((id) => {
selectedAllowedPurposeIds.add(id);
});

const selectedAllowedCustomPurposeIds = new Set();
if (customPurposeList) {
Array.from(selectedCustomPurposeIds).filter(id => allowedCustomPurposeIds.has(id)).forEach((id) => {
selectedAllowedCustomPurposeIds.add(id);
});
}

// Encode the persisted data
return persistedPublisherConsentData && encodePublisherConsentData({
...persistedVendorConsentData,
...persistedPublisherConsentData,
selectedCustomPurposeIds: new Set(Array.from(selectedCustomPurposeIds).filter(id => allowedCustomPurposeIds.has(id))),
selectedPurposeIds: new Set(Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id))),
selectedPurposeIds: selectedAllowedPurposeIds,
selectedCustomPurposeIds: selectedAllowedCustomPurposeIds,
customPurposeList,
vendorList,
});
Expand Down Expand Up @@ -307,14 +335,31 @@ export default class Cmp {
} = persistedVendorConsentData || {};

// Filter consents by values that exist in the current vendorList
const allowedVendorIds = new Set(vendors.map(({id}) => id));
const allowedPurposeIds = new Set(purposes.map(({id}) => id));
const allowedVendorIds = new Set();
for (let i in vendors) {
allowedVendorIds.add(vendors[i].id);
}

const allowedPurposeIds = new Set();
for (let j in purposes) {
allowedPurposeIds.add(purposes[j].id);
}

const selectedAllowedVendorIds = new Set();
Array.from(selectedVendorIds).filter(id => allowedVendorIds.has(id)).forEach((id) => {
selectedAllowedVendorIds.add(id);
});

const selectedAllowedPurposeIds = new Set();
Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id)).forEach((id) => {
selectedAllowedPurposeIds.add(id);
});

// Encode the persisted data
return persistedVendorConsentData && encodeVendorConsentData({
...persistedVendorConsentData,
selectedVendorIds: new Set(Array.from(selectedVendorIds).filter(id => allowedVendorIds.has(id))),
selectedPurposeIds: new Set(Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id))),
selectedVendorIds: selectedAllowedVendorIds,
selectedPurposeIds: selectedAllowedPurposeIds,
vendorList
});
};
Expand Down
39 changes: 29 additions & 10 deletions src/lib/cmp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,37 @@ describe('cmp', () => {
});
});

it('getConsentData executes', (done) => {
cmp.processCommand('getConsentData', null, data => {
expect(typeof data.consentData).to.equal('string');
done();
describe('getConsentData', () => {
beforeEach(() => {
cmp.store = {
consentString: "here's a consent string",
persistedVendorConsentData: {},
persist: () => {
cmp.store.consentString = "here's a persisted consent string";
},
getFullVendorConsentsObject: () => {
return new Promise((resolve) => {
resolve({consentString: cmp.store.consentString});
});
}
};
});
});

it('getConsentData returns persisted data', (done) => {
cmp.store.persist();
cmp.processCommand('getConsentData', null, data => {
expect(typeof data.consentData).to.equal('string');
done();
it('getConsentData executes', (done) => {
cmp.processCommand('getConsentData', null, data => {
expect(typeof data.consentData).to.equal("string");
expect(data.consentData).to.equal("here's a consent string");
done();
});
});

it('getConsentData returns persisted data', (done) => {
cmp.store.persist();
cmp.processCommand('getConsentData', null, data => {
expect(typeof data.consentData).to.equal("string");
expect(data.consentData).to.equal("here's a persisted consent string");
done();
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function openGlobalVendorPortal() {
// Add listener for messages from iframe
window.addEventListener('message', event => {
// Only look at messages with the vendorConsent property
const data = event.data.vendorConsent;
const data = event && event.data && event.data.vendorConsent;
if (data) {
// The iframe has loaded
if (data.command === 'isLoaded' && portalTimeout) {
Expand Down
36 changes: 29 additions & 7 deletions src/lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ function copyData(dataObject) {
const copy = {...dataObject};
for (let key in copy) {
if (copy.hasOwnProperty(key) && copy[key] instanceof Set) {
copy[key] = new Set(copy[key]);
let set = new Set();
copy[key].forEach((val) => {
set.add(val);
});
copy[key] = set;
}
}
return copy;
Expand Down Expand Up @@ -102,8 +106,14 @@ export default class Store {
const {purposes = [], vendors = []} = vendorList;

// No consent will be allowed for vendors or purposes not on the list
const allowedVendorIds = new Set(vendors.map(({id}) => id));
const allowedPurposeIds = new Set(purposes.map(({id}) => id));
const allowedVendorIds = new Set();
for (let i in vendors) {
allowedVendorIds.add(vendors[i].id);
}
const allowedPurposeIds = new Set();
for (let j in purposes) {
allowedPurposeIds.add(purposes[j].id);
}

// Map requested vendorIds
const vendorMap = {};
Expand Down Expand Up @@ -204,7 +214,10 @@ export default class Store {
const {purposes: customPurposes = []} = customPurposeList;

// No consent will be allowed for purposes not on the list
const allowedPurposeIds = new Set(purposes.map(({id}) => id));
const allowedPurposeIds = new Set();
for (let i in purposes) {
allowedPurposeIds.add(purposes[i].id);
}

const lastCustomPurposeId = Math.max(
...customPurposes.map(({id}) => id),
Expand Down Expand Up @@ -404,8 +417,14 @@ export default class Store {

// If vendor consent data has never been persisted set default selected status
if (!created) {
this.vendorConsentData.selectedPurposeIds = new Set(purposes.map(p => p.id));
this.vendorConsentData.selectedVendorIds = new Set(vendors.map(v => v.id));
this.vendorConsentData.selectedPurposeIds = new Set();
for (let i in purposes) {
this.vendorConsentData.selectedPurposeIds.add(purposes[i].id);
}
this.vendorConsentData.selectedVendorIds = new Set();
for (let j in vendors) {
this.vendorConsentData.selectedVendorIds.add(vendors[j].id);
}
}

const {selectedVendorIds = new Set()} = this.vendorConsentData;
Expand All @@ -427,7 +446,10 @@ export default class Store {
// If publisher consent has never been persisted set the default selected status
if (!created) {
const {purposes = [],} = customPurposeList || {};
this.publisherConsentData.selectedCustomPurposeIds = new Set(purposes.map(p => p.id));
this.publisherConsentData.selectedCustomPurposeIds = new Set();
for (let i in purposes) {
this.publisherConsentData.selectedCustomPurposeIds.add(purposes[i].id);
}
}

const {version = 1} = customPurposeList || {};
Expand Down
1 change: 0 additions & 1 deletion src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function initConstants() {

initConstants();


function getConsentsCount(consentObject, vendorList) {
let total = 0;
let consented = 0;
Expand Down

0 comments on commit 27dae86

Please sign in to comment.