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

fix: prevent error when forwarding user attributes to kits while kit … #947

Merged
24 changes: 16 additions & 8 deletions src/kitBlocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,25 @@ export default class KitBlocker {
if (!this.blockUserAttributes) {
return false
}
if (this.blockUserAttributes) {
const matchedAttributes = this.dataPlanMatchLookups['user_attributes'];
if (matchedAttributes === true) {
return false
}
if (!matchedAttributes[key]) {
return true
const matchedAttributes = this.dataPlanMatchLookups['user_attributes'];

// When additionalProperties is set to true, matchedAttributes
// will be a boolean, otherwise it will return an object
if (typeof matchedAttributes === 'boolean' && matchedAttributes) {
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved
return false
}

if (typeof matchedAttributes === "object") {
if (matchedAttributes[key] === true) {
return false;
} else {
return true;
}
}

return false
// When "Block unplanned user attributes" is enabled and "Allow unplanned user
// attributes" is also enabled in the UI, allowing unplanned user attributes will be prioritized
return false;
}

isIdentityBlocked(key: string) {
Expand Down
21 changes: 21 additions & 0 deletions test/src/tests-kit-blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KitBlocker from '../../src/kitBlocking';
import Types from '../../src/types';
import { DataPlanVersion } from '@mparticle/data-planning-models';
import fetchMock from 'fetch-mock/esm/client';
import { expect } from 'chai'
const { findBatch, waitForCondition, fetchMockSuccess, hasIdentifyReturned } = Utils;

let forwarderDefaultConfiguration = Utils.forwarderDefaultConfiguration,
Expand Down Expand Up @@ -616,6 +617,26 @@ describe('kit blocking', () => {
})
});

it('integration test - should not throw an error when unplanned user attributes are allowed and block.ua = true', function(done) {
window.mParticle.config.kitConfigs.push(forwarderDefaultConfiguration('MockForwarder'));
window.mParticle.init(apiKey, window.mParticle.config);

// save old data points for reset later
const oldDataPoints = dataPlan.dtpn.vers.version_document.data_points;
// when "Allow unplanned user attributes" is enabled, the data points returned is an empty array
dataPlan.dtpn.vers.version_document.data_points = [];
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved
let kitBlocker = new KitBlocker(kitBlockerDataPlan, window.mParticle.getInstance());

expect(() => { kitBlocker.isAttributeKeyBlocked('unplannedAttr') }).to.not.throw(TypeError, /Cannot read properties of undefined \(reading 'unplannedAttr'\)/)
// "Allow unplanned user attributes" is prioritized when blocking unplanned attributes is also enabled, hence the expected value is false
expect(kitBlocker.isAttributeKeyBlocked('unplannedAttr')).to.equal(false)
// reset data points
dataPlan.dtpn.vers.version_document.data_points = oldDataPoints;

done();

});

it('integration test - should allow an unplanned attribute to be set on forwarder if additionalProperties = true and blok.ua = true', function(done) {
let userAttributeDataPoint = dataPlan.dtpn.vers.version_document.data_points.find(dataPoint => {
return dataPoint.match.type === 'user_attributes'
Expand Down
Loading